001: package org.kohsuke.rngom.digested;
002:
003: import org.xml.sax.Locator;
004: import org.w3c.dom.Element;
005:
006: import javax.xml.namespace.QName;
007: import java.util.Map;
008: import java.util.HashMap;
009: import java.util.List;
010: import java.util.ArrayList;
011: import java.util.Collections;
012:
013: /**
014: * Annotation.
015: *
016: * @author Kohsuke Kawaguchi (kk@kohsuke.org)
017: */
018: public class DAnnotation {
019:
020: /**
021: * Instance reserved to be empty.
022: */
023: static final DAnnotation EMPTY = new DAnnotation();
024:
025: /**
026: * Keyed by QName.
027: */
028: final Map<QName, Attribute> attributes = new HashMap<QName, Attribute>();
029:
030: /**
031: * List of nested elements.
032: */
033: final List<Element> contents = new ArrayList<Element>();
034:
035: /**
036: * Attribute.
037: */
038: public static class Attribute {
039: private final String ns;
040: private final String localName;
041: private final String prefix;
042:
043: private String value;
044: private Locator loc;
045:
046: public Attribute(String ns, String localName, String prefix) {
047: this .ns = ns;
048: this .localName = localName;
049: this .prefix = prefix;
050: }
051:
052: public Attribute(String ns, String localName, String prefix,
053: String value, Locator loc) {
054: this .ns = ns;
055: this .localName = localName;
056: this .prefix = prefix;
057: this .value = value;
058: this .loc = loc;
059: }
060:
061: /**
062: * Gets the namespace URI of this attribute.
063: *
064: * @return
065: * can be empty (to represent the default namespace), but never null.
066: */
067: public String getNs() {
068: return ns;
069: }
070:
071: /**
072: * Gets the local name of this attribute.
073: *
074: * @return
075: * always non-null.
076: */
077: public String getLocalName() {
078: return localName;
079: }
080:
081: /**
082: * Gets the prefix of thie attribute.
083: *
084: * @return
085: * null if this attribute didn't have a prefix.
086: */
087: public String getPrefix() {
088: return prefix;
089: }
090:
091: /**
092: * Gets the attribute value.
093: *
094: * @return
095: * never null.
096: */
097: public String getValue() {
098: return value;
099: }
100:
101: /**
102: * Gets the location in the source schema file where this annotation was present.
103: *
104: * @return
105: * never null.
106: */
107: public Locator getLoc() {
108: return loc;
109: }
110: }
111:
112: /**
113: * Gets the attribute of a given name.
114: *
115: * @param nsUri
116: * can be empty but must not be null.
117: * @return
118: * null if no such attribute is found.
119: */
120: public Attribute getAttribute(String nsUri, String localName) {
121: return getAttribute(new QName(nsUri, localName));
122: }
123:
124: public Attribute getAttribute(QName n) {
125: return attributes.get(n);
126: }
127:
128: /**
129: * Gets the read-only view of all the attributes.
130: *
131: * @return
132: * can be empty but never null.
133: * the returned map is read-only.
134: */
135: public Map<QName, Attribute> getAttributes() {
136: return Collections.unmodifiableMap(attributes);
137: }
138:
139: /**
140: * Gets the read-only view of all the child elements of this annotation.
141: *
142: * @return
143: * can be empty but never null.
144: * the returned list is read-only.
145: */
146: public List<Element> getChildren() {
147: return Collections.unmodifiableList(contents);
148: }
149: }
|