01: package org.geotools.maven.xmlcodegen.templates;
02:
03: import java.util.*;
04: import java.io.*;
05: import org.eclipse.xsd.*;
06: import org.geotools.xml.*;
07:
08: public class SchemaLocationResolverTemplate {
09: protected static String nl;
10:
11: public static synchronized SchemaLocationResolverTemplate create(
12: String lineSeparator) {
13: nl = lineSeparator;
14: SchemaLocationResolverTemplate result = new SchemaLocationResolverTemplate();
15: nl = null;
16: return result;
17: }
18:
19: protected final String NL = nl == null ? (System.getProperties()
20: .getProperty("line.separator")) : nl;
21: protected final String TEXT_1 = NL
22: + "import org.eclipse.xsd.XSDSchema;" + NL
23: + "import org.eclipse.xsd.util.XSDSchemaLocationResolver;"
24: + NL + "" + NL + "/**" + NL + " * " + NL + " * @generated"
25: + NL + " */" + NL + "public class ";
26: protected final String TEXT_2 = "SchemaLocationResolver implements XSDSchemaLocationResolver {"
27: + NL
28: + ""
29: + NL
30: + "\t/**"
31: + NL
32: + "\t * <!-- begin-user-doc -->"
33: + NL
34: + "\t * <!-- end-user-doc -->"
35: + NL
36: + "\t *"
37: + NL
38: + "\t *\t@generated modifiable"
39: + NL
40: + "\t */"
41: + NL
42: + "\tpublic String resolveSchemaLocation(XSDSchema xsdSchema, String namespaceURI, String schemaLocationURI) {"
43: + NL
44: + "\t\tif (schemaLocationURI == null)"
45: + NL
46: + "\t\t\treturn null;"
47: + NL
48: + "\t\t\t"
49: + NL
50: + "\t\t//if no namespace given, assume default for the current schema"
51: + NL
52: + "\t\tif ((namespaceURI == null || \"\".equals(namespaceURI)) && xsdSchema != null) {"
53: + NL
54: + "\t\t\tnamespaceURI = xsdSchema.getTargetNamespace();"
55: + NL + "\t\t}" + NL + "\t\t\t";
56: protected final String TEXT_3 = NL + "\t\tif (\"";
57: protected final String TEXT_4 = "\".equals(namespaceURI)) {" + NL
58: + "\t\t\tif (schemaLocationURI.endsWith(\"";
59: protected final String TEXT_5 = "\")) {" + NL
60: + "\t\t\t\treturn getClass().getResource(\"";
61: protected final String TEXT_6 = "\").toString();" + NL + "\t\t\t}"
62: + NL + "\t\t}";
63: protected final String TEXT_7 = NL + "\t\t" + NL
64: + "\t\treturn null;" + NL + "\t}" + NL + "" + NL + "}";
65:
66: public String generate(Object argument) {
67: final StringBuffer stringBuffer = new StringBuffer();
68:
69: Object[] args = (Object[]) argument;
70: XSDSchema schema = (XSDSchema) args[0];
71: List includes = (List) args[1];
72: List namespaces = (List) args[2];
73:
74: String ns = schema.getTargetNamespace();
75: String prefix = Schemas.getTargetPrefix(schema);
76:
77: stringBuffer.append(TEXT_1);
78: stringBuffer.append(prefix.toUpperCase());
79: stringBuffer.append(TEXT_2);
80:
81: for (int i = 0; i < includes.size(); i++) {
82: File include = (File) includes.get(i);
83: String namespace = (String) namespaces.get(i);
84:
85: stringBuffer.append(TEXT_3);
86: stringBuffer.append(namespace);
87: stringBuffer.append(TEXT_4);
88: stringBuffer.append(include.getName());
89: stringBuffer.append(TEXT_5);
90: stringBuffer.append(include.getName());
91: stringBuffer.append(TEXT_6);
92:
93: }
94:
95: stringBuffer.append(TEXT_7);
96: return stringBuffer.toString();
97: }
98: }
|