View Javadoc

1   package org.apache.torque.mojo.morph;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.apache.commons.logging.Log;
5   import org.apache.commons.logging.LogFactory;
6   
7   public class SchemaMorpher extends Morpher {
8   	private static final Log log = LogFactory.getLog(SchemaMorpher.class);
9   
10  	// Ant impex has kfs hard coded
11  	String antSchemaName = "kfs";
12  	// Token we can look for to help identify this schema XML file as an Impex file generated by the Ant process
13  	String antSchemaToken = "name=\"" + antSchemaName + "\"";
14  	// Ant impex is hard coded to database.dtd
15  	String antDTDString = "\"database.dtd\"";
16  	// The Kuali database.dtd
17  	String newDTDString = "\"http://www.kuali.org/dtd/database.dtd\"";
18  	// Ant impex comment
19  	String antComment = "<!-- Autogenerated by KualiTorqueJDBCTransformTask! -->";
20  	// Maven impex comment
21  	String newComment = "<!-- Autogenerated by the Maven Impex Plugin -->";
22  	// Ant prologue
23  	String antPrologue = "<?xml version=\"1.0\"?>";
24  	// New prologue
25  	String newPrologue = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
26  
27  	public SchemaMorpher() {
28  		this(null, null);
29  	}
30  
31  	public SchemaMorpher(MorphRequest morphRequest, String artifactId) {
32  		super();
33  		this.artifactId = artifactId;
34  		this.morphRequest = morphRequest;
35  	}
36  
37  	/**
38  	 * Attempt to determine if this content is from an Ant Impex XML export
39  	 */
40  	protected boolean isAntImpexSchemaXML(String contents) {
41  		if (contents == null) {
42  			return false;
43  		}
44  		if (contents.indexOf(antSchemaToken) == -1) {
45  			return false;
46  		}
47  		if (contents.indexOf(antDTDString) == -1) {
48  			return false;
49  		}
50  		if (contents.indexOf(antComment) == -1) {
51  			return false;
52  		}
53  		// All 3 tokens we know about were present in the String
54  		// Pretty good chance it is content from an Ant Impex export
55  		return true;
56  	}
57  
58  	/**
59  	 * Return true if we need to morph this file
60  	 */
61  	protected boolean isMorphNeeded(String contents) {
62  		if (!isAntImpexSchemaXML(contents)) {
63  			log.warn("Unable to determine if this is a schema exported from Ant Impex");
64  		}
65  
66  		// Look for the DTD the Maven Impex Plugin uses
67  		int pos = contents.indexOf(newDTDString);
68  
69  		if (pos == -1) {
70  			// It isn't there so we should morph
71  			return true;
72  		} else {
73  			// It is already there, we are good to go
74  			return false;
75  		}
76  	}
77  
78  	/**
79  	 * Morph an Ant Impex XML file into a Maven Impex Plugin XML file
80  	 */
81  	@Override
82  	protected String getMorphedContents(String contents) {
83  		contents = StringUtils.replace(contents, antDTDString, newDTDString);
84  		contents = StringUtils.replace(contents, antComment, newComment);
85  		contents = StringUtils.replace(contents, antPrologue, newPrologue);
86  		return StringUtils.replace(contents, "name=\"" + antSchemaName + "\">", "name=\"" + getArtifactId() + "\">");
87  	}
88  
89  	public String getAntSchemaName() {
90  		return antSchemaName;
91  	}
92  
93  	public void setAntSchemaName(String defaultSchemaName) {
94  		this.antSchemaName = defaultSchemaName;
95  	}
96  
97  	public String getAntSchemaToken() {
98  		return antSchemaToken;
99  	}
100 
101 	public void setAntSchemaToken(String defaultSchemaToken) {
102 		this.antSchemaToken = defaultSchemaToken;
103 	}
104 
105 	public String getAntDTDString() {
106 		return antDTDString;
107 	}
108 
109 	public void setAntDTDString(String defaultDTDString) {
110 		this.antDTDString = defaultDTDString;
111 	}
112 
113 	public String getNewDTDString() {
114 		return newDTDString;
115 	}
116 
117 	public void setNewDTDString(String newDTDString) {
118 		this.newDTDString = newDTDString;
119 	}
120 
121 	public String getAntComment() {
122 		return antComment;
123 	}
124 
125 	public void setAntComment(String defaultComment) {
126 		this.antComment = defaultComment;
127 	}
128 
129 	public String getNewComment() {
130 		return newComment;
131 	}
132 
133 	public void setNewComment(String newComment) {
134 		this.newComment = newComment;
135 	}
136 
137 	public String getAntPrologue() {
138 		return antPrologue;
139 	}
140 
141 	public void setAntPrologue(String antPrologue) {
142 		this.antPrologue = antPrologue;
143 	}
144 
145 	public String getNewPrologue() {
146 		return newPrologue;
147 	}
148 
149 	public void setNewPrologue(String newPrologue) {
150 		this.newPrologue = newPrologue;
151 	}
152 
153 }