View Javadoc

1   /*
2   	Copyright (c) 2009 Olivier Chafik, All Rights Reserved
3   	
4   	This file is part of JNAerator (http://jnaerator.googlecode.com/).
5   	
6   	JNAerator is free software: you can redistribute it and/or modify
7   	it under the terms of the GNU General Public License as published by
8   	the Free Software Foundation, either version 3 of the License, or
9   	(at your option) any later version.
10  	
11  	JNAerator is distributed in the hope that it will be useful,
12  	but WITHOUT ANY WARRANTY; without even the implied warranty of
13  	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  	GNU General Public License for more details.
15  	
16  	You should have received a copy of the GNU General Public License
17  	along with JNAerator.  If not, see <http://www.gnu.org/licenses/>.
18  */
19  package com.ochafik.xml;
20  import java.io.File;
21  import java.io.StringWriter;
22  import java.text.MessageFormat;
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.LinkedList;
26  import java.util.List;
27  import java.util.regex.Pattern;
28  
29  import javax.xml.parsers.DocumentBuilder;
30  import javax.xml.parsers.DocumentBuilderFactory;
31  import javax.xml.transform.Transformer;
32  import javax.xml.transform.TransformerFactory;
33  import javax.xml.transform.dom.DOMSource;
34  import javax.xml.transform.stream.StreamResult;
35  
36  import org.w3c.dom.Document;
37  import org.w3c.dom.NamedNodeMap;
38  import org.w3c.dom.Node;
39  import org.w3c.dom.NodeList;
40  
41  import com.ochafik.util.listenable.Adapter;
42  import com.ochafik.util.string.RegexUtils;
43  //import ochafik.babel.*;
44  public class XMLUtils  { 
45  	public static String getAttribute(Node node, String name) {
46  		NamedNodeMap m=node.getAttributes();
47  		if (m==null) return null;
48  		Node att=m.getNamedItem(name);
49  		if (att==null) return null;
50  		return att.getNodeValue();
51  	}
52  	public static Node getFirstNamedNode(Node node, String name) {
53  		if (node.getNodeName().equalsIgnoreCase(name)) return node;
54  		else {
55  			NodeList list=node.getChildNodes();
56  			for (int i=0,len=list.getLength();i<len;i++) {
57  				Node n=getFirstNamedNode(list.item(i),name);
58  				if (n!=null) return n;
59  			}
60  			return null;
61  		}
62  	}
63  	public static List<Node> getByName(Node node, String name) {
64  		List<Node> result = new LinkedList<Node>();
65  		getByName(node, name, result);
66  		return result;
67  	}
68  	
69  	private static void getByName(Node node, String name, List<Node> result) {
70  		if (node.getNodeName().equalsIgnoreCase(name)) {
71  			result.add(node);
72  		} else {
73  			NodeList list=node.getChildNodes();
74  			for (int i=0,len=list.getLength();i<len;i++) {
75  				getByName(list.item(i), name, result);
76  			}
77  		}
78  	}
79  	public static Collection<Node> getChildrenByName(Node node, String name) {
80  		Collection<Node> nodes=new ArrayList<Node>();
81  		getChildrenByName(node,name,nodes);
82  		return nodes;
83  	}
84  	private static void getChildrenByName(Node node, String name, Collection<Node> nodes) {
85  		NodeList list=node.getChildNodes();
86  		for (int i=0,len=list.getLength();i<len;i++) {
87  			Node child = list.item(i);
88  			if (child.getNodeName().equalsIgnoreCase(name)) {
89  				nodes.add(child);
90  			}
91  		}
92  	}
93  	public static List<Node> getChildElements(Node node) {
94  		NodeList children = node.getChildNodes();
95  		int childCount = children.getLength();
96  		List<Node> nodes = new ArrayList<Node>(childCount);
97  		for (int i = 0; i < childCount; i++) {
98  			Node child = children.item(i);
99  			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("&","&amp;").replace("<","&lt;");//.replace(">", "&gt;");
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 }