View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.neethi;
17  
18  import javax.xml.stream.XMLStreamException;
19  import javax.xml.stream.XMLStreamWriter;
20  
21  /**
22   * PolicyReference is a wrapper that holds explict PolicyReferences.
23   */
24  public class PolicyReference implements PolicyComponent {
25  
26      private String uri;
27  
28      /**
29       * Sets the Policy URI
30       * @param uri the Policy URI
31       */
32      public void setURI(String uri) {
33          this.uri = uri;
34      }
35  
36      /**
37       * Gets the Policy URI that is refered by self.
38       * @return a String that is the Policy URI refered by self
39       */
40      public String getURI() {
41          return uri;
42      }
43  
44      public boolean equal(PolicyComponent policyComponent) {
45          if (Constants.TYPE_POLICY_REF != policyComponent.getType()) {
46              return false;
47          }
48          
49          String URI = ((PolicyReference) policyComponent).getURI();
50          if (URI != null && URI.length() != 0) {
51              return URI.equals(this.uri);
52          } 
53          
54          return false;
55      }
56  
57  
58      /**
59       * Returns short value of Constants.TYPE_POLICY_REF 
60       */
61      public short getType() {
62          return Constants.TYPE_POLICY_REF;
63      }
64  
65      /**
66       * Throws an UnsupportedOperationException since PolicyReference.normalize()
67       * can't resolve the Policy that it refers to unless a PolicyRegistry is
68       * provided.
69       */
70      public PolicyComponent normalize() {
71          throw new UnsupportedOperationException("PolicyReference.normalize() is meaningless");
72      }
73      
74      /**
75       * Returns normalized version of the Policy that is refered by self. The specified 
76       * PolicyRegistry is used to lookup for the Policy that is refered and <tt>dee</tt> 
77       * indicates the level of normalization fo the returning Policy.
78       * 
79       * @param reg the PolicyRegistry that is used to resolved the Policy refered by self
80       * @param deep the falg to indicate whether returning Policy should be fully normailized
81       * @return the normalized version fo the Policy refered by self
82       */
83      public PolicyComponent normalize(PolicyRegistry reg, boolean deep) {
84          String key = getURI();
85          if (key.startsWith("#")) {
86              key = key.substring(1);
87          }
88          
89          Policy policy = reg.lookup(key);
90          
91          if (policy == null) {
92              throw new RuntimeException(key + " can't be resolved" );
93          }
94          
95          return policy.normalize(reg, deep);
96      }
97  
98      public void serialize(XMLStreamWriter writer) throws XMLStreamException {
99  
100         String wspPrefix = writer.getPrefix(Constants.URI_POLICY_NS);
101         
102         if (wspPrefix == null) {
103             wspPrefix = Constants.ATTR_WSP;
104             writer.setPrefix(wspPrefix, Constants.URI_POLICY_NS);
105         }
106         
107         writer.writeStartElement(wspPrefix, Constants.ELEM_POLICY_REF, Constants.URI_POLICY_NS);
108         
109         writer.writeAttribute(Constants.ATTR_URI, getURI());
110         
111         writer.writeEndElement();
112     }
113 }