KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > dav > server > adapters > WebPageResourceAdapter


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.dav.server.adapters;
20
21 import java.io.*;
22 import java.util.logging.*;
23
24 import javax.xml.parsers.*;
25
26 import org.openharmonise.commons.xml.*;
27 import org.openharmonise.commons.xml.namespace.NamespaceClashException;
28 import org.openharmonise.dav.server.managers.HarmonisePropertiesManager;
29 import org.openharmonise.dav.server.utils.*;
30 import org.openharmonise.rm.*;
31 import org.openharmonise.rm.resources.publishing.WebPage;
32 import org.openharmonise.rm.resources.xml.*;
33 import org.w3c.dom.*;
34 import org.xml.sax.SAXException JavaDoc;
35
36 import com.ibm.webdav.*;
37
38 /**
39  * A class to provide a bridge between the DAV/XML representation of a
40  * <code>WebPage</code> resource and the harmonise class, providing
41  * methods for publishing to and populating from XML.
42  *
43  * @author Michael Bell
44  * @version $Revision: 1.2 $
45  *
46  */

47 public class WebPageResourceAdapter {
48
49     public static final String JavaDoc TAG_PAGEDEF = "PageDef";
50     public static final String JavaDoc ATTRIB_TIMEOUT = "timeout";
51     public static final String JavaDoc TAG_XML = "XML";
52     public static final String JavaDoc TAG_XSL = "XSL";
53
54     private WebPage m_page = null;
55     private Element m_xml = null;
56     
57     private static final Logger m_logger = Logger.getLogger(WebPageResourceAdapter.class.getName());
58
59     /**
60      *
61      */

62     public WebPageResourceAdapter(WebPage page) {
63         m_page = page;
64     }
65     
66     /**
67      * Returns an xml representation of the <code>WebPage</code> object as
68      * <code>String</code>
69      *
70      * @return
71      * @throws WebDAVException
72      */

73     public String JavaDoc asString() throws WebDAVException {
74         String JavaDoc sXML = null;
75         
76         try {
77             Element pageEl = asXML();
78             
79             XMLPrettyPrint xmlPrint = new XMLPrettyPrint();
80     
81             sXML = xmlPrint.printNode(pageEl);
82
83         } catch (NamespaceClashException e) {
84             throw new WebDAVException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR,e.getLocalizedMessage());
85         }
86         
87         return sXML;
88     }
89     
90     /**
91      * Returns an XML representation of the <code>WebPage</code> object
92      *
93      * @return
94      * @throws WebDAVException
95      */

96     public Element asXML() throws WebDAVException {
97         
98         if(m_xml == null) {
99             try {
100                 Document pageDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
101                     
102                 m_xml = pageDoc.createElement(TAG_PAGEDEF);
103                     
104                 m_xml.setAttribute(ATTRIB_TIMEOUT,String.valueOf(m_page.getTimeout()));
105                     
106                 pageDoc.appendChild(m_xml);
107                     
108                 Element xmlEl = pageDoc.createElement(TAG_XML);
109                     
110                 Element hrefEl = pageDoc.createElement(HarmonisePropertiesManager.TAG_HREF);
111                     
112                 XMLResource xml = m_page.getXML();
113                     
114                 if(xml != null) {
115                     String JavaDoc sHREF = HarmoniseNameResolver.getDAVPath(xml);
116                     hrefEl.appendChild(pageDoc.createTextNode(sHREF));
117                         
118                 }
119                     
120                 xmlEl.appendChild(hrefEl);
121                     
122                 m_xml.appendChild(xmlEl);
123                     
124                 Element xslEl = pageDoc.createElement(TAG_XSL);
125                     
126                 hrefEl = pageDoc.createElement(HarmonisePropertiesManager.TAG_HREF);
127                 Element outEl = pageDoc.createElement("mimetype");
128                     
129                 XSLResource xsl = m_page.getXSL();
130                     
131                 if(xsl != null) {
132                     String JavaDoc sHREF = HarmoniseNameResolver.getDAVPath(xsl);
133                     hrefEl.appendChild(pageDoc.createTextNode(sHREF));
134                     String JavaDoc sOutputType = xsl.getOutputType();
135                     outEl.appendChild(pageDoc.createTextNode(sOutputType));
136                 }
137                 
138                 xslEl.appendChild(hrefEl);
139                 xslEl.appendChild(outEl);
140                     
141                 m_xml.appendChild(xslEl);
142             } catch (DataAccessException e) {
143                 throw new WebDAVException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR,e.getLocalizedMessage());
144             } catch (ParserConfigurationException e) {
145                 throw new WebDAVException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR,e.getLocalizedMessage());
146             } catch (FactoryConfigurationError e) {
147                 throw new WebDAVException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR,e.getLocalizedMessage());
148             } catch (NameResolverException e) {
149                 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
150                 throw new WebDAVException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR,e.getLocalizedMessage());
151             }
152         }
153         return m_xml;
154             
155     }
156     
157     /**
158      * Populates the <code>WebPage</code> object from the given xml string
159      *
160      * @param sXML
161      * @throws WebDAVException
162      */

163     public void populate(String JavaDoc sXML) throws WebDAVException {
164         try {
165             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
166             
167             factory.setNamespaceAware(true);
168             
169             DocumentBuilder docbuilder = factory.newDocumentBuilder();
170             
171             StringReader reader = new StringReader(sXML);
172             
173             Document contents = docbuilder.parse(
174                                new org.xml.sax.InputSource JavaDoc(reader));
175                                
176             populate(contents.getDocumentElement());
177         } catch (FactoryConfigurationError e) {
178             throw new WebDAVException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR,e.getLocalizedMessage());
179         } catch (ParserConfigurationException e) {
180             throw new WebDAVException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR,e.getLocalizedMessage());
181         } catch (SAXException JavaDoc e) {
182             throw new WebDAVException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR,e.getLocalizedMessage());
183         } catch (IOException e) {
184             throw new WebDAVException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR,e.getLocalizedMessage());
185         }
186     }
187     
188     /**
189      * Populates the <code>WebPage</code> object from the given xml element
190      *
191      * @param el
192      * @throws WebDAVException
193      */

194     public void populate(Element el) throws WebDAVException {
195         if(el.getTagName().equals(TAG_PAGEDEF) == false) {
196             throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,"Wrong xml to populate page def");
197         }
198         
199         if(el.hasAttribute(ATTRIB_TIMEOUT)) {
200             String JavaDoc sTimeout = el.getAttribute(ATTRIB_TIMEOUT);
201             
202             try {
203                 m_page.setTimeout(Integer.parseInt(sTimeout));
204             } catch (NumberFormatException JavaDoc e) {
205                 //ignore - timeout doesn't change
206
m_logger.log(Level.WARNING, "Ignoring webpage timeout value", e);
207             }
208             
209         }
210         
211         NodeList nodes = el.getChildNodes();
212         
213         for (int i = 0; i < nodes.getLength(); i++) {
214             if(nodes.item(i).getNodeType() == Node.ELEMENT_NODE) {
215                 Element tmpEl = (Element) nodes.item(i);
216                 String JavaDoc sTagName = tmpEl.getTagName();
217                 
218                 try {
219                     if(sTagName.equals(TAG_XML)) {
220                         String JavaDoc sHREF = getHREFValue(tmpEl);
221                         if(sHREF != null) {
222                             XMLResource xml = (XMLResource) HarmoniseNameResolver.getObjectFromURL(m_page.getDataStoreInterface(), sHREF);
223                             m_page.setXML(xml);
224                         }
225                     } else if(sTagName.equals(TAG_XSL)) {
226                         String JavaDoc sHREF = getHREFValue(tmpEl);
227                         if(sHREF != null) {
228                             XSLResource xsl = (XSLResource) HarmoniseNameResolver.getObjectFromURL(m_page.getDataStoreInterface(), sHREF);
229                             m_page.setXSL(xsl);
230                         }
231                     }
232                 } catch (NameResolverException e) {
233                     throw new WebDAVException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR,e.getLocalizedMessage());
234                 } catch (PopulateException e) {
235                     throw new WebDAVException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR,e.getLocalizedMessage());
236                 }
237             }
238         }
239     }
240     
241     /**
242      * Returns the value contained in the 'href' element
243      *
244      * @param hrefParentEl
245      * @return
246      */

247     private String JavaDoc getHREFValue(Element hrefParentEl) {
248         String JavaDoc sVal = null;
249         
250         Element hrefEl = XMLUtils.getFirstNamedChild(hrefParentEl, HarmonisePropertiesManager.TAG_HREF);
251         
252         if(hrefEl != null && hrefEl.getChildNodes().getLength() > 0) {
253             sVal = hrefEl.getFirstChild().getNodeValue();
254         }
255         
256         return sVal;
257     }
258 }
259
Popular Tags