001: /*
002: * Copyright (c) 1998-2007 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Emil Ong
027: */
028:
029: package com.caucho.xml.schema;
030:
031: import java.io.*;
032: import java.util.*;
033:
034: import static javax.xml.XMLConstants.*;
035:
036: import javax.xml.bind.*;
037: import javax.xml.bind.annotation.*;
038:
039: import javax.xml.namespace.QName;
040:
041: /**
042: * JAXB annotated Schema data structure.
043: */
044: @XmlAccessorType(XmlAccessType.FIELD)
045: @XmlRootElement(name="schema",namespace=W3C_XML_SCHEMA_NS_URI)
046: public class Schema {
047: @XmlElement(name="element",namespace=W3C_XML_SCHEMA_NS_URI)
048: private List<Element> _elements;
049:
050: @XmlElements({@XmlElement(name="complexType",namespace=W3C_XML_SCHEMA_NS_URI,type=com.caucho.xml.schema.ComplexType.class)})
051: private List<Type> _types;
052:
053: @XmlElement(name="import",namespace=W3C_XML_SCHEMA_NS_URI)
054: private List<Import> _imports;
055:
056: @XmlAttribute(name="targetNamespace")
057: private String _targetNamespace;
058:
059: @XmlAttribute(name="version")
060: private String _version;
061:
062: @XmlAttribute(name="elementFormDefault")
063: private String _elementFormDefault;
064:
065: public String getElementFormDefault() {
066: return _elementFormDefault;
067: }
068:
069: public String getTargetNamespace() {
070: return _targetNamespace;
071: }
072:
073: public String getVersion() {
074: return _version;
075: }
076:
077: public void afterUnmarshal(Unmarshaller u, Object parent) {
078: if (_types != null) {
079: for (Type type : _types)
080: type.setSchema(this );
081: }
082:
083: if (_elements != null) {
084: for (Element element : _elements)
085: element.setSchema(this );
086: }
087: }
088:
089: public void resolveImports(Unmarshaller u) throws JAXBException {
090: if (_imports != null) {
091: for (int i = 0; i < _imports.size(); i++) {
092: Import imp = _imports.get(i);
093: imp.resolve(u);
094:
095: if (imp.getSchema() != null)
096: imp.getSchema().resolveImports(u);
097: }
098: }
099: }
100:
101: public void writeJAXBClasses(File outputDirectory, String pkg)
102: throws IOException {
103: if (_types != null) {
104: for (Type type : _types)
105: type.writeJava(outputDirectory, pkg);
106: }
107:
108: if (_imports != null) {
109: for (int i = 0; i < _imports.size(); i++) {
110: Import imp = _imports.get(i);
111:
112: if (imp.getSchema() != null)
113: imp.getSchema().writeJAXBClasses(outputDirectory,
114: pkg);
115: }
116: }
117:
118: // XXX Elements -> ObjectFactory
119: }
120:
121: public Type getType(QName typeName) {
122: if (typeName.getNamespaceURI() == null
123: || typeName.getNamespaceURI().equals(_targetNamespace)) {
124: // look at the immediate children types
125: if (_types != null) {
126: for (int i = 0; i < _types.size(); i++) {
127: Type type = _types.get(i);
128:
129: if (type.getName().equals(typeName.getLocalPart()))
130: return type;
131: }
132: }
133:
134: if (_imports != null) {
135: for (int i = 0; i < _imports.size(); i++) {
136: Import imp = _imports.get(i);
137: Schema schema = imp.getSchema();
138:
139: if (schema != null
140: && (schema.getTargetNamespace() == null || schema
141: .getTargetNamespace().equals(
142: getTargetNamespace()))) {
143: Type type = schema.getType(typeName);
144:
145: if (type != null)
146: return type;
147: }
148: }
149: }
150: } else {
151: // look for the children in imported/included schema
152: if (_imports != null) {
153: for (int i = 0; i < _imports.size(); i++) {
154: Import imp = _imports.get(i);
155: Schema schema = imp.getSchema();
156:
157: if (schema != null
158: && schema.getTargetNamespace().equals(
159: typeName.getNamespaceURI())) {
160: Type type = schema.getType(typeName);
161:
162: if (type != null)
163: return type;
164: }
165: }
166: }
167: }
168:
169: return null;
170: }
171: }
|