2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.jaxb
;
12 import java
.io
.StringReader
;
13 import java
.io
.StringWriter
;
15 import javax
.xml
.bind
.annotation
.adapters
.XmlAdapter
;
16 import javax
.xml
.parsers
.DocumentBuilder
;
17 import javax
.xml
.parsers
.DocumentBuilderFactory
;
18 import javax
.xml
.transform
.OutputKeys
;
19 import javax
.xml
.transform
.Result
;
20 import javax
.xml
.transform
.Source
;
21 import javax
.xml
.transform
.Transformer
;
22 import javax
.xml
.transform
.TransformerFactory
;
23 import javax
.xml
.transform
.dom
.DOMSource
;
24 import javax
.xml
.transform
.stream
.StreamResult
;
26 import org
.apache
.commons
.lang
.StringEscapeUtils
;
27 import org
.apache
.commons
.logging
.Log
;
28 import org
.apache
.commons
.logging
.LogFactory
;
29 import org
.w3c
.dom
.DOMException
;
30 import org
.w3c
.dom
.Document
;
31 import org
.w3c
.dom
.DocumentFragment
;
32 import org
.w3c
.dom
.NamedNodeMap
;
33 import org
.w3c
.dom
.Node
;
34 import org
.w3c
.dom
.NodeList
;
35 import org
.xml
.sax
.InputSource
;
37 public class FormattedTextAdapter
extends XmlAdapter
<FormattedText
,java
.lang
.String
> {
38 public static String
[] NAMESPACE_PREFIXES
= {"xmlns:common",
51 @SuppressWarnings("unused")
52 private static final Log logger
= LogFactory
.getLog(FormattedTextAdapter
.class);
54 public FormattedText
marshal(String string
) throws Exception
{
56 string
= StringEscapeUtils
.escapeXml(string
);
57 String documentString
= "<?xml version=\"1.0\"?><text>" + string
+ "</text>";
58 //log.debug("Parsing " + documentString);
59 FormattedText text
= new FormattedText();
60 DocumentBuilderFactory factory
= DocumentBuilderFactory
.newInstance();
61 DocumentBuilder parser
= factory
.newDocumentBuilder();
62 Document document
= parser
.parse(new InputSource(new StringReader(documentString
)));
63 NodeList childNodes
= document
.getDocumentElement().getChildNodes();
64 for(int i
= 0; i
< childNodes
.getLength(); i
++) {
65 Node node
= childNodes
.item(i
);
66 if(node
instanceof org
.w3c
.dom
.Text
) {
67 org
.w3c
.dom
.Text textNode
= (org
.w3c
.dom
.Text
) node
;
69 text
.getContent().add(textNode
.getTextContent());
71 text
.getContent().add(node
);
79 public String
unmarshal(FormattedText text
) throws Exception
{
80 DocumentBuilderFactory factory
= DocumentBuilderFactory
.newInstance();
81 Document document
= factory
.newDocumentBuilder().newDocument();
82 DocumentFragment documentFragment
= document
.createDocumentFragment();
84 for(Object object
: text
.getContent()) {
85 if(object
instanceof String
) {
86 String string
= (String
)object
;
87 documentFragment
.appendChild(document
.createTextNode(string
));
89 Node node
= (Node
)object
;
90 NamedNodeMap attributes
= node
.getAttributes();
91 for(String prefix
: FormattedTextAdapter
.NAMESPACE_PREFIXES
) {
93 attributes
.removeNamedItem(prefix
);
94 } catch(DOMException de
){
95 if(de
.code
!= DOMException
.NOT_FOUND_ERR
) {
102 documentFragment
.appendChild(document
.importNode(node
,true));
107 TransformerFactory transformerFactory
= TransformerFactory
.newInstance();
108 Transformer transformer
= transformerFactory
.newTransformer();
109 transformer
.setOutputProperty(OutputKeys
.OMIT_XML_DECLARATION
, "yes");
111 Source input
= new DOMSource(documentFragment
);
112 StringWriter stringWriter
= new StringWriter();
113 Result output
= new StreamResult(stringWriter
);
114 transformer
.transform(input
, output
);
115 String result
= stringWriter
.toString();
116 result
= StringEscapeUtils
.unescapeXml(result
);