| 1 |
|
package yarfraw.utils; |
| 2 |
|
|
| 3 |
|
import java.io.ByteArrayOutputStream; |
| 4 |
|
import java.io.IOException; |
| 5 |
|
import java.io.InputStream; |
| 6 |
|
import java.io.StringReader; |
| 7 |
|
import java.util.ArrayList; |
| 8 |
|
import java.util.List; |
| 9 |
|
|
| 10 |
|
import javax.xml.namespace.QName; |
| 11 |
|
import javax.xml.parsers.DocumentBuilderFactory; |
| 12 |
|
import javax.xml.parsers.ParserConfigurationException; |
| 13 |
|
import javax.xml.transform.Result; |
| 14 |
|
import javax.xml.transform.Source; |
| 15 |
|
import javax.xml.transform.Transformer; |
| 16 |
|
import javax.xml.transform.TransformerConfigurationException; |
| 17 |
|
import javax.xml.transform.TransformerException; |
| 18 |
|
import javax.xml.transform.TransformerFactory; |
| 19 |
|
import javax.xml.transform.stream.StreamResult; |
| 20 |
|
import javax.xml.transform.stream.StreamSource; |
| 21 |
|
|
| 22 |
|
import org.apache.commons.collections.CollectionUtils; |
| 23 |
|
import org.apache.commons.lang.ObjectUtils; |
| 24 |
|
import org.apache.commons.lang.StringUtils; |
| 25 |
|
import org.w3c.dom.Document; |
| 26 |
|
import org.w3c.dom.Element; |
| 27 |
|
import org.w3c.dom.NamedNodeMap; |
| 28 |
|
import org.w3c.dom.Node; |
| 29 |
|
import org.w3c.dom.NodeList; |
| 30 |
|
import org.xml.sax.InputSource; |
| 31 |
|
import org.xml.sax.SAXException; |
| 32 |
|
|
| 33 |
|
import yarfraw.core.datamodel.YarfrawException; |
| 34 |
|
|
| 35 |
|
public class XMLUtils{ |
| 36 |
0 |
private XMLUtils(){} |
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
public static Document parseXml(String xml, boolean validating, boolean ignoringComments) |
| 49 |
|
throws SAXException, IOException, ParserConfigurationException{ |
| 50 |
120 |
return parseXml(new InputSource(new StringReader(xml)), validating, ignoringComments); |
| 51 |
|
} |
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
public static Document parseXml(InputStream stream, boolean validating, boolean ignoringComments) |
| 65 |
|
throws SAXException, IOException, ParserConfigurationException{ |
| 66 |
0 |
return parseXml(new InputSource(stream), validating, ignoringComments); |
| 67 |
|
} |
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
|
| 76 |
|
|
| 77 |
|
|
| 78 |
|
|
| 79 |
|
public static Document parseXml(InputSource source, boolean validating, boolean ignoringComments) |
| 80 |
|
throws SAXException, IOException, ParserConfigurationException{ |
| 81 |
120 |
DocumentBuilderFactory factory = |
| 82 |
|
DocumentBuilderFactory.newInstance(); |
| 83 |
120 |
factory.setValidating(validating); |
| 84 |
120 |
factory.setIgnoringComments(ignoringComments); |
| 85 |
120 |
factory.setNamespaceAware(true); |
| 86 |
120 |
return factory.newDocumentBuilder().parse(source); |
| 87 |
|
} |
| 88 |
|
|
| 89 |
|
|
| 90 |
|
|
| 91 |
|
|
| 92 |
|
|
| 93 |
|
|
| 94 |
|
|
| 95 |
|
|
| 96 |
|
|
| 97 |
|
|
| 98 |
|
public static Element getElementByNS(List<Element> elements, String namespaceURI, String localName){ |
| 99 |
78 |
if(CollectionUtils.isEmpty(elements)){ |
| 100 |
0 |
return null; |
| 101 |
|
} |
| 102 |
78 |
for(Element e : elements){ |
| 103 |
141 |
if(ObjectUtils.equals(localName, e.getLocalName()) && |
| 104 |
|
ObjectUtils.equals(namespaceURI, emptyIfNull(e.getNamespaceURI()))){ |
| 105 |
78 |
return e; |
| 106 |
|
} |
| 107 |
|
} |
| 108 |
0 |
return null; |
| 109 |
|
} |
| 110 |
|
|
| 111 |
|
|
| 112 |
|
|
| 113 |
|
|
| 114 |
|
|
| 115 |
|
|
| 116 |
|
|
| 117 |
|
|
| 118 |
|
|
| 119 |
|
public static Element getElementByLocalName(List<Element> elements, String localName){ |
| 120 |
12 |
if(CollectionUtils.isEmpty(elements)){ |
| 121 |
0 |
return null; |
| 122 |
|
} |
| 123 |
12 |
for(Element e : elements){ |
| 124 |
12 |
if(ObjectUtils.equals(localName, e.getLocalName())){ |
| 125 |
12 |
return e; |
| 126 |
|
} |
| 127 |
|
} |
| 128 |
0 |
return null; |
| 129 |
|
} |
| 130 |
|
|
| 131 |
|
public static boolean same(QName qn1, QName qn2){ |
| 132 |
244056 |
return ObjectUtils.equals(qn1, qn2); |
| 133 |
|
} |
| 134 |
|
|
| 135 |
|
|
| 136 |
|
|
| 137 |
|
|
| 138 |
|
|
| 139 |
|
|
| 140 |
|
|
| 141 |
|
public static String emptyIfNull(String str){ |
| 142 |
78 |
return str == null?StringUtils.EMPTY:str; |
| 143 |
|
} |
| 144 |
|
|
| 145 |
|
|
| 146 |
|
|
| 147 |
|
|
| 148 |
|
|
| 149 |
|
|
| 150 |
|
|
| 151 |
|
|
| 152 |
|
public static String getAttributeValue(Node element, QName name){ |
| 153 |
0 |
return getAttributeValue(element, name.getLocalPart(), name.getNamespaceURI()); |
| 154 |
|
} |
| 155 |
|
|
| 156 |
|
|
| 157 |
|
|
| 158 |
|
|
| 159 |
|
|
| 160 |
|
|
| 161 |
|
|
| 162 |
|
public static String getAttributeValue(Node element, String attributeLocalName){ |
| 163 |
0 |
return getAttributeValue(element, attributeLocalName, null); |
| 164 |
|
} |
| 165 |
|
|
| 166 |
|
|
| 167 |
|
|
| 168 |
|
|
| 169 |
|
|
| 170 |
|
|
| 171 |
|
public static String getAttributeValue(Node element, String attributeLocalName, String namespaceUri){ |
| 172 |
0 |
NamedNodeMap attr = element.getAttributes(); |
| 173 |
0 |
if(attr == null){ |
| 174 |
0 |
return null; |
| 175 |
|
} |
| 176 |
0 |
for(int i=0; i<attr.getLength(); i++){ |
| 177 |
0 |
Node a = attr.item(i); |
| 178 |
0 |
boolean sameUri = namespaceUri == null? true: namespaceUri.equals(a.getNamespaceURI()); |
| 179 |
0 |
if(sameUri && attributeLocalName.equals(a.getLocalName())){ |
| 180 |
0 |
return a.getNodeValue(); |
| 181 |
|
} |
| 182 |
|
} |
| 183 |
0 |
return null; |
| 184 |
|
} |
| 185 |
|
|
| 186 |
|
|
| 187 |
|
|
| 188 |
|
|
| 189 |
|
|
| 190 |
|
|
| 191 |
|
|
| 192 |
|
public static QName getQName(Node n){ |
| 193 |
0 |
return new QName(emptyIfNull(n.getNamespaceURI()), n.getLocalName()); |
| 194 |
|
} |
| 195 |
|
|
| 196 |
|
|
| 197 |
|
|
| 198 |
|
|
| 199 |
|
|
| 200 |
|
|
| 201 |
|
|
| 202 |
|
|
| 203 |
|
public static List<Node> getChildrenNodesByName(Node parent, String localName){ |
| 204 |
0 |
return getChildrenByName(parent, localName, false); |
| 205 |
|
} |
| 206 |
|
|
| 207 |
|
|
| 208 |
|
|
| 209 |
|
|
| 210 |
|
|
| 211 |
|
|
| 212 |
|
|
| 213 |
|
public static Node getChildrenNodeByName(Node parent, String localName){ |
| 214 |
0 |
List<Node> result = getChildrenByName(parent, localName, true); |
| 215 |
0 |
return result.size() > 0? result.get(0) : null; |
| 216 |
|
} |
| 217 |
|
|
| 218 |
|
|
| 219 |
|
public static String transformWithXsl(String xslt, String xml) throws YarfrawException{ |
| 220 |
0 |
Source source = new StreamSource(xml); |
| 221 |
0 |
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 222 |
0 |
Result res = new StreamResult(out); |
| 223 |
0 |
TransformerFactory transFact = TransformerFactory.newInstance(); |
| 224 |
|
Transformer trans; |
| 225 |
|
|
| 226 |
|
try { |
| 227 |
0 |
trans = transFact.newTransformer(new StreamSource(xslt)); |
| 228 |
0 |
trans.transform(source, res); |
| 229 |
0 |
} catch (TransformerConfigurationException e) { |
| 230 |
0 |
throw new YarfrawException("Transformer config exception", e); |
| 231 |
0 |
} catch (TransformerException e) { |
| 232 |
0 |
throw new YarfrawException("Transform exception", e); |
| 233 |
0 |
} |
| 234 |
|
|
| 235 |
0 |
return out.toString(); |
| 236 |
|
} |
| 237 |
|
|
| 238 |
|
private static List<Node> getChildrenByName(Node parent, String localName, boolean single){ |
| 239 |
0 |
List<Node> nodes = new ArrayList<Node>(); |
| 240 |
0 |
NodeList list = parent.getChildNodes(); |
| 241 |
0 |
for(int i =0; i< list.getLength(); i++){ |
| 242 |
0 |
if(localName.equals(list.item(i).getLocalName())){ |
| 243 |
0 |
nodes.add(list.item(i)); |
| 244 |
0 |
if(single){ |
| 245 |
0 |
return nodes; |
| 246 |
|
} |
| 247 |
|
} |
| 248 |
|
} |
| 249 |
0 |
return nodes; |
| 250 |
|
} |
| 251 |
|
|
| 252 |
|
} |