001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.maven.xmlcodegen;
017:
018: import org.eclipse.xsd.XSDInclude;
019: import org.eclipse.xsd.XSDSchema;
020: import org.eclipse.xsd.util.XSDUtil;
021: import org.geotools.xml.Schemas;
022: import java.io.File;
023: import java.io.IOException;
024: import java.net.MalformedURLException;
025: import java.net.URI;
026: import java.net.URISyntaxException;
027: import java.net.URL;
028: import java.util.ArrayList;
029: import java.util.Iterator;
030: import java.util.List;
031: import java.util.logging.Level;
032:
033: /**
034: * Generates an instance of {@link org.eclipse.xsd.util.XSDSchemaLocationResolver} for
035: * a particular schema.
036: * <p>
037: * The schema supplied, and any included schemas ( not imported ), are added to
038: * the set of schemas that the resulting class can resolve.
039: * </p>
040: *
041: * @author Justin Deoliveira, The Open Planning Project, jdeolive@openplans.org
042: *
043: */
044: public class SchemaLocationResolverGenerator extends AbstractGenerator {
045: public void generate(XSDSchema schema) {
046: ArrayList includes = new ArrayList();
047: ArrayList namespaces = new ArrayList();
048:
049: File file = null;
050: try {
051: file = findSchemaFile(schema.getSchemaLocation());
052: } catch (Exception e) {
053: logger.log(Level.SEVERE, "", e);
054: }
055:
056: if (file != null) {
057: includes.add(file);
058: namespaces.add(schema.getTargetNamespace());
059: } else {
060: logger.log(Level.SEVERE, "Could not find: "
061: + schema.getSchemaLocation() + " to copy.");
062: }
063:
064: List included = Schemas.getIncludes(schema);
065:
066: for (Iterator i = included.iterator(); i.hasNext();) {
067: XSDInclude include = (XSDInclude) i.next();
068:
069: file = null;
070: try {
071: file = findSchemaFile(include.getSchemaLocation());
072: } catch (Exception e) {
073: logger.log(Level.SEVERE, "", e);
074: }
075:
076: if (file != null) {
077: includes.add(file);
078: if (include.getSchema() != null) {
079: namespaces.add(include.getSchema()
080: .getTargetNamespace());
081: } else {
082: namespaces.add(schema.getTargetNamespace());
083: }
084: } else {
085: logger.log(Level.SEVERE, "Could not find: "
086: + include.getSchemaLocation() + " to copy.");
087: }
088:
089: }
090:
091: try {
092: String result = execute("SchemaLocationResolverTemplate",
093: new Object[] { schema, includes, namespaces });
094: String prefix = Schemas.getTargetPrefix(schema)
095: .toUpperCase();
096: write(result, prefix + "SchemaLocationResolver");
097:
098: //copy over all the schemas
099: for (Iterator i = includes.iterator(); i.hasNext();) {
100: File include = (File) i.next();
101: copy(include);
102: }
103: } catch (Exception e) {
104: logger.log(Level.SEVERE, "Error generating resolver", e);
105: }
106: }
107:
108: public static void main(String[] args) throws Exception {
109: SchemaLocationResolverGenerator g = new SchemaLocationResolverGenerator();
110: XSDSchema schema = null;
111:
112: for (int i = 0; i < args.length; i++) {
113: String arg = args[i];
114:
115: if ("--schema".equals(arg)) {
116: schema = Schemas.parse(args[++i]);
117: } else if ("--output".equals(arg)) {
118: g.setLocation(args[++i]);
119: } else if ("--package".equals(arg)) {
120: g.setPackageBase(args[++i]);
121: }
122: }
123:
124: g.generate(schema);
125: }
126: }
|