forked from jgraph/mxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmxXmlUtils.java
More file actions
123 lines (105 loc) · 2.84 KB
/
mxXmlUtils.java
File metadata and controls
123 lines (105 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* Copyright (c) 2007-2012, JGraph Ltd
*/
package com.mxgraph.util;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
/**
* Contains various XML helper methods for use with mxGraph.
*/
public class mxXmlUtils
{
private static final Logger log = Logger.getLogger(mxXmlUtils.class.getName());
/**
*
*/
private static DocumentBuilder documentBuilder = null;
/**
*
*/
public static DocumentBuilder getDocumentBuilder()
{
if (documentBuilder == null)
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setExpandEntityReferences(false);
dbf.setXIncludeAware(false);
dbf.setValidating(false);
try
{
dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
}
catch (ParserConfigurationException e)
{
log.log(Level.SEVERE, "Failed to set feature", e);
}
try
{
documentBuilder = dbf.newDocumentBuilder();
}
catch (Exception e)
{
log.log(Level.SEVERE, "Failed to construct a document builder", e);
}
}
return documentBuilder;
}
/**
* Returns a new document for the given XML string. External entities and DTDs are ignored.
*
* @param xml
* String that represents the XML data.
* @return Returns a new XML document.
*/
public static Document parseXml(String xml)
{
try
{
return getDocumentBuilder().parse(new InputSource(new StringReader(xml)));
}
catch (Exception e)
{
log.log(Level.SEVERE, "Failed to parse XML", e);
}
return null;
}
/**
* Returns a string that represents the given node.
*
* @param node
* Node to return the XML for.
* @return Returns an XML string.
*/
public static String getXml(Node node)
{
try
{
Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
StreamResult dest = new StreamResult(new StringWriter());
tf.transform(new DOMSource(node), dest);
return dest.getWriter().toString();
}
catch (Exception e)
{
log.log(Level.SEVERE, "Failed to convert XML object to string", e);
}
return "";
}
}