Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/sun/xacml/attr/StringAttribute.java


1   
2   /*
3    * @(#)StringAttribute.java
4    *
5    * Copyright 2003-2004 Sun Microsystems, Inc. All Rights Reserved.
6    *
7    * Redistribution and use in source and binary forms, with or without
8    * modification, are permitted provided that the following conditions are met:
9    *
10   *   1. Redistribution of source code must retain the above copyright notice,
11   *      this list of conditions and the following disclaimer.
12   * 
13   *   2. Redistribution in binary form must reproduce the above copyright
14   *      notice, this list of conditions and the following disclaimer in the
15   *      documentation and/or other materials provided with the distribution.
16   *
17   * Neither the name of Sun Microsystems, Inc. or the names of contributors may
18   * be used to endorse or promote products derived from this software without
19   * specific prior written permission.
20   * 
21   * This software is provided "AS IS," without a warranty of any kind. ALL
22   * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
23   * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
24   * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN")
25   * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
26   * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
27   * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
28   * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
29   * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
30   * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
31   * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32   *
33   * You acknowledge that this software is not designed or intended for use in
34   * the design, construction, operation or maintenance of any nuclear facility.
35   */
36  
37  package com.sun.xacml.attr;
38  
39  import java.net.URI;
40  
41  import org.w3c.dom.Node;
42  import org.w3c.dom.NodeList;
43  
44  
45  /**
46   * Representation of an xs:string value. This class supports parsing
47   * xs:string values. All objects of this class are immutable and
48   * all methods of the class are thread-safe.
49   * <p>
50   * Note that there is currently some confusion in the XACML specification
51   * about whether this datatype should be able to handle XML elements (ie,
52   * whether &lt;AttributeValue DataType="...string"&gt;&lt;foo/&gt;
53   * &lt;/AttributeValue&gt; is valid). Until that is clarified the strict
54   * definition of the string datatype is used in this code, which means that
55   * elements are not valid.
56   *
57   * @since 1.0
58   * @author Marco Barreno
59   * @author Seth Proctor
60   * @author Steve Hanna
61   */
62  public class StringAttribute extends AttributeValue
63  {
64      /**
65       * Official name of this type
66       */
67      public static final String identifier =
68          "http://www.w3.org/2001/XMLSchema#string";
69   
70      /**
71       * URI version of name for this type
72       * <p>
73       * This field is initialized by a static initializer so that
74       * we can catch any exceptions thrown by URI(String) and
75       * transform them into a RuntimeException, since this should
76       * never happen but should be reported properly if it ever does.
77       */
78      private static URI identifierURI;
79  
80      /**
81       * RuntimeException that wraps an Exception thrown during the
82       * creation of identifierURI, null if none.
83       */
84      private static RuntimeException earlyException;
85  
86      /**
87       * Static initializer that initializes the identifierURI
88       * class field so that we can catch any exceptions thrown
89       * by URI(String) and transform them into a RuntimeException.
90       * Such exceptions should never happen but should be reported
91       * properly if they ever do.
92       */
93      static {
94          try {
95              identifierURI = new URI(identifier);
96          } catch (Exception e) {
97              earlyException = new IllegalArgumentException();
98              earlyException.initCause(e);
99          }
100     };
101  
102     /**
103      * The actual String value that this object represents.
104      */
105     private String value;
106 
107     /**
108      * Creates a new <code>StringAttribute</code> that represents
109      * the String value supplied.
110      *
111      * @param value the <code>String</code> value to be represented
112      */
113     public StringAttribute(String value) {
114         super(identifierURI);
115 
116         // Shouldn't happen, but just in case...
117         if (earlyException != null)
118             throw earlyException;
119 
120         this.value = value;
121     }
122 
123     /**
124      * Returns a new <code>StringAttribute</code> that represents
125      * the xs:string at a particular DOM node.
126      *
127      * @param root the <code>Node</code> that contains the desired value
128      * @return a new <code>StringAttribute</code> representing the
129      *         appropriate value (null if there is a parsing error)
130      */
131     public static StringAttribute getInstance(Node root) {
132         Node node = root.getFirstChild();
133 
134         // Strings are allowed to have an empty AttributeValue element and are
135         // just treated as empty strings...we have to handle this case
136         if (node == null)
137             return new StringAttribute("");
138 
139         // get the type of the node
140         short type = node.getNodeType();
141 
142         // now see if we have (effectively) a simple string value
143         if ((type == Node.TEXT_NODE) || (type == Node.CDATA_SECTION_NODE) ||
144             (type == Node.COMMENT_NODE)) {
145             return getInstance(node.getNodeValue());
146         }
147 
148         // there is some confusion in the specifications about what should
149         // happen at this point, but the strict reading of the XMLSchema
150         // specification suggests that this should be an error
151         return null;
152     }
153 
154     /**
155      * Returns a new <code>StringAttribute</code> that represents
156      * the xs:string value indicated by the <code>String</code> provided.
157      *
158      * @param value a string representing the desired value
159      * @return a new <code>StringAttribute</code> representing the
160      *         appropriate value
161      */
162     public static StringAttribute getInstance(String value) {
163         return new StringAttribute(value);
164     }
165 
166     /**
167      * Returns the <code>String</code> value represented by this object.
168      *
169      * @return the <code>String</code> value
170      */
171     public String getValue() {
172         return value;
173     }
174 
175     /**
176      * Returns true if the input is an instance of this class and if its
177      * value equals the value contained in this class.
178      *
179      * @param o the object to compare
180      *
181      * @return true if this object and the input represent the same value
182      */
183     public boolean equals(Object o) {
184         if (! (o instanceof StringAttribute))
185             return false;
186 
187         StringAttribute other = (StringAttribute)o;
188 
189         return value.equals(other.value);
190     }
191 
192     /**
193      * Returns the hashcode value used to index and compare this object with
194      * others of the same type. Typically this is the hashcode of the backing
195      * data object.
196      *
197      * @return the object's hashcode value
198      */
199     public int hashCode() {
200         return value.hashCode();
201     }
202 
203     /**
204      * Converts to a String representation.
205      *
206      * @return the String representation
207      */
208     public String toString() {
209         return "StringAttribute: \"" + value + "\"";
210     }
211 
212     /**
213      *
214      */
215     public String encode() {
216         return value;
217     }
218 
219 }