001: /*
002: * Copyright 2001-2007 Hippo (www.hippo.nl)
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: /*
017: * Based on Gianugo Rabellino's (gianugo.rabellino@orixo.com) code.
018: * Found at : http://marc.theaimsgroup.com/?l=xml-cocoon-users&m=110838989005480&w=2
019: */
020: package nl.hippo.cocoon.forms.binding;
021:
022: import java.io.IOException;
023: import java.io.StringReader;
024: import java.util.Properties;
025:
026: import javax.xml.parsers.DocumentBuilder;
027: import javax.xml.parsers.DocumentBuilderFactory;
028: import javax.xml.parsers.ParserConfigurationException;
029: import javax.xml.transform.OutputKeys;
030:
031: import org.apache.cocoon.ProcessingException;
032: import org.apache.cocoon.forms.binding.AbstractCustomBinding;
033: import org.apache.cocoon.forms.binding.BindingException;
034: import org.apache.cocoon.forms.formmodel.Widget;
035: import org.apache.cocoon.xml.XMLUtils;
036: import org.apache.commons.jxpath.JXPathContext;
037: import org.apache.commons.jxpath.Pointer;
038: import org.apache.commons.jxpath.ri.model.beans.NullPropertyPointer;
039: import org.w3c.dom.Document;
040: import org.w3c.dom.Node;
041: import org.w3c.dom.NodeList;
042: import org.xml.sax.InputSource;
043: import org.xml.sax.SAXException;
044: import org.apache.commons.logging.Log;
045: import org.apache.commons.logging.LogFactory;
046:
047: /**
048: * Simple custom binding for HTMLArea and XML data.
049: *
050: * @author <a href="mailto:gianugo.rabellino@orixo.com">Gianugo Rabellino </a>
051: * @version $Id: HTMLAreaBinding.java 10725 2008-03-12 09:36:45Z jjoachimsthal $
052: */
053: public class HTMLAreaBinding extends AbstractCustomBinding {
054:
055: private final Log logger = LogFactory.getLog(HTMLAreaBinding.class);
056:
057: /**
058: * Serialize the node as String upon load.
059: *
060: * @see org.apache.cocoon.forms.binding.AbstractCustomBinding#doLoad(org.apache.co \
061: * coon.forms.formmodel.Widget,
062: * org.apache.commons.jxpath.JXPathContext)
063: */
064:
065: protected void doLoad(Widget frmModel, JXPathContext context)
066: throws BindingException {
067:
068: Pointer pointer = context.getPointer(this .getXpath());
069: if (pointer.getNode() != null) {
070: Node value = (Node) pointer.getNode();
071: NodeList childNodes = value.getChildNodes();
072:
073: String str = "";
074: try {
075: for (int i = 0; i < childNodes.getLength(); i++) {
076:
077: Properties props = new Properties();
078: props.put(OutputKeys.OMIT_XML_DECLARATION, "yes");
079: str += XMLUtils.serializeNode(childNodes.item(i),
080: props);
081: }
082: frmModel.setValue(str);
083: } catch (ProcessingException e) {
084: throw new BindingException("Unable to serialize node",
085: e);
086: }
087:
088: // try {
089: // Properties props = new Properties();
090: // props.put(OutputKeys.OMIT_XML_DECLARATION,"yes");
091: // frmModel.setValue(XMLUtils.serializeNode(value,props));
092: //
093: // } catch (ProcessingException e) {
094: // throw new BindingException("Unable to serialize node", e);
095: // }
096: } else {
097: if (logger.isDebugEnabled()) {
098: logger.debug("non-existent path: skipping "
099: + pointer.asPath());
100: }
101: }
102: }
103:
104: /**
105: * Parse and import the XML String upon save.
106: *
107: * @throws IOException
108: * @throws SAXException
109: * @see org.apache.cocoon.forms.binding.AbstractCustomBinding#doSave(org.apache.co \
110: * coon.forms.formmodel.Widget,
111: * org.apache.commons.jxpath.JXPathContext)
112: */
113: protected void doSave(Widget frmModel, JXPathContext context)
114: throws BindingException, SAXException, IOException {
115:
116: DocumentBuilder builder = null;
117: DocumentBuilderFactory factory = DocumentBuilderFactory
118: .newInstance();
119: try {
120: builder = factory.newDocumentBuilder();
121: } catch (ParserConfigurationException e) {
122: throw new BindingException("Problem getting a parser", e);
123: }
124: String content = (String) frmModel.getValue();
125:
126: if (content != null) {
127: Pointer pointer = context.getPointer(this .getXpath());
128: if (pointer instanceof NullPropertyPointer) {
129: pointer = context.createPath(this .getXpath());
130: }
131:
132: Document dom = builder.parse(new InputSource(
133: new StringReader(content)));
134:
135: Node contextNode = (Node) pointer.getNode();
136:
137: if (contextNode != null) {
138: while (contextNode.getChildNodes().getLength() > 0) {
139: contextNode
140: .removeChild(contextNode.getFirstChild());
141: }
142: }
143: Node result = contextNode.getOwnerDocument().importNode(
144: dom.getDocumentElement(), true);
145: result = contextNode.appendChild(result);
146: }
147: content = "";
148: }
149:
150: }
|