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:
026: package com.sun.xml.internal.xsom.impl.parser;
027:
028: import com.sun.xml.internal.xsom.impl.SchemaImpl;
029: import com.sun.xml.internal.xsom.parser.SchemaDocument;
030:
031: import java.util.Collections;
032: import java.util.HashSet;
033: import java.util.Set;
034:
035: /**
036: * {@link SchemaDocument} implementation.
037: *
038: * @author Kohsuke Kawaguchi
039: */
040: public final class SchemaDocumentImpl implements SchemaDocument {
041: private final SchemaImpl schema;
042:
043: /**
044: * URI of the schema document to be parsed. Can be null.
045: */
046: private final String schemaDocumentURI;
047:
048: /**
049: * {@link SchemaDocumentImpl}s that are referenced from this document.
050: */
051: final Set<SchemaDocumentImpl> references = new HashSet<SchemaDocumentImpl>();
052:
053: /**
054: * {@link SchemaDocumentImpl}s that are referencing this document.
055: */
056: final Set<SchemaDocumentImpl> referers = new HashSet<SchemaDocumentImpl>();
057:
058: protected SchemaDocumentImpl(SchemaImpl schema,
059: String _schemaDocumentURI) {
060: this .schema = schema;
061: this .schemaDocumentURI = _schemaDocumentURI;
062: }
063:
064: public String getSystemId() {
065: return schemaDocumentURI;
066: }
067:
068: public String getTargetNamespace() {
069: return schema.getTargetNamespace();
070: }
071:
072: public SchemaImpl getSchema() {
073: return schema;
074: }
075:
076: public Set<SchemaDocument> getReferencedDocuments() {
077: return Collections.<SchemaDocument> unmodifiableSet(references);
078: }
079:
080: public Set<SchemaDocument> getIncludedDocuments() {
081: return getImportedDocuments(this .getTargetNamespace());
082: }
083:
084: public Set<SchemaDocument> getImportedDocuments(
085: String targetNamespace) {
086: if (targetNamespace == null)
087: throw new IllegalArgumentException();
088: Set<SchemaDocument> r = new HashSet<SchemaDocument>();
089: for (SchemaDocumentImpl doc : references) {
090: if (doc.getTargetNamespace().equals(targetNamespace))
091: r.add(doc);
092: }
093: return Collections.unmodifiableSet(r);
094: }
095:
096: public boolean includes(SchemaDocument doc) {
097: if (!references.contains(doc))
098: return false;
099: return doc.getSchema() == schema;
100: }
101:
102: public boolean imports(SchemaDocument doc) {
103: if (!references.contains(doc))
104: return false;
105: return doc.getSchema() != schema;
106: }
107:
108: public Set<SchemaDocument> getReferers() {
109: return Collections.<SchemaDocument> unmodifiableSet(referers);
110: }
111:
112: public boolean equals(Object o) {
113: SchemaDocumentImpl rhs = (SchemaDocumentImpl) o;
114:
115: if (this .schemaDocumentURI == null
116: || rhs.schemaDocumentURI == null)
117: return this == rhs;
118: if (!schemaDocumentURI.equals(rhs.schemaDocumentURI))
119: return false;
120: return this .schema == rhs.schema;
121: }
122:
123: public int hashCode() {
124: if (schemaDocumentURI == null)
125: return super.hashCode();
126: return schemaDocumentURI.hashCode() ^ this.schema.hashCode();
127: }
128: }
|