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;
27
28import com.sun.xml.internal.xsom.XSAttGroupDecl;
29import com.sun.xml.internal.xsom.XSAttributeUse;
30import com.sun.xml.internal.xsom.impl.parser.SchemaDocumentImpl;
31import com.sun.xml.internal.xsom.impl.scd.Iterators;
32import com.sun.xml.internal.xsom.impl.Ref.AttGroup;
33import org.xml.sax.Locator;
34
35import java.util.AbstractSet;
36import java.util.ArrayList;
37import java.util.Collection;
38import java.util.HashSet;
39import java.util.Iterator;
40import java.util.List;
41import java.util.Map;
42import java.util.Set;
43import java.util.TreeMap;
44import java.util.LinkedHashMap;
45
46public abstract class AttributesHolder extends DeclarationImpl {
47
48    protected AttributesHolder( SchemaDocumentImpl _parent, AnnotationImpl _annon,
49                                Locator loc, ForeignAttributesImpl _fa, String _name, boolean _anonymous ) {
50
51        super(_parent,_annon,loc,_fa,_parent.getTargetNamespace(),_name,_anonymous);
52    }
53
54    /** set the local wildcard. */
55    public abstract void setWildcard(WildcardImpl wc);
56
57    /**
58     * Local attribute use.
59     * Use linked hash map to guarantee the iteration order, and make it close to
60     * what was in the schema document.
61     */
62    protected final Map<UName,AttributeUseImpl> attributes = new LinkedHashMap<UName,AttributeUseImpl>();
63    public void addAttributeUse( UName name, AttributeUseImpl a ) {
64        attributes.put( name, a );
65    }
66    /** prohibited attributes. */
67    protected final Set<UName> prohibitedAtts = new HashSet<UName>();
68    public void addProhibitedAttribute( UName name ) {
69        prohibitedAtts.add(name);
70    }
71
72    /**
73     * Returns the attribute uses by looking at attribute groups and etc.
74     * Searching for the base type is done in {@link ComplexTypeImpl}.
75     */
76    public Collection<XSAttributeUse> getAttributeUses() {
77        // TODO: this is fairly inefficient
78        List<XSAttributeUse> v = new ArrayList<XSAttributeUse>();
79        v.addAll(attributes.values());
80        for( XSAttGroupDecl agd : getAttGroups() )
81            v.addAll(agd.getAttributeUses());
82        return v;
83    }
84    public Iterator<XSAttributeUse> iterateAttributeUses() {
85        return getAttributeUses().iterator();
86    }
87
88
89
90    public XSAttributeUse getDeclaredAttributeUse( String nsURI, String localName ) {
91        return attributes.get(new UName(nsURI,localName));
92    }
93
94    public Iterator<AttributeUseImpl> iterateDeclaredAttributeUses() {
95        return attributes.values().iterator();
96    }
97
98    public Collection<AttributeUseImpl> getDeclaredAttributeUses() {
99        return attributes.values();
100    }
101
102
103    /** {@link Ref.AttGroup}s that are directly refered from this. */
104    protected final Set<Ref.AttGroup> attGroups = new HashSet<Ref.AttGroup>();
105
106    public void addAttGroup( Ref.AttGroup a ) { attGroups.add(a); }
107
108    // Iterates all AttGroups which are directly referenced from this component
109    // this does not iterate att groups referenced from the base type
110    public Iterator<XSAttGroupDecl> iterateAttGroups() {
111        return new Iterators.Adapter<XSAttGroupDecl,Ref.AttGroup>(attGroups.iterator()) {
112            protected XSAttGroupDecl filter(AttGroup u) {
113                return u.get();
114            }
115        };
116    }
117
118    public Set<XSAttGroupDecl> getAttGroups() {
119        return new AbstractSet<XSAttGroupDecl>() {
120            public Iterator<XSAttGroupDecl> iterator() {
121                return iterateAttGroups();
122            }
123
124            public int size() {
125                return attGroups.size();
126            }
127        };
128    }
129}
130