01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */package org.apache.cxf.aegis.type.xml;
19:
20: import javax.xml.parsers.DocumentBuilder;
21: import javax.xml.parsers.DocumentBuilderFactory;
22: import javax.xml.parsers.ParserConfigurationException;
23: import javax.xml.stream.XMLStreamException;
24: import javax.xml.stream.XMLStreamReader;
25:
26: import org.w3c.dom.Document;
27:
28: import org.apache.cxf.aegis.Context;
29: import org.apache.cxf.aegis.DatabindingException;
30: import org.apache.cxf.aegis.type.Type;
31: import org.apache.cxf.aegis.xml.MessageReader;
32: import org.apache.cxf.aegis.xml.MessageWriter;
33: import org.apache.cxf.aegis.xml.stax.ElementReader;
34: import org.apache.cxf.aegis.xml.stax.ElementWriter;
35: import org.apache.cxf.staxutils.FragmentStreamReader;
36: import org.apache.cxf.staxutils.StaxUtils;
37:
38: /**
39: * Reads and writes <code>org.w3c.dom.Document</code> types.
40: *
41: * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
42: */
43: public class DocumentType extends Type {
44: private DocumentBuilder builder;
45:
46: public DocumentType() {
47: DocumentBuilderFactory factory = DocumentBuilderFactory
48: .newInstance();
49: factory.setNamespaceAware(true);
50: try {
51: builder = factory.newDocumentBuilder();
52: } catch (ParserConfigurationException e) {
53: throw new DatabindingException(
54: "Couldn't load document builder.", e);
55: }
56: setWriteOuter(false);
57: }
58:
59: public DocumentType(DocumentBuilder builder) {
60: this .builder = builder;
61: setWriteOuter(false);
62: }
63:
64: @Override
65: public Object readObject(MessageReader mreader, Context context)
66: throws DatabindingException {
67: try {
68: XMLStreamReader reader = ((ElementReader) mreader)
69: .getXMLStreamReader();
70: return StaxUtils.read(builder, new FragmentStreamReader(
71: reader), true);
72: } catch (XMLStreamException e) {
73: throw new DatabindingException("Could not parse xml.", e);
74: }
75: }
76:
77: @Override
78: public void writeObject(Object object, MessageWriter writer,
79: Context context) throws DatabindingException {
80: Document doc = (Document) object;
81:
82: try {
83: StaxUtils.writeElement(doc.getDocumentElement(),
84: ((ElementWriter) writer).getXMLStreamWriter(),
85: false);
86: } catch (XMLStreamException e) {
87: throw new DatabindingException("Could not write xml.", e);
88: }
89: }
90: }
|