View Javadoc
1   /*
2    * XmldbClientXMLResource.java
3    *
4    * Created on May 26, 2004, 12:40 PM
5    */
6   
7   package gov.noaa.gdsg.xmldbremote.xmldbClient;
8   
9   import org.xmldb.api.modules.XMLResource;
10  
11  import javax.xml.transform.dom.DOMSource;
12  import javax.xml.transform.stream.StreamResult;
13  import javax.xml.parsers.DocumentBuilderFactory;
14  
15  import org.w3c.dom.Node;
16  import java.io.StringReader;
17  import java.io.StringWriter;
18  import org.xml.sax.InputSource;
19  
20  import org.apache.log4j.Logger;
21  import org.w3c.dom.Document;
22  
23  /***
24   * Tranforms SOAP calls into the XML:DB API.  This represents XMLResource objects.
25   *
26   * @author  tns
27   * @version $Id: XmldbClientXMLResource.java,v 1.3 2004/12/29 01:08:05 mrxtravis Exp $
28   */
29  public class XmldbClientXMLResource extends XmldbClientResource implements XMLResource {
30      
31      private static Logger log = Logger.getLogger(XmldbClientXMLResource.class);
32      
33      private Node node = null;
34      private Object documentMutex = new Object();
35      
36      
37      /*** Creates a new instance of XmldbClientXMLResource
38       * @param resourceTransport The resource which represents the object on the remote server.
39       * @param stub used to make SOAP calls
40       * @param parentCollection The collection this resource belongs to.
41       */
42      public XmldbClientXMLResource(
43              gov.noaa.gdsg.xmldbremote.Resource resourceTransport,
44              gov.noaa.gdsg.xmldbremote.NmmrdbSoapBindingStub stub,
45              gov.noaa.gdsg.xmldbremote.xmldbClient.XmldbClientCollection parentCollection) {
46          
47          super(resourceTransport,stub,parentCollection);
48          
49      }
50      
51      /*** Getter for property resourceTransport.
52       * @return Value of property resourceTransport.
53       *
54       */
55      public gov.noaa.gdsg.xmldbremote.Resource getResourceTransport() {
56          return this.resourceTransport;
57      }
58      
59      /*** Setter for property resourceTransport.
60       * @param resourceTransport New value of property resourceTransport.
61       *
62       */
63      public void setResourceTransport(gov.noaa.gdsg.xmldbremote.Resource resourceTransport) {
64          this.resourceTransport = resourceTransport;
65      }
66      
67      /*** Returns 'XMLResource' string.
68       *@return 'XMLResource'.
69       *@throws org.xmldb.api.base.XMLDBException Never
70       */
71      public String getResourceType() throws org.xmldb.api.base.XMLDBException {
72          return "XMLResource";
73      }
74      
75      /*** Retrieves the Resource content as an org.w3c.dom.Node object.
76       *@throws org.xmldb.api.base.XMLDBException All is caught and rethrown as one.
77       *@return A Node representing the contents of this resource
78       */
79      public org.w3c.dom.Node getContentAsDOM() throws org.xmldb.api.base.XMLDBException {
80          try {
81              synchronized (this.documentMutex){
82                  if (this.node == null){
83                      Document document = null;
84                      String nodeName = this.resourceTransport.getRootNodeName();
85                      String transportXML = this.stub.getContentAsDOMText(resourceTransport);
86                      if (transportXML == null){
87                          log.warn("ResourceTransport object " + resourceTransport + " returned a null when asked for DOM Text");
88                          document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
89                      } else {
90                          InputSource inputSource = new InputSource(new StringReader(transportXML));
91                          document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputSource);
92                      }
93                      if ("#document".equals(nodeName)){
94                          this.node = document;
95                      } else {
96                          this.node = document.getDocumentElement();
97                      }
98                  }
99                  return this.node;
100             }
101         } catch (org.xml.sax.SAXException e){
102             throw new org.xmldb.api.base.XMLDBException(org.xmldb.api.base.ErrorCodes.VENDOR_ERROR, e);
103         } catch (javax.xml.parsers.ParserConfigurationException e){
104             throw new org.xmldb.api.base.XMLDBException(org.xmldb.api.base.ErrorCodes.VENDOR_ERROR, e);
105         } catch (java.io.IOException e){
106             throw new org.xmldb.api.base.XMLDBException(org.xmldb.api.base.ErrorCodes.VENDOR_ERROR, e);
107         }
108     }
109     
110     /***
111      * Throws a NOT_IMPLEMENTED Exception.
112      * @throws org.xmldb.api.base.XMLDBException With ErrorCodes.NOT_IMPLEMENTED.
113      * @param contentHandler ignored.
114      */
115     public void getContentAsSAX(org.xml.sax.ContentHandler contentHandler) throws org.xmldb.api.base.XMLDBException {
116         throw new org.xmldb.api.base.XMLDBException(
117                 org.xmldb.api.base.ErrorCodes.NOT_IMPLEMENTED);
118     }
119     
120     /***Returns the id identifying this document this XMLResource is part of.  It is the same as
121      * {@link Resource#getId} if this resource is an org.w3c.dom.Document
122      *@throws org.xmldb.api.base.XMLDBException All exceptions are rethrown as one.
123      *@return The document id.
124      */
125     public String getDocumentId() throws org.xmldb.api.base.XMLDBException {
126         try {
127             return stub.getDocumentId(this.resourceTransport);
128         } catch (java.rmi.RemoteException e){
129             throw new org.xmldb.api.base.XMLDBException(
130                     org.xmldb.api.base.ErrorCodes.VENDOR_ERROR, e.toString());
131         }
132     }
133     
134     /***This syncronises the local DOM on the server with the local DOM.  This is
135      *important because someone may want to get the DOM, and make some changes.
136      *@throws org.xmldb.api.base.XMLDBException All exceptions are rethrown as one.
137      */
138     public void syncDomWithService() throws org.xmldb.api.base.XMLDBException {
139         this.setContentAsDOM(this.getContentAsDOM());
140     }
141     
142     /*** Sets the content using an org.w3c.dom.Node.
143      *@param node The Node which should replace the current node
144      *@throws org.xmldb.api.base.XMLDBException All exceptions are rethrown as one.
145      */
146     public void setContentAsDOM(org.w3c.dom.Node node) throws org.xmldb.api.base.XMLDBException {
147         try {
148             synchronized (this.documentMutex){
149                 DOMSource source = new DOMSource(node);
150                 StringWriter stringWriter = new StringWriter();
151                 StreamResult result = new StreamResult(stringWriter);
152                 org.apache.xalan.processor.TransformerFactoryImpl factory =
153                         new org.apache.xalan.processor.TransformerFactoryImpl();
154                 
155                 factory.newTransformer().transform(source,result);
156                 stub.setContentAsDOMText(resourceTransport,stringWriter.getBuffer().toString());
157                 this.node = node;
158             }
159         } catch (javax.xml.transform.TransformerConfigurationException e){
160             throw new org.xmldb.api.base.XMLDBException(org.xmldb.api.base.ErrorCodes.VENDOR_ERROR, e);
161         } catch (javax.xml.transform.TransformerException e){
162             throw new org.xmldb.api.base.XMLDBException(org.xmldb.api.base.ErrorCodes.VENDOR_ERROR, e);
163         } catch (java.rmi.RemoteException e){
164             throw new org.xmldb.api.base.XMLDBException(org.xmldb.api.base.ErrorCodes.VENDOR_ERROR,e);
165         }
166     }
167     
168     /***
169      * Throws a NOT_IMPLEMENTED Exception.
170      * @throws org.xmldb.api.base.XMLDBException With ErrorCodes.NOT_IMPLEMENTED.
171      * @return Nothing, ever.
172      */
173     public org.xml.sax.ContentHandler setContentAsSAX() throws org.xmldb.api.base.XMLDBException {
174         throw new org.xmldb.api.base.XMLDBException(
175                 org.xmldb.api.base.ErrorCodes.NOT_IMPLEMENTED);
176     }
177     
178     public boolean getSAXFeature(String feature) {
179         throw new UnsupportedOperationException("SAX Not supported");
180     }
181     
182     public void setSAXFeature(String feature, boolean setting) {
183         throw new UnsupportedOperationException("SAX Not supported");
184     }
185     
186 }