001 /* 002 Copyright (c) 2009 Olivier Chafik, All Rights Reserved 003 004 This file is part of JNAerator (http://jnaerator.googlecode.com/). 005 006 JNAerator is free software: you can redistribute it and/or modify 007 it under the terms of the GNU General Public License as published by 008 the Free Software Foundation, either version 3 of the License, or 009 (at your option) any later version. 010 011 JNAerator is distributed in the hope that it will be useful, 012 but WITHOUT ANY WARRANTY; without even the implied warranty of 013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 GNU General Public License for more details. 015 016 You should have received a copy of the GNU General Public License 017 along with JNAerator. If not, see <http://www.gnu.org/licenses/>. 018 */ 019 package com.ochafik.xml; 020 import java.io.File; 021 import java.io.StringWriter; 022 import java.text.MessageFormat; 023 import java.util.ArrayList; 024 import java.util.Collection; 025 import java.util.LinkedList; 026 import java.util.List; 027 import java.util.regex.Pattern; 028 029 import javax.xml.parsers.DocumentBuilder; 030 import javax.xml.parsers.DocumentBuilderFactory; 031 import javax.xml.transform.Transformer; 032 import javax.xml.transform.TransformerFactory; 033 import javax.xml.transform.dom.DOMSource; 034 import javax.xml.transform.stream.StreamResult; 035 036 import org.w3c.dom.Document; 037 import org.w3c.dom.NamedNodeMap; 038 import org.w3c.dom.Node; 039 import org.w3c.dom.NodeList; 040 041 import com.ochafik.util.listenable.Adapter; 042 import com.ochafik.util.string.RegexUtils; 043 //import ochafik.babel.*; 044 public class XMLUtils { 045 public static String getAttribute(Node node, String name) { 046 NamedNodeMap m=node.getAttributes(); 047 if (m==null) return null; 048 Node att=m.getNamedItem(name); 049 if (att==null) return null; 050 return att.getNodeValue(); 051 } 052 public static Node getFirstNamedNode(Node node, String name) { 053 if (node.getNodeName().equalsIgnoreCase(name)) return node; 054 else { 055 NodeList list=node.getChildNodes(); 056 for (int i=0,len=list.getLength();i<len;i++) { 057 Node n=getFirstNamedNode(list.item(i),name); 058 if (n!=null) return n; 059 } 060 return null; 061 } 062 } 063 public static List<Node> getByName(Node node, String name) { 064 List<Node> result = new LinkedList<Node>(); 065 getByName(node, name, result); 066 return result; 067 } 068 069 private static void getByName(Node node, String name, List<Node> result) { 070 if (node.getNodeName().equalsIgnoreCase(name)) { 071 result.add(node); 072 } else { 073 NodeList list=node.getChildNodes(); 074 for (int i=0,len=list.getLength();i<len;i++) { 075 getByName(list.item(i), name, result); 076 } 077 } 078 } 079 public static Collection<Node> getChildrenByName(Node node, String name) { 080 Collection<Node> nodes=new ArrayList<Node>(); 081 getChildrenByName(node,name,nodes); 082 return nodes; 083 } 084 private static void getChildrenByName(Node node, String name, Collection<Node> nodes) { 085 NodeList list=node.getChildNodes(); 086 for (int i=0,len=list.getLength();i<len;i++) { 087 Node child = list.item(i); 088 if (child.getNodeName().equalsIgnoreCase(name)) { 089 nodes.add(child); 090 } 091 } 092 } 093 public static List<Node> getChildElements(Node node) { 094 NodeList children = node.getChildNodes(); 095 int childCount = children.getLength(); 096 List<Node> nodes = new ArrayList<Node>(childCount); 097 for (int i = 0; i < childCount; i++) { 098 Node child = children.item(i); 099 if (child.getNodeType() == Node.ELEMENT_NODE) { 100 nodes.add(child); 101 } 102 } 103 return nodes; 104 } 105 public static List<Node> getAttributes(Node node) { 106 NamedNodeMap attrs = node.getAttributes(); 107 int attrCount = attrs.getLength(); 108 List<Node> nodes = new ArrayList<Node>(attrCount); 109 for (int i = 0; i < attrCount; i++) { 110 nodes.add(attrs.item(i)); 111 } 112 return nodes; 113 } 114 public static StringBuffer gatherTextPCDATAAndCDATADescendants(Node node,StringBuffer sb,String separator) { 115 if (sb==null) sb=new StringBuffer(); 116 int t=node.getNodeType(); 117 if (t==Node.CDATA_SECTION_NODE || t==Node.TEXT_NODE) { 118 //sb.append("================= "+node.getNodeName()+" ================="); 119 sb.append(node.getNodeValue()); 120 } else { 121 NodeList list = node.getChildNodes(); 122 for (int i=0,len=list.getLength();i<len;i++) { 123 Node n=list.item(i); 124 gatherTextPCDATAAndCDATADescendants(n,sb,separator); 125 if (i<len-1) sb.append(separator); 126 } 127 } 128 return sb; 129 } 130 static TransformerFactory tFactory = TransformerFactory.newInstance(); 131 public static String nodeToString(Node node) { 132 try { 133 Transformer transformer=tFactory.newTransformer(); 134 DOMSource source = new DOMSource(node); 135 StringWriter writer=new StringWriter(); 136 StreamResult result = new StreamResult(writer); 137 transformer.transform(source, result); 138 return writer.toString(); 139 } catch (Exception ex) { 140 ex.printStackTrace(); 141 return null; 142 } 143 } 144 145 public static final String escapeEntities(String s) { 146 return s.replace("&","&").replace("<","<");//.replace(">", ">"); 147 } 148 149 150 private static final Pattern xmlTagPattern=Pattern.compile("<!--.*?-->|</?\\w+[^\">]*(?:(?:\"[^\"]*\")[\">]*)*>"); 151 private static final Pattern spacesPattern=Pattern.compile("\\s+"); 152 private static final MessageFormat spaceMessageFormat=new MessageFormat(" "); 153 154 public static String stripTags(String xmlString) { 155 String strippedHtml = RegexUtils.regexReplace(xmlTagPattern, xmlString, (Adapter<String[], String>)null); 156 return RegexUtils.regexReplace(spacesPattern,strippedHtml,spaceMessageFormat).trim(); 157 } 158 public static Document readXML(File file) throws Exception { 159 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); 160 documentBuilderFactory.setNamespaceAware(true); // never forget this! 161 DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); 162 return documentBuilder.parse(file); 163 } 164 public static List<Node> list(NodeList nodeList) { 165 if (nodeList == null) 166 return null; 167 168 int len = nodeList.getLength(); 169 List<Node> list = new ArrayList<Node>(len); 170 for (int i = 0; i < len; i++) 171 list.add(nodeList.item(i)); 172 return list; 173 } 174 175 176 }