1 /*
   2  * Copyright (c) 2005, 2010, 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 
  26 package com.sun.xml.internal.rngom.digested;
  27 
  28 import org.xml.sax.Locator;
  29 import org.w3c.dom.Element;
  30 
  31 import javax.xml.namespace.QName;
  32 import java.util.Map;
  33 import java.util.HashMap;
  34 import java.util.List;
  35 import java.util.ArrayList;
  36 import java.util.Collections;
  37 
  38 /**
  39  * Annotation.
  40  *
  41  * @author Kohsuke Kawaguchi (kk@kohsuke.org)
  42  */
  43 public class DAnnotation {
  44 
  45     /**
  46      * Instance reserved to be empty.
  47      */
  48     static final DAnnotation EMPTY = new DAnnotation();
  49 
  50     /**
  51      * Keyed by QName.
  52      */
  53     final Map<QName,Attribute> attributes = new HashMap<QName,Attribute>();
  54 
  55     /**
  56      * List of nested elements.
  57      */
  58     final List<Element> contents = new ArrayList<Element>();
  59 
  60     /**
  61      * Attribute.
  62      */
  63     public static class Attribute {
  64         private final String ns;
  65         private final String localName;
  66         private final String prefix;
  67 
  68         private String value;
  69         private Locator loc;
  70 
  71         public Attribute(String ns, String localName, String prefix) {
  72             this.ns = ns;
  73             this.localName = localName;
  74             this.prefix = prefix;
  75         }
  76 
  77         public Attribute(String ns, String localName, String prefix, String value, Locator loc) {
  78             this.ns = ns;
  79             this.localName = localName;
  80             this.prefix = prefix;
  81             this.value = value;
  82             this.loc = loc;
  83         }
  84 
  85         /**
  86          * Gets the namespace URI of this attribute.
  87          *
  88          * @return
  89          *      can be empty (to represent the default namespace), but never null.
  90          */
  91         public String getNs() {
  92             return ns;
  93         }
  94 
  95         /**
  96          * Gets the local name of this attribute.
  97          *
  98          * @return
  99          *      always non-null.
 100          */
 101         public String getLocalName() {
 102             return localName;
 103         }
 104 
 105         /**
 106          * Gets the prefix of thie attribute.
 107          *
 108          * @return
 109          *      null if this attribute didn't have a prefix.
 110          */
 111         public String getPrefix() {
 112             return prefix;
 113         }
 114 
 115         /**
 116          * Gets the attribute value.
 117          *
 118          * @return
 119          *      never null.
 120          */
 121         public String getValue() {
 122             return value;
 123         }
 124 
 125         /**
 126          * Gets the location in the source schema file where this annotation was present.
 127          *
 128          * @return
 129          *      never null.
 130          */
 131         public Locator getLoc() {
 132             return loc;
 133         }
 134     }
 135 
 136     /**
 137      * Gets the attribute of a given name.
 138      *
 139      * @param nsUri
 140      *      can be empty but must not be null.
 141      * @return
 142      *      null if no such attribute is found.
 143      */
 144     public Attribute getAttribute( String nsUri, String localName ) {
 145         return getAttribute(new QName(nsUri,localName));
 146     }
 147 
 148     public Attribute getAttribute( QName n ) {
 149         return attributes.get(n);
 150     }
 151 
 152     /**
 153      * Gets the read-only view of all the attributes.
 154      *
 155      * @return
 156      *      can be empty but never null.
 157      *      the returned map is read-only.
 158      */
 159     public Map<QName,Attribute> getAttributes() {
 160         return Collections.unmodifiableMap(attributes);
 161     }
 162 
 163     /**
 164      * Gets the read-only view of all the child elements of this annotation.
 165      *
 166      * @return
 167      *      can be empty but never null.
 168      *      the returned list is read-only.
 169      */
 170     public List<Element> getChildren() {
 171         return Collections.unmodifiableList(contents);
 172     }
 173 }