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;
026:
027: import com.sun.xml.internal.xsom.XSAnnotation;
028: import com.sun.xml.internal.xsom.XSComponent;
029: import com.sun.xml.internal.xsom.impl.parser.SchemaDocumentImpl;
030: import com.sun.xml.internal.xsom.parser.SchemaDocument;
031: import org.xml.sax.Locator;
032:
033: import java.util.ArrayList;
034: import java.util.Collections;
035: import java.util.List;
036:
037: public abstract class ComponentImpl implements XSComponent {
038: protected ComponentImpl(SchemaDocumentImpl _owner,
039: AnnotationImpl _annon, Locator _loc,
040: ForeignAttributesImpl fa) {
041: this .ownerDocument = _owner;
042: this .annotation = _annon;
043: this .locator = _loc;
044: this .foreignAttributes = fa;
045: }
046:
047: protected final SchemaDocumentImpl ownerDocument;
048:
049: public SchemaImpl getOwnerSchema() {
050: if (ownerDocument == null)
051: return null;
052: else
053: return ownerDocument.getSchema();
054: }
055:
056: public SchemaDocument getSourceDocument() {
057: return ownerDocument;
058: }
059:
060: private final AnnotationImpl annotation;
061:
062: public final XSAnnotation getAnnotation() {
063: return annotation;
064: }
065:
066: private final Locator locator;
067:
068: public final Locator getLocator() {
069: return locator;
070: }
071:
072: /**
073: * Either {@link ForeignAttributesImpl} or {@link List}.
074: *
075: * Initially it's {@link ForeignAttributesImpl}, but it's lazily turned into
076: * a list when necessary.
077: */
078: private Object foreignAttributes;
079:
080: public List getForeignAttributes() {
081: Object t = foreignAttributes;
082:
083: if (t == null)
084: return Collections.EMPTY_LIST;
085:
086: if (t instanceof List)
087: return (List) t;
088:
089: t = foreignAttributes = convertToList((ForeignAttributesImpl) t);
090: return (List) t;
091: }
092:
093: public String getForeignAttribute(String nsUri, String localName) {
094: for (ForeignAttributesImpl fa : (List<ForeignAttributesImpl>) getForeignAttributes()) {
095: String v = fa.getValue(nsUri, localName);
096: if (v != null)
097: return v;
098: }
099: return null;
100: }
101:
102: private List convertToList(ForeignAttributesImpl fa) {
103: List lst = new ArrayList();
104: while (fa != null) {
105: lst.add(fa);
106: fa = fa.next;
107: }
108: return Collections.unmodifiableList(lst);
109: }
110: }
|