View Javadoc

1   /* 
2    * Copyright (c) 2007, Fraunhofer-Gesellschaft
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are
7    * met:
8    * 
9    * (1) Redistributions of source code must retain the above copyright
10   *     notice, this list of conditions and the disclaimer at the end.
11   *     Redistributions in binary form must reproduce the above copyright
12   *     notice, this list of conditions and the following disclaimer in
13   *     the documentation and/or other materials provided with the
14   *     distribution.
15   * 
16   * (2) Neither the name of Fraunhofer nor the names of its
17   *     contributors may be used to endorse or promote products derived
18   *     from this software without specific prior written permission.
19   * 
20   * DISCLAIMER
21   * 
22   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33   *  
34   */
35  package org.ogf.graap.wsag.wsrf;
36  
37  import org.apache.muse.ws.resource.impl.AbstractWsResourceCapability;
38  import org.apache.xmlbeans.XmlObject;
39  import org.ogf.graap.wsag.api.exceptions.WSAgreementException;
40  import org.w3c.dom.Document;
41  import org.w3c.dom.Element;
42  import org.w3c.dom.Node;
43  
44  /**
45   * WSAG4JCapability
46   * 
47   * Abstract capability that provides some convenience methods.
48   * 
49   * @author Oliver Waeldrich
50   * 
51   */
52  public abstract class WSAG4JCapability extends AbstractWsResourceCapability
53  {
54  
55      /**
56       * Converts a XMLBean to a DOM element.
57       * 
58       * @param xmlObject
59       *            the object to convert
60       * @return the converted XMLBean
61       * 
62       * @throws WSAgreementException
63       *             indicates a conversion error
64       */
65      public static Element toElement( XmlObject xmlObject ) throws WSAgreementException
66      {
67          try
68          {
69  
70              if ( xmlObject == null )
71              {
72                  return null;
73              }
74  
75              Node element = xmlObject.getDomNode();
76              int nodeType = element.getNodeType();
77  
78              if ( nodeType == Node.ELEMENT_NODE )
79              {
80                  return (Element) element;
81              }
82  
83              if ( nodeType == Node.DOCUMENT_NODE )
84              {
85                  Document document = (Document) element;
86                  return document.getDocumentElement();
87              }
88  
89              if ( nodeType == Node.DOCUMENT_FRAGMENT_NODE )
90              {
91                  String message = "Document or element expected, but found xml document fragment.";
92                  throw new Exception( message );
93              }
94  
95          }
96          catch ( Exception e )
97          {
98              String message = "Error converting XmlBean to Element. Message: ";
99              throw new WSAgreementException( message + e.getMessage() );
100         }
101 
102         String message =
103             "Error converting XmlBean to Element. Node is neither from type document nor element.";
104         throw new WSAgreementException( message );
105     }
106 
107     /**
108      * Converts an array of XMLBeans to an array of DOM elements.
109      * 
110      * @param xmlObjects
111      *            the objects to convert
112      * @return the element array
113      * 
114      * @throws WSAgreementException
115      *             indicates a conversion error
116      */
117     public static Element[] toElementArray( XmlObject[] xmlObjects ) throws WSAgreementException
118     {
119         Element[] elements = new Element[xmlObjects.length];
120         for ( int i = 0; i < elements.length; i++ )
121         {
122             elements[i] = toElement( xmlObjects[i] );
123         }
124 
125         return elements;
126     }
127 }