001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.dav.server.adapters;
020:
021: import java.io.*;
022: import java.util.logging.*;
023:
024: import javax.xml.parsers.*;
025:
026: import org.openharmonise.commons.xml.*;
027: import org.openharmonise.commons.xml.namespace.NamespaceClashException;
028: import org.openharmonise.dav.server.managers.HarmonisePropertiesManager;
029: import org.openharmonise.dav.server.utils.*;
030: import org.openharmonise.rm.*;
031: import org.openharmonise.rm.resources.publishing.WebPage;
032: import org.openharmonise.rm.resources.xml.*;
033: import org.w3c.dom.*;
034: import org.xml.sax.SAXException;
035:
036: import com.ibm.webdav.*;
037:
038: /**
039: * A class to provide a bridge between the DAV/XML representation of a
040: * <code>WebPage</code> resource and the harmonise class, providing
041: * methods for publishing to and populating from XML.
042: *
043: * @author Michael Bell
044: * @version $Revision: 1.2 $
045: *
046: */
047: public class WebPageResourceAdapter {
048:
049: public static final String TAG_PAGEDEF = "PageDef";
050: public static final String ATTRIB_TIMEOUT = "timeout";
051: public static final String TAG_XML = "XML";
052: public static final String TAG_XSL = "XSL";
053:
054: private WebPage m_page = null;
055: private Element m_xml = null;
056:
057: private static final Logger m_logger = Logger
058: .getLogger(WebPageResourceAdapter.class.getName());
059:
060: /**
061: *
062: */
063: public WebPageResourceAdapter(WebPage page) {
064: m_page = page;
065: }
066:
067: /**
068: * Returns an xml representation of the <code>WebPage</code> object as
069: * <code>String</code>
070: *
071: * @return
072: * @throws WebDAVException
073: */
074: public String asString() throws WebDAVException {
075: String sXML = null;
076:
077: try {
078: Element pageEl = asXML();
079:
080: XMLPrettyPrint xmlPrint = new XMLPrettyPrint();
081:
082: sXML = xmlPrint.printNode(pageEl);
083:
084: } catch (NamespaceClashException e) {
085: throw new WebDAVException(
086: WebDAVStatus.SC_INTERNAL_SERVER_ERROR, e
087: .getLocalizedMessage());
088: }
089:
090: return sXML;
091: }
092:
093: /**
094: * Returns an XML representation of the <code>WebPage</code> object
095: *
096: * @return
097: * @throws WebDAVException
098: */
099: public Element asXML() throws WebDAVException {
100:
101: if (m_xml == null) {
102: try {
103: Document pageDoc = DocumentBuilderFactory.newInstance()
104: .newDocumentBuilder().newDocument();
105:
106: m_xml = pageDoc.createElement(TAG_PAGEDEF);
107:
108: m_xml.setAttribute(ATTRIB_TIMEOUT, String
109: .valueOf(m_page.getTimeout()));
110:
111: pageDoc.appendChild(m_xml);
112:
113: Element xmlEl = pageDoc.createElement(TAG_XML);
114:
115: Element hrefEl = pageDoc
116: .createElement(HarmonisePropertiesManager.TAG_HREF);
117:
118: XMLResource xml = m_page.getXML();
119:
120: if (xml != null) {
121: String sHREF = HarmoniseNameResolver
122: .getDAVPath(xml);
123: hrefEl.appendChild(pageDoc.createTextNode(sHREF));
124:
125: }
126:
127: xmlEl.appendChild(hrefEl);
128:
129: m_xml.appendChild(xmlEl);
130:
131: Element xslEl = pageDoc.createElement(TAG_XSL);
132:
133: hrefEl = pageDoc
134: .createElement(HarmonisePropertiesManager.TAG_HREF);
135: Element outEl = pageDoc.createElement("mimetype");
136:
137: XSLResource xsl = m_page.getXSL();
138:
139: if (xsl != null) {
140: String sHREF = HarmoniseNameResolver
141: .getDAVPath(xsl);
142: hrefEl.appendChild(pageDoc.createTextNode(sHREF));
143: String sOutputType = xsl.getOutputType();
144: outEl.appendChild(pageDoc
145: .createTextNode(sOutputType));
146: }
147:
148: xslEl.appendChild(hrefEl);
149: xslEl.appendChild(outEl);
150:
151: m_xml.appendChild(xslEl);
152: } catch (DataAccessException e) {
153: throw new WebDAVException(
154: WebDAVStatus.SC_INTERNAL_SERVER_ERROR, e
155: .getLocalizedMessage());
156: } catch (ParserConfigurationException e) {
157: throw new WebDAVException(
158: WebDAVStatus.SC_INTERNAL_SERVER_ERROR, e
159: .getLocalizedMessage());
160: } catch (FactoryConfigurationError e) {
161: throw new WebDAVException(
162: WebDAVStatus.SC_INTERNAL_SERVER_ERROR, e
163: .getLocalizedMessage());
164: } catch (NameResolverException e) {
165: m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
166: throw new WebDAVException(
167: WebDAVStatus.SC_INTERNAL_SERVER_ERROR, e
168: .getLocalizedMessage());
169: }
170: }
171: return m_xml;
172:
173: }
174:
175: /**
176: * Populates the <code>WebPage</code> object from the given xml string
177: *
178: * @param sXML
179: * @throws WebDAVException
180: */
181: public void populate(String sXML) throws WebDAVException {
182: try {
183: DocumentBuilderFactory factory = DocumentBuilderFactory
184: .newInstance();
185:
186: factory.setNamespaceAware(true);
187:
188: DocumentBuilder docbuilder = factory.newDocumentBuilder();
189:
190: StringReader reader = new StringReader(sXML);
191:
192: Document contents = docbuilder
193: .parse(new org.xml.sax.InputSource(reader));
194:
195: populate(contents.getDocumentElement());
196: } catch (FactoryConfigurationError e) {
197: throw new WebDAVException(
198: WebDAVStatus.SC_INTERNAL_SERVER_ERROR, e
199: .getLocalizedMessage());
200: } catch (ParserConfigurationException e) {
201: throw new WebDAVException(
202: WebDAVStatus.SC_INTERNAL_SERVER_ERROR, e
203: .getLocalizedMessage());
204: } catch (SAXException e) {
205: throw new WebDAVException(
206: WebDAVStatus.SC_INTERNAL_SERVER_ERROR, e
207: .getLocalizedMessage());
208: } catch (IOException e) {
209: throw new WebDAVException(
210: WebDAVStatus.SC_INTERNAL_SERVER_ERROR, e
211: .getLocalizedMessage());
212: }
213: }
214:
215: /**
216: * Populates the <code>WebPage</code> object from the given xml element
217: *
218: * @param el
219: * @throws WebDAVException
220: */
221: public void populate(Element el) throws WebDAVException {
222: if (el.getTagName().equals(TAG_PAGEDEF) == false) {
223: throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,
224: "Wrong xml to populate page def");
225: }
226:
227: if (el.hasAttribute(ATTRIB_TIMEOUT)) {
228: String sTimeout = el.getAttribute(ATTRIB_TIMEOUT);
229:
230: try {
231: m_page.setTimeout(Integer.parseInt(sTimeout));
232: } catch (NumberFormatException e) {
233: //ignore - timeout doesn't change
234: m_logger.log(Level.WARNING,
235: "Ignoring webpage timeout value", e);
236: }
237:
238: }
239:
240: NodeList nodes = el.getChildNodes();
241:
242: for (int i = 0; i < nodes.getLength(); i++) {
243: if (nodes.item(i).getNodeType() == Node.ELEMENT_NODE) {
244: Element tmpEl = (Element) nodes.item(i);
245: String sTagName = tmpEl.getTagName();
246:
247: try {
248: if (sTagName.equals(TAG_XML)) {
249: String sHREF = getHREFValue(tmpEl);
250: if (sHREF != null) {
251: XMLResource xml = (XMLResource) HarmoniseNameResolver
252: .getObjectFromURL(m_page
253: .getDataStoreInterface(),
254: sHREF);
255: m_page.setXML(xml);
256: }
257: } else if (sTagName.equals(TAG_XSL)) {
258: String sHREF = getHREFValue(tmpEl);
259: if (sHREF != null) {
260: XSLResource xsl = (XSLResource) HarmoniseNameResolver
261: .getObjectFromURL(m_page
262: .getDataStoreInterface(),
263: sHREF);
264: m_page.setXSL(xsl);
265: }
266: }
267: } catch (NameResolverException e) {
268: throw new WebDAVException(
269: WebDAVStatus.SC_INTERNAL_SERVER_ERROR, e
270: .getLocalizedMessage());
271: } catch (PopulateException e) {
272: throw new WebDAVException(
273: WebDAVStatus.SC_INTERNAL_SERVER_ERROR, e
274: .getLocalizedMessage());
275: }
276: }
277: }
278: }
279:
280: /**
281: * Returns the value contained in the 'href' element
282: *
283: * @param hrefParentEl
284: * @return
285: */
286: private String getHREFValue(Element hrefParentEl) {
287: String sVal = null;
288:
289: Element hrefEl = XMLUtils.getFirstNamedChild(hrefParentEl,
290: HarmonisePropertiesManager.TAG_HREF);
291:
292: if (hrefEl != null && hrefEl.getChildNodes().getLength() > 0) {
293: sVal = hrefEl.getFirstChild().getNodeValue();
294: }
295:
296: return sVal;
297: }
298: }
|