View Javadoc

1   /*
2    * Copyright 2009 University Corporation for Advanced Internet Development, Inc.
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  
17  package org.opensaml.xml.util;
18  
19  import org.opensaml.xml.AttributeExtensibleXMLObject;
20  import org.opensaml.xml.BaseBearing;
21  import org.opensaml.xml.IdBearing;
22  import org.opensaml.xml.LangBearing;
23  import org.opensaml.xml.SpaceBearing;
24  import org.opensaml.xml.XMLObject;
25  import org.opensaml.xml.SpaceBearing.XMLSpaceEnum;
26  
27  /**
28   * Helper methods for working with global attributes from the XML namespace. These are namely:
29   * <ol>
30   *   <li>xml:id</li>
31   *   <li>xml:lang</li>
32   *   <li>xml:base</li>
33   *   <li>xml:space</li>
34   * </ol>
35   */
36  public final class XMLAttributeHelper {
37  
38      /**
39       * Private constructor.
40       */
41      private XMLAttributeHelper() {
42      }
43      
44      /**
45       * Adds a <code>xml:id</code> attribute to the given XML object.
46       * 
47       * @param xmlObject the XML object to which to add the attribute
48       * @param id the Id value
49       */
50      public static void addXMLId(XMLObject xmlObject, String id) {
51          if (xmlObject instanceof IdBearing) {
52              ((IdBearing)xmlObject).setXMLId(id);
53          } else if (xmlObject instanceof AttributeExtensibleXMLObject) {
54              ((AttributeExtensibleXMLObject)xmlObject).getUnknownAttributes()
55                  .put(IdBearing.XML_ID_ATTR_NAME, id);
56          } else {
57              throw new IllegalArgumentException("Specified object was neither IdBearing nor AttributeExtensible");
58          }
59      }
60      
61      /**
62       * Gets the <code>xml:id</code> attribute from a given XML object.
63       * 
64       * @param xmlObject the XML object from which to extract the attribute
65       * 
66       * @return the value of the xml:id attribute, or null if not present
67       */
68      public static String getXMLId(XMLObject xmlObject) {
69          String value = null;
70          if (xmlObject instanceof IdBearing) {
71              value = DatatypeHelper.safeTrimOrNullString(((IdBearing)xmlObject).getXMLId());
72              if (value != null) {
73                  return value;
74              }
75          }
76          if (xmlObject instanceof AttributeExtensibleXMLObject) {
77              value = DatatypeHelper.safeTrimOrNullString(((AttributeExtensibleXMLObject)xmlObject)
78                          .getUnknownAttributes().get(IdBearing.XML_ID_ATTR_NAME));
79              return value;
80          }
81          return null;
82      }
83      
84      /**
85       * Adds a <code>xml:lang</code> attribute to the given XML object.
86       * 
87       * @param xmlObject the XML object to which to add the attribute
88       * @param lang the lang value
89       */
90      public static void addXMLLang(XMLObject xmlObject, String lang) {
91          if (xmlObject instanceof LangBearing) {
92              ((LangBearing)xmlObject).setXMLLang(lang);
93          } else if (xmlObject instanceof AttributeExtensibleXMLObject) {
94              ((AttributeExtensibleXMLObject)xmlObject).getUnknownAttributes()
95                  .put(LangBearing.XML_LANG_ATTR_NAME, lang);
96          } else {
97              throw new IllegalArgumentException("Specified object was neither LangBearing nor AttributeExtensible");
98          }
99      }
100     
101     /**
102      * Gets the <code>xml:lang</code> attribute from a given XML object.
103      * 
104      * @param xmlObject the XML object from which to extract the attribute
105      * 
106      * @return the value of the xml:lang attribute, or null if not present
107      */
108     public static String getXMLLang(XMLObject xmlObject) {
109         String value = null;
110         if (xmlObject instanceof LangBearing) {
111             value = DatatypeHelper.safeTrimOrNullString(((LangBearing)xmlObject).getXMLLang());
112             if (value != null) {
113                 return value;
114             }
115         }
116         if (xmlObject instanceof AttributeExtensibleXMLObject) {
117             value = DatatypeHelper.safeTrimOrNullString(((AttributeExtensibleXMLObject)xmlObject)
118                         .getUnknownAttributes().get(LangBearing.XML_LANG_ATTR_NAME));
119             return value;
120         }
121         return null;
122     }
123     
124     /**
125      * Adds a <code>xml:base</code> attribute to the given XML object.
126      * 
127      * @param xmlObject the XML object to which to add the attribute
128      * @param base the base value
129      */
130     public static void addXMLBase(XMLObject xmlObject, String base) {
131         if (xmlObject instanceof BaseBearing) {
132             ((BaseBearing)xmlObject).setXMLBase(base);
133         } else if (xmlObject instanceof AttributeExtensibleXMLObject) {
134             ((AttributeExtensibleXMLObject)xmlObject).getUnknownAttributes()
135                 .put(BaseBearing.XML_BASE_ATTR_NAME, base);
136         } else {
137             throw new IllegalArgumentException("Specified object was neither BaseBearing nor AttributeExtensible");
138         }
139     }
140     
141     /**
142      * Gets the <code>xml:base</code> attribute from a given XML object.
143      * 
144      * @param xmlObject the XML object from which to extract the attribute
145      * 
146      * @return the value of the xml:base attribute, or null if not present
147      */
148     public static String getXMLBase(XMLObject xmlObject) {
149         String value = null;
150         if (xmlObject instanceof BaseBearing) {
151             value = DatatypeHelper.safeTrimOrNullString(((BaseBearing)xmlObject).getXMLBase());
152             if (value != null) {
153                 return value;
154             }
155         }
156         if (xmlObject instanceof AttributeExtensibleXMLObject) {
157             value = DatatypeHelper.safeTrimOrNullString(((AttributeExtensibleXMLObject)xmlObject)
158                         .getUnknownAttributes().get(BaseBearing.XML_BASE_ATTR_NAME));
159             return value;
160         }
161         return null;
162     }
163     
164     /**
165      * Adds a <code>xml:space</code> attribute to the given XML object.
166      * 
167      * @param xmlObject the XML object to which to add the attribute
168      * @param space the space value
169      */
170     public static void addXMLSpace(XMLObject xmlObject, XMLSpaceEnum space) {
171         if (xmlObject instanceof SpaceBearing) {
172             ((SpaceBearing)xmlObject).setXMLSpace(space);
173         } else if (xmlObject instanceof AttributeExtensibleXMLObject) {
174             ((AttributeExtensibleXMLObject)xmlObject).getUnknownAttributes()
175                 .put(SpaceBearing.XML_SPACE_ATTR_NAME, space.toString());
176         } else {
177             throw new IllegalArgumentException("Specified object was neither SpaceBearing nor AttributeExtensible");
178         }
179     }
180     
181     /**
182      * Gets the <code>xml:space</code> attribute from a given XML object.
183      * 
184      * @param xmlObject the XML object from which to extract the attribute
185      * 
186      * @return the value of the xml:space attribute, or null if not present
187      */
188     public static XMLSpaceEnum getXMLSpace(XMLObject xmlObject) {
189         XMLSpaceEnum valueEnum = null;
190         if (xmlObject instanceof SpaceBearing) {
191             valueEnum = ((SpaceBearing)xmlObject).getXMLSpace();
192             if (valueEnum != null) {
193                 return valueEnum;
194             }
195         }
196         String valueString = null;
197         if (xmlObject instanceof AttributeExtensibleXMLObject) {
198             valueString = DatatypeHelper.safeTrimOrNullString(((AttributeExtensibleXMLObject)xmlObject)
199                         .getUnknownAttributes().get(SpaceBearing.XML_SPACE_ATTR_NAME));
200             if (valueString != null) {
201                 return XMLSpaceEnum.parseValue(valueString);
202             }
203         }
204         return null;
205     }
206 }