cleanup
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / jaxb / FormattedTextAdapter.java
1 /**
2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9
10 package eu.etaxonomy.cdm.jaxb;
11
12 import java.io.StringReader;
13 import java.io.StringWriter;
14
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;
25
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;
36
37 public class FormattedTextAdapter extends XmlAdapter<FormattedText,java.lang.String> {
38 public static String[] NAMESPACE_PREFIXES = {"xmlns:common",
39 "xmlns:agent",
40 "xmlns:description",
41 "xmlns:location",
42 "xmlns:media",
43 "xmlns:molecular",
44 "xmlns:name",
45 "xmlns:occurence",
46 "xmlns:reference",
47 "xmlns:taxon",
48 "xmlns:view",
49 "xmlns:xsi"};
50
51 @SuppressWarnings("unused")
52 private static final Log logger = LogFactory.getLog(FormattedTextAdapter.class);
53
54 public FormattedText marshal(String string) throws Exception {
55 if(string != null) {
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;
68
69 text.getContent().add(textNode.getTextContent());
70 } else {
71 text.getContent().add(node);
72 }
73 }
74 return text;
75 }
76 return null;
77 }
78
79 public String unmarshal(FormattedText text) throws Exception {
80 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
81 Document document = factory.newDocumentBuilder().newDocument();
82 DocumentFragment documentFragment = document.createDocumentFragment();
83
84 for(Object object : text.getContent()) {
85 if(object instanceof String) {
86 String string = (String)object;
87 documentFragment.appendChild(document.createTextNode(string));
88 } else {
89 Node node = (Node)object;
90 NamedNodeMap attributes = node.getAttributes();
91 for(String prefix : FormattedTextAdapter.NAMESPACE_PREFIXES) {
92 try{
93 attributes.removeNamedItem(prefix);
94 } catch(DOMException de){
95 if(de.code != DOMException.NOT_FOUND_ERR) {
96 throw de;
97 }
98 }
99
100 }
101
102 documentFragment.appendChild(document.importNode(node,true));
103 }
104
105 }
106
107 TransformerFactory transformerFactory = TransformerFactory.newInstance();
108 Transformer transformer = transformerFactory.newTransformer();
109 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION , "yes");
110
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);
117 return result;
118 }
119 }