001: package org.geotools.maven.xmlcodegen.templates;
002:
003: import java.util.*;
004: import org.apache.xml.serialize.*;
005: import org.eclipse.xsd.*;
006: import java.io.*;
007: import org.geotools.xml.*;
008:
009: public class CLASS {
010: protected static String nl;
011:
012: public static synchronized CLASS create(String lineSeparator) {
013: nl = lineSeparator;
014: CLASS result = new CLASS();
015: nl = null;
016: return result;
017: }
018:
019: protected final String NL = nl == null ? (System.getProperties()
020: .getProperty("line.separator")) : nl;
021: protected final String TEXT_1 = NL + "import org.geotools.xml.*;"
022: + NL;
023: protected final String TEXT_2 = NL + "import ";
024: protected final String TEXT_3 = ";\t\t";
025: protected final String TEXT_4 = NL + NL
026: + "import javax.xml.namespace.QName;" + NL + "" + NL
027: + "/**" + NL + " * Binding object for the ";
028: protected final String TEXT_5 = " ";
029: protected final String TEXT_6 = ":";
030: protected final String TEXT_7 = "." + NL + " *" + NL + " * <p>"
031: + NL + " *\t<pre>" + NL + " *\t <code>";
032: protected final String TEXT_8 = NL + " * ";
033: protected final String TEXT_9 = " " + NL + " *\t\t" + NL
034: + " *\t </code>" + NL + " *\t </pre>" + NL + " * </p>"
035: + NL + " *" + NL + " * @generated" + NL + " */";
036: protected final String TEXT_10 = NL + "public class ";
037: protected final String TEXT_11 = " extends AbstractSimpleBinding {";
038: protected final String TEXT_12 = NL + "public class ";
039: protected final String TEXT_13 = " extends AbstractComplexBinding {";
040: protected final String TEXT_14 = NL;
041: protected final String TEXT_15 = NL + "\t";
042: protected final String TEXT_16 = " ";
043: protected final String TEXT_17 = ";\t";
044: protected final String TEXT_18 = "\t" + NL + "\t";
045: protected final String TEXT_19 = NL + "\t/**" + NL
046: + "\t * @generated" + NL + "\t */" + NL
047: + "\tpublic QName getTarget() {" + NL + "\t\treturn ";
048: protected final String TEXT_20 = ".";
049: protected final String TEXT_21 = ";" + NL + "\t}" + NL + "\t" + NL
050: + "\t/**" + NL + "\t * <!-- begin-user-doc -->" + NL
051: + "\t * <!-- end-user-doc -->" + NL + "\t *\t" + NL
052: + "\t * @generated modifiable" + NL + "\t */\t" + NL
053: + "\tpublic Class getType() {" + NL + "\t\treturn null;"
054: + NL + "\t}" + NL + "\t";
055: protected final String TEXT_22 = NL
056: + "\t/**"
057: + NL
058: + "\t * <!-- begin-user-doc -->"
059: + NL
060: + "\t * <!-- end-user-doc -->"
061: + NL
062: + "\t *\t"
063: + NL
064: + "\t * @generated modifiable"
065: + NL
066: + "\t */\t"
067: + NL
068: + "\tpublic Object parse(InstanceComponent instance, Object value) "
069: + NL + "\t\tthrows Exception {" + NL + "\t\t" + NL
070: + "\t\t//TODO: implement" + NL + "\t\treturn null;" + NL
071: + "\t}" + NL;
072: protected final String TEXT_23 = NL
073: + "\t/**"
074: + NL
075: + "\t * <!-- begin-user-doc -->"
076: + NL
077: + "\t * <!-- end-user-doc -->"
078: + NL
079: + "\t *\t"
080: + NL
081: + "\t * @generated modifiable"
082: + NL
083: + "\t */\t"
084: + NL
085: + "\tpublic Object parse(ElementInstance instance, Node node, Object value) "
086: + NL + "\t\tthrows Exception {" + NL + "\t\t" + NL
087: + "\t\t//TODO: implement" + NL + "\t\treturn null;" + NL
088: + "\t}" + NL;
089: protected final String TEXT_24 = NL + "}";
090:
091: public String generate(Object argument) {
092: final StringBuffer stringBuffer = new StringBuffer();
093:
094: Object[] arguments = (Object[]) argument;
095: XSDNamedComponent named = (XSDNamedComponent) arguments[0];
096: Map constructorArgs = (Map) arguments[1];
097:
098: XSDSchema schema = named.getSchema();
099: XSDTypeDefinition type = null;
100: String desc = null;
101: if (named instanceof XSDTypeDefinition) {
102: type = (XSDTypeDefinition) named;
103: desc = "type";
104: }
105: if (named instanceof XSDElementDeclaration) {
106: type = ((XSDElementDeclaration) named).getTypeDefinition();
107: desc = "element";
108: }
109: if (named instanceof XSDAttributeDeclaration) {
110: type = ((XSDAttributeDeclaration) named)
111: .getTypeDefinition();
112: desc = "attribute";
113: }
114:
115: String ns = schema.getTargetNamespace();
116: String prefix = Schemas.getTargetPrefix(schema);
117:
118: stringBuffer.append(TEXT_1);
119:
120: if (constructorArgs != null) {
121: for (Iterator i = constructorArgs.values().iterator(); i
122: .hasNext();) {
123: Class arg = (Class) i.next();
124:
125: stringBuffer.append(TEXT_2);
126: stringBuffer.append(arg.getName());
127: stringBuffer.append(TEXT_3);
128:
129: }
130: }
131:
132: stringBuffer.append(TEXT_4);
133: stringBuffer.append(desc);
134: stringBuffer.append(TEXT_5);
135: stringBuffer.append(named.getTargetNamespace());
136: stringBuffer.append(TEXT_6);
137: stringBuffer.append(named.getName());
138: stringBuffer.append(TEXT_7);
139:
140: OutputFormat output = new OutputFormat();
141: output.setOmitXMLDeclaration(true);
142: output.setIndenting(true);
143:
144: StringWriter writer = new StringWriter();
145: XMLSerializer serializer = new XMLSerializer(writer, output);
146:
147: try {
148: serializer.serialize(named.getElement());
149: } catch (IOException e) {
150: e.printStackTrace();
151: return null;
152: }
153:
154: String[] lines = writer.getBuffer().toString().split("\n");
155: for (int i = 0; i < lines.length; i++) {
156:
157: stringBuffer.append(TEXT_8);
158: stringBuffer.append(lines[i].replaceAll("<", "<")
159: .replaceAll(">", ">"));
160:
161: }
162:
163: stringBuffer.append(TEXT_9);
164:
165: String className = named.getName().substring(0, 1)
166: .toUpperCase()
167: + named.getName().substring(1) + "Binding";
168:
169: if (type instanceof XSDSimpleTypeDefinition) {
170:
171: stringBuffer.append(TEXT_10);
172: stringBuffer.append(className);
173: stringBuffer.append(TEXT_11);
174:
175: } else {
176:
177: stringBuffer.append(TEXT_12);
178: stringBuffer.append(className);
179: stringBuffer.append(TEXT_13);
180:
181: }
182:
183: stringBuffer.append(TEXT_14);
184:
185: if (constructorArgs != null) {
186: List fieldNames = new ArrayList();
187:
188: StringBuffer constructor = new StringBuffer();
189: constructor.append("public " + className + "( ");
190:
191: for (Iterator i = constructorArgs.entrySet().iterator(); i
192: .hasNext();) {
193: Map.Entry entry = (Map.Entry) i.next();
194: String fieldName = (String) entry.getKey();
195: Class arg = (Class) entry.getValue();
196:
197: String typeName = arg.getName();
198:
199: if (typeName.lastIndexOf('.') != -1) {
200: typeName = typeName.substring(typeName
201: .lastIndexOf('.') + 1);
202: }
203:
204: fieldNames.add(fieldName);
205:
206: stringBuffer.append(TEXT_15);
207: stringBuffer.append(typeName);
208: stringBuffer.append(TEXT_16);
209: stringBuffer.append(fieldName);
210: stringBuffer.append(TEXT_17);
211:
212: constructor.append(typeName + " " + fieldName);
213: if (i.hasNext()) {
214: constructor.append(",");
215: }
216: }
217:
218: constructor.append(" ) {\n");
219: for (int i = 0; i < fieldNames.size(); i++) {
220:
221: String fieldName = (String) fieldNames.get(i);
222: constructor.append("\t\tthis." + fieldName + " = "
223: + fieldName + ";\n");
224:
225: }
226: constructor.append("\t}\n");
227:
228: stringBuffer.append(TEXT_18);
229: stringBuffer.append(constructor.toString());
230:
231: }
232:
233: stringBuffer.append(TEXT_19);
234: stringBuffer.append(prefix.toUpperCase());
235: stringBuffer.append(TEXT_20);
236: stringBuffer.append(named.getName());
237: stringBuffer.append(TEXT_21);
238:
239: if (type instanceof XSDSimpleTypeDefinition) {
240:
241: stringBuffer.append(TEXT_22);
242:
243: } else {
244:
245: stringBuffer.append(TEXT_23);
246:
247: }
248:
249: stringBuffer.append(TEXT_24);
250: return stringBuffer.toString();
251: }
252: }
|