001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020: package com.sun.xml.xsom.impl.parser;
021:
022: import com.sun.xml.xsom.XSAttGroupDecl;
023: import com.sun.xml.xsom.XSAttributeDecl;
024: import com.sun.xml.xsom.XSComplexType;
025: import com.sun.xml.xsom.XSDeclaration;
026: import com.sun.xml.xsom.XSElementDecl;
027: import com.sun.xml.xsom.XSIdentityConstraint;
028: import com.sun.xml.xsom.XSModelGroupDecl;
029: import com.sun.xml.xsom.XSSchemaSet;
030: import com.sun.xml.xsom.XSSimpleType;
031: import com.sun.xml.xsom.XSTerm;
032: import com.sun.xml.xsom.XSType;
033: import com.sun.xml.xsom.impl.Ref;
034: import com.sun.xml.xsom.impl.SchemaImpl;
035: import com.sun.xml.xsom.impl.UName;
036: import org.xml.sax.Locator;
037: import org.xml.sax.SAXException;
038:
039: /**
040: * Reference by name.
041: *
042: * UName will be later resolved to a target object,
043: * after all the schemas are parsed.
044: */
045: public abstract class DelayedRef implements Patch {
046: DelayedRef(PatcherManager _manager, Locator _source,
047: SchemaImpl _schema, UName _name) {
048:
049: this .schema = _schema.getRoot();
050: this .manager = _manager;
051: this .name = _name;
052: this .source = _source;
053:
054: if (name == null)
055: throw new InternalError();
056:
057: manager.addPatcher(this );
058: }
059:
060: /**
061: * Patch implementation. Makes sure that the name resolves
062: * to a schema component.
063: */
064: public void run() throws SAXException {
065: if (ref == null) // redefinition can set ref without actually resolving the reference
066: resolve();
067: manager = null; // avoid keeping the reference too long
068: name = null;
069: source = null;
070: }
071:
072: protected final XSSchemaSet schema;
073: private PatcherManager manager;
074: private UName name;
075: /** location in the source file where this reference was made. */
076: private Locator source;
077:
078: protected abstract Object resolveReference(UName name);
079:
080: protected abstract String getErrorProperty();
081:
082: private Object ref = null;
083:
084: protected final Object _get() {
085: if (ref == null)
086: throw new InternalError("unresolved reference");
087: return ref;
088: }
089:
090: private void resolve() throws SAXException {
091: ref = resolveReference(name);
092: if (ref == null)
093: manager.reportError(Messages.format(getErrorProperty(),
094: name.getQualifiedName()), source);
095: }
096:
097: /**
098: * If this reference refers to the given declaration,
099: * resolve the reference now. This is used to implement redefinition.
100: */
101: public void redefine(XSDeclaration d) {
102: if (!d.getTargetNamespace().equals(name.getNamespaceURI())
103: || !d.getName().equals(name.getName()))
104: return;
105:
106: ref = d;
107: manager = null;
108: name = null;
109: source = null;
110: }
111:
112: public static class Type extends DelayedRef implements Ref.Type {
113: public Type(PatcherManager manager, Locator loc,
114: SchemaImpl schema, UName name) {
115: super (manager, loc, schema, name);
116: }
117:
118: protected Object resolveReference(UName name) {
119: Object o = super .schema.getSimpleType(name
120: .getNamespaceURI(), name.getName());
121: if (o != null)
122: return o;
123:
124: return super .schema.getComplexType(name.getNamespaceURI(),
125: name.getName());
126: }
127:
128: protected String getErrorProperty() {
129: return Messages.ERR_UNDEFINED_TYPE;
130: }
131:
132: public XSType getType() {
133: return (XSType) super ._get();
134: }
135: }
136:
137: public static class SimpleType extends DelayedRef implements
138: Ref.SimpleType {
139: public SimpleType(PatcherManager manager, Locator loc,
140: SchemaImpl schema, UName name) {
141: super (manager, loc, schema, name);
142: }
143:
144: public XSSimpleType getType() {
145: return (XSSimpleType) _get();
146: }
147:
148: protected Object resolveReference(UName name) {
149: return super .schema.getSimpleType(name.getNamespaceURI(),
150: name.getName());
151: }
152:
153: protected String getErrorProperty() {
154: return Messages.ERR_UNDEFINED_SIMPLETYPE;
155: }
156: }
157:
158: public static class ComplexType extends DelayedRef implements
159: Ref.ComplexType {
160: public ComplexType(PatcherManager manager, Locator loc,
161: SchemaImpl schema, UName name) {
162: super (manager, loc, schema, name);
163: }
164:
165: protected Object resolveReference(UName name) {
166: return super .schema.getComplexType(name.getNamespaceURI(),
167: name.getName());
168: }
169:
170: protected String getErrorProperty() {
171: return Messages.ERR_UNDEFINED_COMPLEXTYPE;
172: }
173:
174: public XSComplexType getType() {
175: return (XSComplexType) super ._get();
176: }
177: }
178:
179: public static class Element extends DelayedRef implements
180: Ref.Element {
181: public Element(PatcherManager manager, Locator loc,
182: SchemaImpl schema, UName name) {
183: super (manager, loc, schema, name);
184: }
185:
186: protected Object resolveReference(UName name) {
187: return super .schema.getElementDecl(name.getNamespaceURI(),
188: name.getName());
189: }
190:
191: protected String getErrorProperty() {
192: return Messages.ERR_UNDEFINED_ELEMENT;
193: }
194:
195: public XSElementDecl get() {
196: return (XSElementDecl) super ._get();
197: }
198:
199: public XSTerm getTerm() {
200: return get();
201: }
202: }
203:
204: public static class ModelGroup extends DelayedRef implements
205: Ref.Term {
206: public ModelGroup(PatcherManager manager, Locator loc,
207: SchemaImpl schema, UName name) {
208: super (manager, loc, schema, name);
209: }
210:
211: protected Object resolveReference(UName name) {
212: return super .schema.getModelGroupDecl(name
213: .getNamespaceURI(), name.getName());
214: }
215:
216: protected String getErrorProperty() {
217: return Messages.ERR_UNDEFINED_MODELGROUP;
218: }
219:
220: public XSModelGroupDecl get() {
221: return (XSModelGroupDecl) super ._get();
222: }
223:
224: public XSTerm getTerm() {
225: return get();
226: }
227: }
228:
229: public static class AttGroup extends DelayedRef implements
230: Ref.AttGroup {
231: public AttGroup(PatcherManager manager, Locator loc,
232: SchemaImpl schema, UName name) {
233: super (manager, loc, schema, name);
234: }
235:
236: protected Object resolveReference(UName name) {
237: return super .schema.getAttGroupDecl(name.getNamespaceURI(),
238: name.getName());
239: }
240:
241: protected String getErrorProperty() {
242: return Messages.ERR_UNDEFINED_ATTRIBUTEGROUP;
243: }
244:
245: public XSAttGroupDecl get() {
246: return (XSAttGroupDecl) super ._get();
247: }
248: }
249:
250: public static class Attribute extends DelayedRef implements
251: Ref.Attribute {
252: public Attribute(PatcherManager manager, Locator loc,
253: SchemaImpl schema, UName name) {
254: super (manager, loc, schema, name);
255: }
256:
257: protected Object resolveReference(UName name) {
258: return super .schema.getAttributeDecl(
259: name.getNamespaceURI(), name.getName());
260: }
261:
262: protected String getErrorProperty() {
263: return Messages.ERR_UNDEFINED_ATTRIBUTE;
264: }
265:
266: public XSAttributeDecl getAttribute() {
267: return (XSAttributeDecl) super ._get();
268: }
269: }
270:
271: public static class IdentityConstraint extends DelayedRef implements
272: Ref.IdentityConstraint {
273: public IdentityConstraint(PatcherManager manager, Locator loc,
274: SchemaImpl schema, UName name) {
275: super (manager, loc, schema, name);
276: }
277:
278: protected Object resolveReference(UName name) {
279: return super .schema.getIdentityConstraint(name
280: .getNamespaceURI(), name.getName());
281: }
282:
283: protected String getErrorProperty() {
284: return Messages.ERR_UNDEFINED_IDENTITY_CONSTRAINT;
285: }
286:
287: public XSIdentityConstraint get() {
288: return (XSIdentityConstraint) super._get();
289: }
290: }
291: }
|