001: package org.geotools.maven.xmlcodegen.templates;
002:
003: import java.util.*;
004: import java.io.*;
005: import org.opengis.feature.type.Schema;
006: import org.geotools.maven.xmlcodegen.*;
007: import org.geotools.feature.*;
008: import org.opengis.feature.type.TypeName;
009: import org.apache.xml.serialize.*;
010: import org.eclipse.xsd.*;
011:
012: public class SchemaClassTemplate {
013: protected static String nl;
014:
015: public static synchronized SchemaClassTemplate create(
016: String lineSeparator) {
017: nl = lineSeparator;
018: SchemaClassTemplate result = new SchemaClassTemplate();
019: nl = null;
020: return result;
021: }
022:
023: protected final String NL = nl == null ? (System.getProperties()
024: .getProperty("line.separator")) : nl;
025: protected final String TEXT_1 = "";
026: protected final String TEXT_2 = NL + NL + NL
027: + "import org.geotools.feature.AttributeType;" + NL
028: + "import org.geotools.feature.AttributeTypeFactory;" + NL
029: + "import org.geotools.feature.Name;" + NL
030: + "import org.geotools.feature.type.SchemaImpl;" + NL;
031: protected final String TEXT_3 = NL + "import ";
032: protected final String TEXT_4 = ";";
033: protected final String TEXT_5 = NL + NL + "public class ";
034: protected final String TEXT_6 = "Schema extends SchemaImpl {" + NL;
035: protected final String TEXT_7 = NL + "\t/**" + NL + "\t * <p>" + NL
036: + " \t * <pre>" + NL + " \t * <code>";
037: protected final String TEXT_8 = NL + " \t * ";
038: protected final String TEXT_9 = NL + "\t *" + NL
039: + "\t * </code>" + NL + "\t * </pre>" + NL
040: + "\t * </p>" + NL + "\t *" + NL + "\t * @generated" + NL
041: + "\t */" + NL + "\tpublic static final AttributeType ";
042: protected final String TEXT_10 = "_TYPE = " + NL
043: + "\t\tAttributeTypeFactory.newAttributeType( \"";
044: protected final String TEXT_11 = "\", ";
045: protected final String TEXT_12 = " );";
046: protected final String TEXT_13 = NL + NL + "\tpublic ";
047: protected final String TEXT_14 = "Schema() {" + NL + "\t\tsuper(\"";
048: protected final String TEXT_15 = "\");" + NL + "\t\t";
049: protected final String TEXT_16 = NL + "\t\tput(new Name( \"";
050: protected final String TEXT_17 = "\", \"";
051: protected final String TEXT_18 = "\" ),";
052: protected final String TEXT_19 = "_TYPE);";
053: protected final String TEXT_20 = NL + "\t}" + NL + "}";
054:
055: public String generate(Object argument) {
056: final StringBuffer stringBuffer = new StringBuffer();
057: stringBuffer.append(TEXT_1);
058:
059: Object[] arguments = (Object[]) argument;
060: Schema schema = (Schema) arguments[0];
061:
062: String prefix = (String) arguments[1];
063: prefix = prefix.toUpperCase();
064:
065: SchemaGenerator sg = (SchemaGenerator) arguments[2];
066:
067: stringBuffer.append(TEXT_2);
068:
069: HashMap ns2import = new HashMap();
070: for (Iterator itr = sg.getImports().iterator(); itr.hasNext();) {
071: Schema imported = (Schema) itr.next();
072: String fullClassName = imported.getClass().getName();
073: String className = fullClassName.substring(fullClassName
074: .lastIndexOf(".") + 1);
075:
076: ns2import.put(imported.namespace().getURI(), className);
077:
078: stringBuffer.append(TEXT_3);
079: stringBuffer.append(fullClassName);
080: stringBuffer.append(TEXT_4);
081:
082: }
083:
084: stringBuffer.append(TEXT_5);
085: stringBuffer.append(prefix);
086: stringBuffer.append(TEXT_6);
087:
088: for (Iterator itr = schema.values().iterator(); itr.hasNext();) {
089: AttributeType type = (AttributeType) itr.next();
090:
091: String name = type.getName();
092: String binding = type.getType().getName() + ".class";
093:
094: stringBuffer.append(TEXT_7);
095:
096: XSDTypeDefinition xsdType = sg.getXSDType(type);
097: OutputFormat output = new OutputFormat();
098: output.setOmitXMLDeclaration(true);
099: output.setIndenting(true);
100:
101: StringWriter writer = new StringWriter();
102: XMLSerializer serializer = new XMLSerializer(writer, output);
103:
104: try {
105: serializer.serialize(xsdType.getElement());
106: } catch (IOException e) {
107: e.printStackTrace();
108: return null;
109: }
110:
111: String[] lines = writer.getBuffer().toString().split("\n");
112: for (int i = 0; i < lines.length; i++) {
113:
114: stringBuffer.append(TEXT_8);
115: stringBuffer.append(lines[i].replaceAll("<", "<")
116: .replaceAll(">", ">"));
117:
118: }
119:
120: stringBuffer.append(TEXT_9);
121: stringBuffer.append(name.toUpperCase());
122: stringBuffer.append(TEXT_10);
123: stringBuffer.append(name);
124: stringBuffer.append(TEXT_11);
125: stringBuffer.append(binding);
126: stringBuffer.append(TEXT_12);
127:
128: }
129:
130: stringBuffer.append(TEXT_13);
131: stringBuffer.append(prefix);
132: stringBuffer.append(TEXT_14);
133: stringBuffer.append(schema.toURI());
134: stringBuffer.append(TEXT_15);
135:
136: for (Iterator itr = schema.values().iterator(); itr.hasNext();) {
137: AttributeType type = (AttributeType) itr.next();
138:
139: stringBuffer.append(TEXT_16);
140: stringBuffer.append(schema.namespace().getURI());
141: stringBuffer.append(TEXT_17);
142: stringBuffer.append(type.getName());
143: stringBuffer.append(TEXT_18);
144: stringBuffer.append(type.getName().toUpperCase());
145: stringBuffer.append(TEXT_19);
146:
147: }
148:
149: stringBuffer.append(TEXT_20);
150: return stringBuffer.toString();
151: }
152: }
|