View Javadoc

1   /*
2    * ResourceHandler.java
3    *
4    * Created on April 16, 2004, 1:25 PM
5    */
6   
7   package gov.noaa.gdsg.xmldbremote.service;
8   
9   import org.xmldb.api.base.Resource;
10  import org.xmldb.api.modules.XMLResource;
11  
12  import org.xmldb.api.base.XMLDBException;
13  import org.xmldb.api.base.ErrorCodes;
14  import org.xmldb.api.base.Collection;
15  import gov.noaa.gdsg.xmldbremote.service.transport.ResourceTransport;
16  import gov.noaa.gdsg.xmldbremote.service.transport.BaseTransport;
17  
18  import org.w3c.dom.Node;
19  import javax.xml.transform.stream.StreamResult;
20  import javax.xml.transform.dom.DOMSource;
21  import javax.xml.transform.Transformer;
22  import javax.xml.transform.TransformerFactory;
23  import javax.xml.parsers.DocumentBuilder;
24  import javax.xml.parsers.DocumentBuilderFactory;
25  import org.xml.sax.InputSource;
26  
27  import java.io.StringWriter;
28  import java.io.StringReader;
29  import java.io.IOException;
30  
31  /***
32   * This object is deisnged to keep track of Resource objects
33   * @version $Id: ResourceHandler.java,v 1.2 2004/12/29 01:06:46 mrxtravis Exp $
34   * @author  tns
35   */
36  public class ResourceHandler extends BaseHandler {
37      
38      /*** Creates a new instance of ResourceHandler */
39      public ResourceHandler() {
40      }
41      
42      
43      /***
44       * Creates the Resource Transport object.
45       * @param object The object in which to create a lightweight transport object.
46       * @throws org.xmldb.api.base.XMLDBException IF something goes wrong.
47       * @return The new transport object.
48       */
49      protected BaseTransport createTransportObject(Object object) throws XMLDBException {
50          XMLResource resource = (XMLResource) object;
51          ResourceTransport transport = new ResourceTransport();
52          transport.setResourceType(resource.getResourceType());
53          transport.setId(resource.getId());
54          transport.setRootNodeName(resource.getContentAsDOM().getNodeName());
55          return transport;
56      }
57      
58      /***
59       * Returns the resource content as an XML String.
60       * @param transport The object representing the object in which the contents
61       * will be extracted.
62       * @throws org.xmldb.api.base.XMLDBException All exceptions are caught and rethrown.
63       * @return A string representing the resource.
64       */
65      public String getContentAsDOMText(ResourceTransport transport) throws XMLDBException {
66          
67          try {
68              Resource resource = (Resource) this.getObjectFromSession(transport);
69              if (! (resource instanceof XMLResource)){
70                  throw new XMLDBException( ErrorCodes.NOT_IMPLEMENTED, "Resource of the service must be of type XMLResource");
71              }
72              XMLResource xResource = (XMLResource) resource;
73              //first we get the DOM
74              Node node = xResource.getContentAsDOM();
75              DOMSource source = new DOMSource();
76              source.setNode(node);
77              StreamResult result = new StreamResult();
78              StringWriter writer = new StringWriter();
79              result.setWriter(writer);
80              TransformerFactory factory = new org.apache.xalan.processor.TransformerFactoryImpl();
81              factory.newTransformer().transform(source,result);
82              return writer.getBuffer().toString();
83          } catch (javax.xml.transform.TransformerConfigurationException e){
84              throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.toString());
85          } catch (javax.xml.transform.TransformerException e){
86              throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.toString());
87          }
88      }
89      
90      /***
91       * Returns the document id.
92       * @param transport The transport representing the object in which to tget a document id.
93       * @throws org.xmldb.api.base.XMLDBException If the wrapped method throws one.
94       * @return Whatever is returned by the wrapped method.
95       */
96      
97      public String getDocumentId(ResourceTransport transport) throws XMLDBException {
98          XMLResource resource = (XMLResource) this.getObjectFromSession(transport);
99          return resource.getDocumentId();
100     }
101     
102     //getSAXFeature, not handled
103     
104     /***
105      * Sets the resource content as the specified DOM String.
106      * @param transport The transport object representing the actual object.
107      * @param xml The XML to set as DOM.
108      * @throws org.xmldb.api.base.XMLDBException All exceptions are caught and rethrown.
109      */
110     public void setContentAsDOMText(ResourceTransport transport,String xml) throws XMLDBException {
111         try {
112             XMLResource resource = (XMLResource) this.getObjectFromSession(transport);
113             Node doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
114             .parse(new InputSource(new StringReader(xml)));
115             resource.setContentAsDOM(doc);
116         } catch (javax.xml.parsers.ParserConfigurationException e){
117             throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.toString());
118         } catch (org.xml.sax.SAXException e){
119             throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.toString());
120         } catch (java.io.IOException e){
121             throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.toString());
122         }
123         
124     }
125     
126     //setContentAsSax not implemented
127     
128     //set SAX Feature not implemented
129     
130     //as for the inherited methods, getContent and setContent are not supported
131     //getId and getResourceType are not supported.
132     /***
133      * Wrappes the {@see Resrouce#getParentCollection} method.
134      * @param transport The object representing the actual object to work on.
135      * @throws org.xmldb.api.base.XMLDBException If the wrapped method throws one.
136      * @return Whatever the mapped method throws.
137      */
138     public Collection getParentCollection(ResourceTransport transport) throws XMLDBException {
139         Resource resource = (Resource) this.getObjectFromSession(transport);
140         return resource.getParentCollection();
141     }
142     
143     
144     
145     
146 }