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;
021:
022: import com.sun.xml.xsom.SCD;
023: import com.sun.xml.xsom.XSAnnotation;
024: import com.sun.xml.xsom.XSComponent;
025: import com.sun.xml.xsom.XSSchemaSet;
026: import com.sun.xml.xsom.util.ComponentNameFunction;
027: import com.sun.xml.xsom.impl.parser.SchemaDocumentImpl;
028: import com.sun.xml.xsom.parser.SchemaDocument;
029: import org.xml.sax.Locator;
030: import org.xml.sax.helpers.LocatorImpl;
031:
032: import javax.xml.namespace.NamespaceContext;
033: import java.text.ParseException;
034: import java.util.ArrayList;
035: import java.util.Collection;
036: import java.util.Collections;
037: import java.util.List;
038:
039: public abstract class ComponentImpl implements XSComponent {
040: protected ComponentImpl(SchemaDocumentImpl _owner,
041: AnnotationImpl _annon, Locator _loc,
042: ForeignAttributesImpl fa) {
043: this .ownerDocument = _owner;
044: this .annotation = _annon;
045: this .locator = _loc;
046: this .foreignAttributes = fa;
047: }
048:
049: protected final SchemaDocumentImpl ownerDocument;
050:
051: public SchemaImpl getOwnerSchema() {
052: if (ownerDocument == null)
053: return null;
054: else
055: return ownerDocument.getSchema();
056: }
057:
058: public XSSchemaSet getRoot() {
059: if (ownerDocument == null)
060: return null;
061: else
062: return getOwnerSchema().getRoot();
063: }
064:
065: public SchemaDocument getSourceDocument() {
066: return ownerDocument;
067: }
068:
069: private AnnotationImpl annotation;
070:
071: public final XSAnnotation getAnnotation() {
072: return annotation;
073: }
074:
075: public XSAnnotation getAnnotation(boolean createIfNotExist) {
076: if (createIfNotExist && annotation == null) {
077: annotation = new AnnotationImpl();
078: }
079: return annotation;
080: }
081:
082: private final Locator locator;
083:
084: public final Locator getLocator() {
085: return locator;
086: }
087:
088: /**
089: * Either {@link ForeignAttributesImpl} or {@link List}.
090: *
091: * Initially it's {@link ForeignAttributesImpl}, but it's lazily turned into
092: * a list when necessary.
093: */
094: private Object foreignAttributes;
095:
096: public List<ForeignAttributesImpl> getForeignAttributes() {
097: Object t = foreignAttributes;
098:
099: if (t == null)
100: return Collections.EMPTY_LIST;
101:
102: if (t instanceof List)
103: return (List) t;
104:
105: t = foreignAttributes = convertToList((ForeignAttributesImpl) t);
106: return (List) t;
107: }
108:
109: public String getForeignAttribute(String nsUri, String localName) {
110: for (ForeignAttributesImpl fa : getForeignAttributes()) {
111: String v = fa.getValue(nsUri, localName);
112: if (v != null)
113: return v;
114: }
115: return null;
116: }
117:
118: private List<ForeignAttributesImpl> convertToList(
119: ForeignAttributesImpl fa) {
120: List<ForeignAttributesImpl> lst = new ArrayList<ForeignAttributesImpl>();
121: while (fa != null) {
122: lst.add(fa);
123: fa = fa.next;
124: }
125: return Collections.unmodifiableList(lst);
126: }
127:
128: public Collection<XSComponent> select(String scd,
129: NamespaceContext nsContext) {
130: try {
131: return SCD.create(scd, nsContext).select(this );
132: } catch (ParseException e) {
133: throw new IllegalArgumentException(e);
134: }
135: }
136:
137: public XSComponent selectSingle(String scd,
138: NamespaceContext nsContext) {
139: try {
140: return SCD.create(scd, nsContext).selectSingle(this );
141: } catch (ParseException e) {
142: throw new IllegalArgumentException(e);
143: }
144: }
145:
146: public String toString() {
147: return apply(new ComponentNameFunction());
148: }
149: }
|