1 package eu
.etaxonomy
.cdm
.jaxb
;
3 import java
.io
.StringReader
;
4 import java
.io
.StringWriter
;
6 import javax
.xml
.bind
.annotation
.adapters
.XmlAdapter
;
7 import javax
.xml
.parsers
.DocumentBuilder
;
8 import javax
.xml
.parsers
.DocumentBuilderFactory
;
9 import javax
.xml
.transform
.OutputKeys
;
10 import javax
.xml
.transform
.Result
;
11 import javax
.xml
.transform
.Source
;
12 import javax
.xml
.transform
.Transformer
;
13 import javax
.xml
.transform
.TransformerFactory
;
14 import javax
.xml
.transform
.dom
.DOMSource
;
15 import javax
.xml
.transform
.stream
.StreamResult
;
17 import org
.apache
.commons
.logging
.Log
;
18 import org
.apache
.commons
.logging
.LogFactory
;
19 import org
.w3c
.dom
.DOMException
;
20 import org
.w3c
.dom
.Document
;
21 import org
.w3c
.dom
.DocumentFragment
;
22 import org
.w3c
.dom
.NamedNodeMap
;
23 import org
.w3c
.dom
.Node
;
24 import org
.w3c
.dom
.NodeList
;
25 import org
.xml
.sax
.InputSource
;
27 public class FormattedTextAdapter
extends XmlAdapter
<FormattedText
,java
.lang
.String
> {
28 public static String
[] NAMESPACE_PREFIXES
= {"xmlns:common",
41 private static final Log log
= LogFactory
.getLog(FormattedTextAdapter
.class);
43 public FormattedText
marshal(String string
) throws Exception
{
45 String documentString
= "<?xml version=\"1.0\"?><text>" + string
+ "</text>";
46 log
.debug("Parsing " + documentString
);
47 FormattedText text
= new FormattedText();
48 DocumentBuilderFactory factory
= DocumentBuilderFactory
.newInstance();
49 DocumentBuilder parser
= factory
.newDocumentBuilder();
50 Document document
= parser
.parse(new InputSource(new StringReader(documentString
)));
51 NodeList childNodes
= document
.getDocumentElement().getChildNodes();
52 for(int i
= 0; i
< childNodes
.getLength(); i
++) {
53 Node node
= childNodes
.item(i
);
54 if(node
instanceof org
.w3c
.dom
.Text
) {
55 org
.w3c
.dom
.Text textNode
= (org
.w3c
.dom
.Text
) node
;
57 text
.getContent().add(textNode
.getTextContent());
59 text
.getContent().add(node
);
67 public String
unmarshal(FormattedText text
) throws Exception
{
68 DocumentBuilderFactory factory
= DocumentBuilderFactory
.newInstance();
69 Document document
= factory
.newDocumentBuilder().newDocument();
70 DocumentFragment documentFragment
= document
.createDocumentFragment();
72 for(Object object
: text
.getContent()) {
73 if(object
instanceof String
) {
74 String string
= (String
)object
;
75 documentFragment
.appendChild(document
.createTextNode(string
));
77 Node node
= (Node
)object
;
78 NamedNodeMap attributes
= node
.getAttributes();
79 for(String prefix
: FormattedTextAdapter
.NAMESPACE_PREFIXES
) {
81 attributes
.removeNamedItem(prefix
);
82 } catch(DOMException de
){
83 if(de
.code
!= DOMException
.NOT_FOUND_ERR
) {
90 documentFragment
.appendChild(document
.importNode(node
,true));
95 TransformerFactory transformerFactory
= TransformerFactory
.newInstance();
96 Transformer transformer
= transformerFactory
.newTransformer();
97 transformer
.setOutputProperty(OutputKeys
.OMIT_XML_DECLARATION
, "yes");
99 Source input
= new DOMSource(documentFragment
);
100 StringWriter stringWriter
= new StringWriter();
101 Result output
= new StreamResult(stringWriter
);
102 transformer
.transform(input
, output
);
103 return stringWriter
.toString();