001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: package com.sun.xml.internal.xsom.impl.parser;
026:
027: import com.sun.xml.internal.xsom.XSAttGroupDecl;
028: import com.sun.xml.internal.xsom.XSAttributeDecl;
029: import com.sun.xml.internal.xsom.XSComplexType;
030: import com.sun.xml.internal.xsom.XSDeclaration;
031: import com.sun.xml.internal.xsom.XSElementDecl;
032: import com.sun.xml.internal.xsom.XSIdentityConstraint;
033: import com.sun.xml.internal.xsom.XSModelGroupDecl;
034: import com.sun.xml.internal.xsom.XSSchemaSet;
035: import com.sun.xml.internal.xsom.XSSimpleType;
036: import com.sun.xml.internal.xsom.XSTerm;
037: import com.sun.xml.internal.xsom.XSType;
038: import com.sun.xml.internal.xsom.impl.Ref;
039: import com.sun.xml.internal.xsom.impl.SchemaImpl;
040: import com.sun.xml.internal.xsom.impl.UName;
041: import org.xml.sax.Locator;
042: import org.xml.sax.SAXException;
043:
044: /**
045: * Reference by name.
046: *
047: * UName will be later resolved to a target object,
048: * after all the schemas are parsed.
049: */
050: public abstract class DelayedRef implements Patch {
051: DelayedRef(PatcherManager _manager, Locator _source,
052: SchemaImpl _schema, UName _name) {
053:
054: this .schema = _schema.getParent();
055: this .manager = _manager;
056: this .name = _name;
057: this .source = _source;
058:
059: if (name == null)
060: throw new InternalError();
061:
062: manager.addPatcher(this );
063: }
064:
065: /**
066: * Patch implementation. Makes sure that the name resolves
067: * to a schema component.
068: */
069: public void run() throws SAXException {
070: if (ref == null) // redefinition can set ref without actually resolving the reference
071: resolve();
072: manager = null; // avoid keeping the reference too long
073: name = null;
074: source = null;
075: }
076:
077: protected final XSSchemaSet schema;
078: private PatcherManager manager;
079: private UName name;
080: /** location in the source file where this reference was made. */
081: private Locator source;
082:
083: protected abstract Object resolveReference(UName name);
084:
085: protected abstract String getErrorProperty();
086:
087: private Object ref = null;
088:
089: protected final Object _get() {
090: if (ref == null)
091: throw new InternalError("unresolved reference");
092: return ref;
093: }
094:
095: private void resolve() throws SAXException {
096: ref = resolveReference(name);
097: if (ref == null)
098: manager.reportError(Messages.format(getErrorProperty(),
099: name.getQualifiedName()), source);
100: }
101:
102: /**
103: * If this reference refers to the given declaration,
104: * resolve the reference now. This is used to implement redefinition.
105: */
106: public void redefine(XSDeclaration d) {
107: if (!d.getTargetNamespace().equals(name.getNamespaceURI())
108: || !d.getName().equals(name.getName()))
109: return;
110:
111: ref = d;
112: manager = null;
113: name = null;
114: source = null;
115: }
116:
117: public static class Type extends DelayedRef implements Ref.Type {
118: public Type(PatcherManager manager, Locator loc,
119: SchemaImpl schema, UName name) {
120: super (manager, loc, schema, name);
121: }
122:
123: protected Object resolveReference(UName name) {
124: Object o = super .schema.getSimpleType(name
125: .getNamespaceURI(), name.getName());
126: if (o != null)
127: return o;
128:
129: return super .schema.getComplexType(name.getNamespaceURI(),
130: name.getName());
131: }
132:
133: protected String getErrorProperty() {
134: return Messages.ERR_UNDEFINED_TYPE;
135: }
136:
137: public XSType getType() {
138: return (XSType) super ._get();
139: }
140: }
141:
142: public static class SimpleType extends DelayedRef implements
143: Ref.SimpleType {
144: public SimpleType(PatcherManager manager, Locator loc,
145: SchemaImpl schema, UName name) {
146: super (manager, loc, schema, name);
147: }
148:
149: public XSSimpleType getType() {
150: return (XSSimpleType) _get();
151: }
152:
153: protected Object resolveReference(UName name) {
154: return super .schema.getSimpleType(name.getNamespaceURI(),
155: name.getName());
156: }
157:
158: protected String getErrorProperty() {
159: return Messages.ERR_UNDEFINED_SIMPLETYPE;
160: }
161: }
162:
163: public static class ComplexType extends DelayedRef implements
164: Ref.ComplexType {
165: public ComplexType(PatcherManager manager, Locator loc,
166: SchemaImpl schema, UName name) {
167: super (manager, loc, schema, name);
168: }
169:
170: protected Object resolveReference(UName name) {
171: return super .schema.getComplexType(name.getNamespaceURI(),
172: name.getName());
173: }
174:
175: protected String getErrorProperty() {
176: return Messages.ERR_UNDEFINED_COMPLEXTYPE;
177: }
178:
179: public XSComplexType getType() {
180: return (XSComplexType) super ._get();
181: }
182: }
183:
184: public static class Element extends DelayedRef implements
185: Ref.Element {
186: public Element(PatcherManager manager, Locator loc,
187: SchemaImpl schema, UName name) {
188: super (manager, loc, schema, name);
189: }
190:
191: protected Object resolveReference(UName name) {
192: return super .schema.getElementDecl(name.getNamespaceURI(),
193: name.getName());
194: }
195:
196: protected String getErrorProperty() {
197: return Messages.ERR_UNDEFINED_ELEMENT;
198: }
199:
200: public XSElementDecl get() {
201: return (XSElementDecl) super ._get();
202: }
203:
204: public XSTerm getTerm() {
205: return get();
206: }
207: }
208:
209: public static class ModelGroup extends DelayedRef implements
210: Ref.Term {
211: public ModelGroup(PatcherManager manager, Locator loc,
212: SchemaImpl schema, UName name) {
213: super (manager, loc, schema, name);
214: }
215:
216: protected Object resolveReference(UName name) {
217: return super .schema.getModelGroupDecl(name
218: .getNamespaceURI(), name.getName());
219: }
220:
221: protected String getErrorProperty() {
222: return Messages.ERR_UNDEFINED_MODELGROUP;
223: }
224:
225: public XSModelGroupDecl get() {
226: return (XSModelGroupDecl) super ._get();
227: }
228:
229: public XSTerm getTerm() {
230: return get();
231: }
232: }
233:
234: public static class AttGroup extends DelayedRef implements
235: Ref.AttGroup {
236: public AttGroup(PatcherManager manager, Locator loc,
237: SchemaImpl schema, UName name) {
238: super (manager, loc, schema, name);
239: }
240:
241: protected Object resolveReference(UName name) {
242: return super .schema.getAttGroupDecl(name.getNamespaceURI(),
243: name.getName());
244: }
245:
246: protected String getErrorProperty() {
247: return Messages.ERR_UNDEFINED_ATTRIBUTEGROUP;
248: }
249:
250: public XSAttGroupDecl get() {
251: return (XSAttGroupDecl) super ._get();
252: }
253: }
254:
255: public static class Attribute extends DelayedRef implements
256: Ref.Attribute {
257: public Attribute(PatcherManager manager, Locator loc,
258: SchemaImpl schema, UName name) {
259: super (manager, loc, schema, name);
260: }
261:
262: protected Object resolveReference(UName name) {
263: return super .schema.getAttributeDecl(
264: name.getNamespaceURI(), name.getName());
265: }
266:
267: protected String getErrorProperty() {
268: return Messages.ERR_UNDEFINED_ATTRIBUTE;
269: }
270:
271: public XSAttributeDecl getAttribute() {
272: return (XSAttributeDecl) super ._get();
273: }
274: }
275:
276: public static class IdentityConstraint extends DelayedRef implements
277: Ref.IdentityConstraint {
278: public IdentityConstraint(PatcherManager manager, Locator loc,
279: SchemaImpl schema, UName name) {
280: super (manager, loc, schema, name);
281: }
282:
283: protected Object resolveReference(UName name) {
284: return super .schema.getIdentityConstraint(name
285: .getNamespaceURI(), name.getName());
286: }
287:
288: protected String getErrorProperty() {
289: return Messages.ERR_UNDEFINED_IDENTITY_CONSTRAINT;
290: }
291:
292: public XSIdentityConstraint get() {
293: return (XSIdentityConstraint) super._get();
294: }
295: }
296: }
|