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 | 0 | private static final Log log = LogFactory.getLog(SchemaMorpher.class); |
9 | |
|
10 | |
|
11 | 0 | String antSchemaName = "kfs"; |
12 | |
|
13 | 0 | String antSchemaToken = "name=\"" + antSchemaName + "\""; |
14 | |
|
15 | 0 | String antDTDString = "\"database.dtd\""; |
16 | |
|
17 | 0 | String newDTDString = "\"http://www.kuali.org/dtd/database.dtd\""; |
18 | |
|
19 | 0 | String antComment = "<!-- Autogenerated by KualiTorqueJDBCTransformTask! -->"; |
20 | |
|
21 | 0 | String newComment = "<!-- Autogenerated by the Maven Impex Plugin -->"; |
22 | |
|
23 | 0 | String antPrologue = "<?xml version=\"1.0\"?>"; |
24 | |
|
25 | 0 | String newPrologue = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; |
26 | |
|
27 | |
public SchemaMorpher() { |
28 | 0 | this(null, null); |
29 | 0 | } |
30 | |
|
31 | |
public SchemaMorpher(MorphRequest morphRequest, String artifactId) { |
32 | 0 | super(); |
33 | 0 | this.artifactId = artifactId; |
34 | 0 | this.morphRequest = morphRequest; |
35 | 0 | } |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
protected boolean isAntImpexSchemaXML(String contents) { |
41 | 0 | if (contents == null) { |
42 | 0 | return false; |
43 | |
} |
44 | 0 | if (contents.indexOf(antSchemaToken) == -1) { |
45 | 0 | return false; |
46 | |
} |
47 | 0 | if (contents.indexOf(antDTDString) == -1) { |
48 | 0 | return false; |
49 | |
} |
50 | 0 | if (contents.indexOf(antComment) == -1) { |
51 | 0 | return false; |
52 | |
} |
53 | |
|
54 | |
|
55 | 0 | return true; |
56 | |
} |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
protected boolean isMorphNeeded(String contents) { |
62 | 0 | if (!isAntImpexSchemaXML(contents)) { |
63 | 0 | log.warn("Unable to determine if this is a schema exported from Ant Impex"); |
64 | |
} |
65 | |
|
66 | |
|
67 | 0 | int pos = contents.indexOf(newDTDString); |
68 | |
|
69 | 0 | if (pos == -1) { |
70 | |
|
71 | 0 | return true; |
72 | |
} else { |
73 | |
|
74 | 0 | return false; |
75 | |
} |
76 | |
} |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
@Override |
82 | |
protected String getMorphedContents(String contents) { |
83 | 0 | contents = StringUtils.replace(contents, antDTDString, newDTDString); |
84 | 0 | contents = StringUtils.replace(contents, antComment, newComment); |
85 | 0 | contents = StringUtils.replace(contents, antPrologue, newPrologue); |
86 | 0 | return StringUtils.replace(contents, "name=\"" + antSchemaName + "\">", "name=\"" + getArtifactId() + "\">"); |
87 | |
} |
88 | |
|
89 | |
public String getAntSchemaName() { |
90 | 0 | return antSchemaName; |
91 | |
} |
92 | |
|
93 | |
public void setAntSchemaName(String defaultSchemaName) { |
94 | 0 | this.antSchemaName = defaultSchemaName; |
95 | 0 | } |
96 | |
|
97 | |
public String getAntSchemaToken() { |
98 | 0 | return antSchemaToken; |
99 | |
} |
100 | |
|
101 | |
public void setAntSchemaToken(String defaultSchemaToken) { |
102 | 0 | this.antSchemaToken = defaultSchemaToken; |
103 | 0 | } |
104 | |
|
105 | |
public String getAntDTDString() { |
106 | 0 | return antDTDString; |
107 | |
} |
108 | |
|
109 | |
public void setAntDTDString(String defaultDTDString) { |
110 | 0 | this.antDTDString = defaultDTDString; |
111 | 0 | } |
112 | |
|
113 | |
public String getNewDTDString() { |
114 | 0 | return newDTDString; |
115 | |
} |
116 | |
|
117 | |
public void setNewDTDString(String newDTDString) { |
118 | 0 | this.newDTDString = newDTDString; |
119 | 0 | } |
120 | |
|
121 | |
public String getAntComment() { |
122 | 0 | return antComment; |
123 | |
} |
124 | |
|
125 | |
public void setAntComment(String defaultComment) { |
126 | 0 | this.antComment = defaultComment; |
127 | 0 | } |
128 | |
|
129 | |
public String getNewComment() { |
130 | 0 | return newComment; |
131 | |
} |
132 | |
|
133 | |
public void setNewComment(String newComment) { |
134 | 0 | this.newComment = newComment; |
135 | 0 | } |
136 | |
|
137 | |
public String getAntPrologue() { |
138 | 0 | return antPrologue; |
139 | |
} |
140 | |
|
141 | |
public void setAntPrologue(String antPrologue) { |
142 | 0 | this.antPrologue = antPrologue; |
143 | 0 | } |
144 | |
|
145 | |
public String getNewPrologue() { |
146 | 0 | return newPrologue; |
147 | |
} |
148 | |
|
149 | |
public void setNewPrologue(String newPrologue) { |
150 | 0 | this.newPrologue = newPrologue; |
151 | 0 | } |
152 | |
|
153 | |
} |