1 19 20 package org.openharmonise.commons.xml; 21 22 import org.w3c.dom.*; 23 import org.xml.sax.*; 24 import org.xml.sax.InputSource ; 25 26 import java.io.*; 27 import java.util.*; 28 import java.util.List ; 29 import java.util.logging.*; 30 import java.util.logging.Level ; 31 32 import javax.xml.parsers.*; 33 import javax.xml.parsers.DocumentBuilderFactory ; 34 35 42 43 public class XMLUtils { 44 45 private static final Logger m_logger = Logger.getLogger(XMLUtils.class.getName()); 46 47 48 56 public static String getElementName(Element el) { 57 String sElName = el.getLocalName(); 58 59 if(sElName == null) { 60 sElName = el.getTagName(); 61 } 62 63 return sElName; 64 } 65 66 72 static public String getChildTextValue(Element el) { 73 String sVal = null; 74 75 if(el != null) { 76 Node node = el.getFirstChild(); 77 78 if(node instanceof Text) { 79 sVal = node.getNodeValue(); 80 } 81 } 82 83 return sVal; 84 } 85 86 95 public static Element getFirstNamedChild(Element el, String sName) { 96 return (Element) XMLUtils.getFirstNamedChild((Node) el, sName); 97 } 98 99 108 public static Node getFirstNamedChild(Node node, String sName) { 109 Node retn = null; 110 String sElName = sName; 111 int nIndex = sName.indexOf(":"); 112 if (nIndex > -1) { 113 sElName = sName.substring(nIndex + 1); 114 } 115 116 NodeList nl = node.getChildNodes(); 117 for (int i = 0; i < nl.getLength() && retn == null; i++) { 118 Node currNode = nl.item(i); 119 if (currNode instanceof Element) { 120 String sNodeName = getElementName((Element) currNode); 121 122 if (sNodeName.equals(sElName)) { 123 retn = currNode; 124 } 125 } 126 } 127 128 return retn; 129 } 130 131 138 public static Element getFirstElementChild(Element el) { 139 Element elRetn = null; 140 141 NodeList nl = el.getChildNodes(); 142 for (int i = 0; i < nl.getLength(); i++) { 143 if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) { 144 elRetn = (Element) nl.item(i); 145 break; 146 } 147 } 148 149 return elRetn; 150 } 151 152 159 public static File fixXMLFileEntities(File file) { 160 File newFile = null; 161 BufferedWriter wBuff = null; 162 BufferedReader rBuff = null; 163 try { 164 newFile = File.createTempFile("sim-", "tmp"); 165 wBuff = new BufferedWriter(new FileWriter(newFile)); 166 rBuff = new BufferedReader(new FileReader(file)); 167 168 while (rBuff.ready()) { 169 String sLine = rBuff.readLine(); 170 if (sLine.indexOf("&") > -1) { 171 String sFixed = XMLUtils.encodeXMLText(sLine); 172 wBuff.write(sFixed); 173 } else { 174 wBuff.write(sLine); 175 } 176 } 177 } catch (Exception e) { 178 m_logger.log(Level.WARNING, e.getMessage(), e); 179 } finally { 180 try { 181 wBuff.close(); 182 rBuff.close(); 183 } catch (Exception ex) { 184 m_logger.log(Level.WARNING, ex.getMessage(), ex); 185 } 186 } 187 return newFile; 188 } 189 190 197 private static String encodeXMLText(String sText) { 198 StringBuffer sBuff2 = new StringBuffer (sText); 199 StringBuffer sNewBuff = new StringBuffer (); 200 201 for (int i = 0; i < sBuff2.length(); i++) { 202 char currChar = sBuff2.charAt(i); 203 if (currChar == '&') { 204 if (sBuff2.charAt(i + 1) != 'a') { 205 sNewBuff.append(currChar); 206 } else if (sBuff2.charAt(i + 2) != 'p') { 207 sNewBuff.append(currChar); 208 } else if ( 209 sBuff2.charAt(i + 1) == 'a' 210 && sBuff2.charAt(i + 2) == 'm' 211 && sBuff2.charAt(i + 3) == 'p' 212 && sBuff2.charAt(i + 4) == ';' 213 && sBuff2.charAt(i + 5) == 'a' 214 && sBuff2.charAt(i + 6) == 'm' 215 && sBuff2.charAt(i + 7) == 'p' 216 && sBuff2.charAt(i + 8) == ';') { 217 i = i + 8; 218 sNewBuff.append("&amp;"); 219 } else if ( 220 sBuff2.charAt(i + 1) == 'a' 221 && sBuff2.charAt(i + 2) == 'm' 222 && sBuff2.charAt(i + 3) == 'p' 223 && sBuff2.charAt(i + 4) == ';') { 224 i = i + 4; 225 sNewBuff.append("&amp;"); 226 } else { 227 sNewBuff.append("&amp;"); 228 } 229 } else { 230 sNewBuff.append(currChar); 231 } 232 } 233 234 return sNewBuff.toString(); 235 } 236 237 246 public static List getChildrenByName(Element propEl, String sName) { 247 NodeList nodes = propEl.getChildNodes(); 248 List result = new ArrayList(); 249 int nIndex = sName.indexOf(":"); 250 251 if(nIndex > 0) { 252 sName = sName.substring(nIndex + 1); 253 } 254 255 for (int i = 0; i < nodes.getLength(); i++) { 256 Node tmpNode = nodes.item(i); 257 if (tmpNode.getNodeType() != Node.ELEMENT_NODE) { 258 continue; 259 } 260 261 if (getElementName((Element) tmpNode).equals(sName)) { 262 result.add(tmpNode); 263 } 264 } 265 266 return result; 267 } 268 269 276 public static Node copyNode(Node originalNode, Document parent_destination) { 277 int i = 0; 278 279 Node returnNode = null; 280 281 if (originalNode.getNodeType() == Node.ELEMENT_NODE) { 282 Element el = parent_destination.createElement(((Element) originalNode).getTagName()); 283 NamedNodeMap attribs = originalNode.getAttributes(); 284 285 for (i = 0; i < attribs.getLength(); i++) { 286 Attr nextAtt = (Attr) attribs.item(i); 287 el.setAttribute(nextAtt.getNodeName(), nextAtt.getValue()); 288 } 289 290 NodeList nodes = originalNode.getChildNodes(); 291 292 for (i = 0; i < nodes.getLength(); i++) { 293 if ((nodes.item(i).getNodeType() == Node.ELEMENT_NODE) 294 || (nodes.item(i).getNodeType() == Node.TEXT_NODE)) { 295 el.appendChild(copyNode(nodes.item(i),parent_destination)); 296 } 297 } 298 299 returnNode = (Node) el; 300 } else if (originalNode.getNodeType() == Node.TEXT_NODE) { 301 Text el = parent_destination.createTextNode(originalNode.getNodeValue()); 302 303 returnNode = (Node) el; 304 } 305 306 return returnNode; 307 } 308 309 317 public static void copyChildren( 318 Element parent_destination, 319 Element parent_source, 320 Document doc_destination) { 321 copyChildren(parent_destination, parent_source, doc_destination,new Vector()); 322 } 323 324 333 public static void copyChildren( 334 Element parent_destination, 335 Element parent_source, 336 Document doc_destination, 337 Vector ignoreTags) { 338 NodeList child_nodes = parent_source.getChildNodes(); 340 341 for (int k = 0; k < child_nodes.getLength(); k++) { 342 if ((child_nodes.item(k).getNodeType() == Node.ELEMENT_NODE) 343 && ignoreTags.contains( 344 ((Element) child_nodes.item(k)).getTagName())) { 345 continue; 346 } 347 348 Node node = child_nodes.item(k); 349 350 if (node != null) { 351 parent_destination.appendChild(copyNode(node,doc_destination)); 352 } 353 } 354 } 355 356 365 public static Document getDocumentFromString(String sVal) throws SAXException, IOException, ParserConfigurationException { 366 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 367 factory.setNamespaceAware(true); 368 369 StringReader sreader = new StringReader(sVal); 370 371 InputSource isource = new InputSource (sreader); 372 373 return factory.newDocumentBuilder().parse(isource); 374 } 375 } | Popular Tags |