Revision b5f21afa
Added by Andreas Müller almost 2 years ago
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.lang3.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 |
@Override |
|
55 |
public FormattedText marshal(String string) throws Exception { |
|
56 |
if(string != null) { |
|
57 |
string = StringEscapeUtils.escapeXml(string); |
|
58 |
String documentString = "<?xml version=\"1.0\"?><text>" + string + "</text>"; |
|
59 |
//log.debug("Parsing " + documentString); |
|
60 |
FormattedText text = new FormattedText(); |
|
61 |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
|
62 |
DocumentBuilder parser = factory.newDocumentBuilder(); |
|
63 |
Document document = parser.parse(new InputSource(new StringReader(documentString))); |
|
64 |
NodeList childNodes = document.getDocumentElement().getChildNodes(); |
|
65 |
for(int i = 0; i < childNodes.getLength(); i++) { |
|
66 |
Node node = childNodes.item(i); |
|
67 |
if(node instanceof org.w3c.dom.Text ) { |
|
68 |
org.w3c.dom.Text textNode = (org.w3c.dom.Text) node; |
|
69 |
|
|
70 |
text.getContent().add(textNode.getTextContent()); |
|
71 |
} else { |
|
72 |
text.getContent().add(node); |
|
73 |
} |
|
74 |
} |
|
75 |
return text; |
|
76 |
} |
|
77 |
return null; |
|
78 |
} |
|
79 |
|
|
80 |
@Override |
|
81 |
public String unmarshal(FormattedText text) throws Exception { |
|
82 |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
|
83 |
Document document = factory.newDocumentBuilder().newDocument(); |
|
84 |
DocumentFragment documentFragment = document.createDocumentFragment(); |
|
85 |
|
|
86 |
for(Object object : text.getContent()) { |
|
87 |
if(object instanceof String) { |
|
88 |
String string = (String)object; |
|
89 |
documentFragment.appendChild(document.createTextNode(string)); |
|
90 |
} else { |
|
91 |
Node node = (Node)object; |
|
92 |
NamedNodeMap attributes = node.getAttributes(); |
|
93 |
for(String prefix : FormattedTextAdapter.NAMESPACE_PREFIXES) { |
|
94 |
try{ |
|
95 |
attributes.removeNamedItem(prefix); |
|
96 |
} catch(DOMException de){ |
|
97 |
if(de.code != DOMException.NOT_FOUND_ERR) { |
|
98 |
throw de; |
|
99 |
} |
|
100 |
} |
|
101 |
|
|
102 |
} |
|
103 |
|
|
104 |
documentFragment.appendChild(document.importNode(node,true)); |
|
105 |
} |
|
106 |
|
|
107 |
} |
|
108 |
|
|
109 |
TransformerFactory transformerFactory = TransformerFactory.newInstance(); |
|
110 |
Transformer transformer = transformerFactory.newTransformer(); |
|
111 |
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION , "yes"); |
|
112 |
|
|
113 |
Source input = new DOMSource(documentFragment); |
|
114 |
StringWriter stringWriter = new StringWriter(); |
|
115 |
Result output = new StreamResult(stringWriter); |
|
116 |
transformer.transform(input, output); |
|
117 |
String result = stringWriter.toString(); |
|
118 |
result = StringEscapeUtils.unescapeXml(result); |
|
119 |
return result; |
|
120 |
} |
|
121 |
} |
|
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 |
package eu.etaxonomy.cdm.jaxb; |
|
10 |
|
|
11 |
import java.io.StringReader; |
|
12 |
import java.io.StringWriter; |
|
13 |
|
|
14 |
import javax.xml.bind.annotation.adapters.XmlAdapter; |
|
15 |
import javax.xml.parsers.DocumentBuilder; |
|
16 |
import javax.xml.parsers.DocumentBuilderFactory; |
|
17 |
import javax.xml.transform.OutputKeys; |
|
18 |
import javax.xml.transform.Result; |
|
19 |
import javax.xml.transform.Source; |
|
20 |
import javax.xml.transform.Transformer; |
|
21 |
import javax.xml.transform.TransformerFactory; |
|
22 |
import javax.xml.transform.dom.DOMSource; |
|
23 |
import javax.xml.transform.stream.StreamResult; |
|
24 |
|
|
25 |
import org.apache.commons.text.StringEscapeUtils; |
|
26 |
import org.apache.logging.log4j.LogManager; |
|
27 |
import org.apache.logging.log4j.Logger; |
|
28 |
import org.w3c.dom.DOMException; |
|
29 |
import org.w3c.dom.Document; |
|
30 |
import org.w3c.dom.DocumentFragment; |
|
31 |
import org.w3c.dom.NamedNodeMap; |
|
32 |
import org.w3c.dom.Node; |
|
33 |
import org.w3c.dom.NodeList; |
|
34 |
import org.xml.sax.InputSource; |
|
35 |
|
|
36 |
public class FormattedTextAdapter extends XmlAdapter<FormattedText,java.lang.String> { |
|
37 |
public static String[] NAMESPACE_PREFIXES = {"xmlns:common", |
|
38 |
"xmlns:agent", |
|
39 |
"xmlns:description", |
|
40 |
"xmlns:location", |
|
41 |
"xmlns:media", |
|
42 |
"xmlns:molecular", |
|
43 |
"xmlns:name", |
|
44 |
"xmlns:occurence", |
|
45 |
"xmlns:reference", |
|
46 |
"xmlns:taxon", |
|
47 |
"xmlns:view", |
|
48 |
"xmlns:xsi"}; |
|
49 |
|
|
50 |
@SuppressWarnings("unused") |
|
51 |
private static final Logger logger = LogManager.getLogger(); |
|
52 |
|
|
53 |
@Override |
|
54 |
public FormattedText marshal(String string) throws Exception { |
|
55 |
if(string != null) { |
|
56 |
string = StringEscapeUtils .escapeXml11(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 |
@Override |
|
80 |
public String unmarshal(FormattedText text) throws Exception { |
|
81 |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
|
82 |
Document document = factory.newDocumentBuilder().newDocument(); |
|
83 |
DocumentFragment documentFragment = document.createDocumentFragment(); |
|
84 |
|
|
85 |
for(Object object : text.getContent()) { |
|
86 |
if(object instanceof String) { |
|
87 |
String string = (String)object; |
|
88 |
documentFragment.appendChild(document.createTextNode(string)); |
|
89 |
} else { |
|
90 |
Node node = (Node)object; |
|
91 |
NamedNodeMap attributes = node.getAttributes(); |
|
92 |
for(String prefix : FormattedTextAdapter.NAMESPACE_PREFIXES) { |
|
93 |
try{ |
|
94 |
attributes.removeNamedItem(prefix); |
|
95 |
} catch(DOMException de){ |
|
96 |
if(de.code != DOMException.NOT_FOUND_ERR) { |
|
97 |
throw de; |
|
98 |
} |
|
99 |
} |
|
100 |
|
|
101 |
} |
|
102 |
|
|
103 |
documentFragment.appendChild(document.importNode(node,true)); |
|
104 |
} |
|
105 |
|
|
106 |
} |
|
107 |
|
|
108 |
TransformerFactory transformerFactory = TransformerFactory.newInstance(); |
|
109 |
Transformer transformer = transformerFactory.newTransformer(); |
|
110 |
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION , "yes"); |
|
111 |
|
|
112 |
Source input = new DOMSource(documentFragment); |
|
113 |
StringWriter stringWriter = new StringWriter(); |
|
114 |
Result output = new StreamResult(stringWriter); |
|
115 |
transformer.transform(input, output); |
|
116 |
String result = stringWriter.toString(); |
|
117 |
result = StringEscapeUtils.unescapeXml(result); |
|
118 |
return result; |
|
119 |
} |
|
120 |
} |
Also available in: Unified diff
ref #10072, ref #9359, ref #10095 replace commons-logging by jcl-over-slf4j and adapt some loggers to log4j