1/*
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.xml.internal.xsom.impl.parser;
27
28import com.sun.xml.internal.xsom.impl.SchemaImpl;
29import com.sun.xml.internal.xsom.parser.SchemaDocument;
30
31import java.util.Collections;
32import java.util.HashSet;
33import java.util.Set;
34
35/**
36 * {@link SchemaDocument} implementation.
37 *
38 * @author Kohsuke Kawaguchi
39 */
40public final class SchemaDocumentImpl implements SchemaDocument
41{
42    private final SchemaImpl schema;
43
44    /**
45     * URI of the schema document to be parsed. Can be null.
46     */
47    private final String schemaDocumentURI;
48
49    /**
50     * {@link SchemaDocumentImpl}s that are referenced from this document.
51     */
52    final Set<SchemaDocumentImpl> references = new HashSet<SchemaDocumentImpl>();
53
54    /**
55     * {@link SchemaDocumentImpl}s that are referencing this document.
56     */
57    final Set<SchemaDocumentImpl> referers = new HashSet<SchemaDocumentImpl>();
58
59    protected SchemaDocumentImpl(SchemaImpl schema, String _schemaDocumentURI) {
60        this.schema = schema;
61        this.schemaDocumentURI = _schemaDocumentURI;
62    }
63
64    public String getSystemId() {
65        return schemaDocumentURI;
66    }
67
68    public String getTargetNamespace() {
69        return schema.getTargetNamespace();
70    }
71
72    public SchemaImpl getSchema() {
73        return schema;
74    }
75
76    public Set<SchemaDocument> getReferencedDocuments() {
77        return Collections.<SchemaDocument>unmodifiableSet(references);
78    }
79
80    public Set<SchemaDocument> getIncludedDocuments() {
81        return getImportedDocuments(this.getTargetNamespace());
82    }
83
84    public Set<SchemaDocument> getImportedDocuments(String targetNamespace) {
85        if(targetNamespace==null)
86            throw new IllegalArgumentException();
87        Set<SchemaDocument> r = new HashSet<SchemaDocument>();
88        for (SchemaDocumentImpl doc : references) {
89            if(doc.getTargetNamespace().equals(targetNamespace))
90                r.add(doc);
91        }
92        return Collections.unmodifiableSet(r);
93    }
94
95    public boolean includes(SchemaDocument doc) {
96        if(!references.contains(doc))
97            return false;
98        return doc.getSchema()==schema;
99    }
100
101    public boolean imports(SchemaDocument doc) {
102        if(!references.contains(doc))
103            return false;
104        return doc.getSchema()!=schema;
105    }
106
107    public Set<SchemaDocument> getReferers() {
108        return Collections.<SchemaDocument>unmodifiableSet(referers);
109    }
110
111    @Override
112    public boolean equals(Object o) {
113        SchemaDocumentImpl rhs = (SchemaDocumentImpl) o;
114
115        if( this.schemaDocumentURI==null || rhs.schemaDocumentURI==null)
116            return this==rhs;
117        if(!schemaDocumentURI.equals(rhs.schemaDocumentURI) )
118            return false;
119        return this.schema==rhs.schema;
120    }
121
122    @Override
123    public int hashCode() {
124        if (schemaDocumentURI==null)
125            return super.hashCode();
126        if (schema == null) {
127            return schemaDocumentURI.hashCode();
128        }
129        return schemaDocumentURI.hashCode()^this.schema.hashCode();
130    }
131}
132