package javaxt.xml; import java.io.ByteArrayOutputStream; import java.io.ByteArrayInputStream; import java.io.InputStream; import org.w3c.dom.*; import javax.xml.parsers.*; //imports used to transform xml import javax.xml.transform.*; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; //****************************************************************************** //** DOM Utilities //****************************************************************************** /** * Provides basic utilities to simplify loading and parsing xml * ******************************************************************************/ public class DOM { //************************************************************************** //** Private Constructor //************************************************************************** /** Defeats Instantiation */ private DOM() { } //************************************************************************** //** createDocument //************************************************************************** /** Used to create a DOM document from a URL. */ public static Document createDocument(java.net.URL url){ return new javaxt.http.Request(url).getResponse().getXML(); } //************************************************************************** //** createDocument //************************************************************************** /** Used to create a DOM document from a java.io.File. Returns null if the * file cannot be serialized to XML. */ public static Document createDocument(java.io.File file){ if (file.exists()==false) return null; try{ return createDocument(new java.io.FileInputStream(file)); } catch(Exception e){ //e.printStackTrace(); return null; } } //************************************************************************** //** createDocument //************************************************************************** /** Used to create a DOM document from a String. Returns null if the string * is invalid. */ public static Document createDocument(String xml){ if (xml==null) return null; xml = xml.trim(); String encoding = "UTF-8"; try{ String xmlHeader = xml.substring(xml.indexOf("")); if (xmlHeader.contains(" encoding")){ encoding = xmlHeader.substring(xmlHeader.indexOf(" encoding")+" encoding".length()); encoding = encoding.substring(encoding.indexOf("=")+1); while(encoding.substring(0, 1).equals(" ")){ encoding = encoding.substring(1); } String firstChar = encoding.substring(0, 1); if (firstChar.equals("\"") || firstChar.equals("'")){ encoding = encoding.substring(1); encoding = encoding.substring(0, encoding.indexOf(firstChar)).trim(); } else{ encoding = encoding.substring(0, encoding.indexOf(" ")).trim(); } } } catch(Exception e){} return createDocument(xml, encoding); } public static Document createDocument(String xml, String charsetName){ xml = xml.trim(); try{ return createDocument(new ByteArrayInputStream(xml.getBytes(charsetName))); } catch(Exception e){ //e.printStackTrace(); return createDocument(new ByteArrayInputStream(xml.getBytes())); } } //************************************************************************** //** createDocument //************************************************************************** /** Used to create a DOM document from an InputStream. */ public static Document createDocument(InputStream is){ try{ DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = builderFactory.newDocumentBuilder(); return builder.parse(is); } catch(Exception e){ //e.printStackTrace(); return null; } } //************************************************************************** //** getOuterNode //************************************************************************** /** Returns the outer node for a given xml document. * @param xml A org.w3c.dom.Document */ public static Node getOuterNode(Document xml){ if (xml==null) return null; NodeList OuterNodes = xml.getChildNodes(); for (int i=0; i"); ret.append(getNodeValue(node)); ret.append(""); } } return ret.toString(); } //************************************************************************** //** getAttributes //************************************************************************** /** Used to retrieve all of the attributes for a given node. */ private static String getAttributes(Node node){ if (node==null) return ""; NamedNodeMap attr = node.getAttributes(); String Attributes = ""; if (attr!=null){ for (int j=0; j"); if (hasChildren(tree)) { NodeList xmlNodeList = tree.getChildNodes(); for (int i=0; i"); } } //************************************************************************** //** getDocumentAttributes //************************************************************************** /** * @param xml A org.w3c.dom.Document */ public static NamedNodeMap getDocumentAttributes(Document xml){ NodeList Definitions = xml.getChildNodes(); for (int i=0; i getNameSpaces(Document xml){ java.util.HashMap namespaces = new java.util.HashMap(); getNameSpaces(getOuterNode(xml), namespaces); return namespaces; } private static void getNameSpaces(Node node, java.util.HashMap namespaces){ if (node.getNodeType()==1){ NamedNodeMap attr = node.getAttributes(); if (attr!=null){ for (int j=0; j nodes = new java.util.ArrayList(); getElementsByTagName(tagName, node, nodes); return nodes.toArray(new org.w3c.dom.Node[nodes.size()]); } private static void getElementsByTagName(String tagName, Node node, java.util.ArrayList nodes){ if (node!=null && node.getNodeType()==1){ String nodeName = node.getNodeName().trim(); if (nodeName.contains(":") && !tagName.contains(":")){ nodeName = nodeName.substring(nodeName.indexOf(":")+1); } if (nodeName.equalsIgnoreCase(tagName)){ nodes.add(node); } if (hasChildren(node)) { NodeList childNodes = node.getChildNodes(); for (int i=0; i nodes = new java.util.ArrayList(); for (int i=0; i ESCAPE_STRINGS = java.util.Collections.unmodifiableList(java.util.Arrays.asList(new String[] { "<" , ">" , "&" , """ , "'" })); private static String UNICODE_LOW = "" + ((char)0x20); //space private static String UNICODE_HIGH = "" + ((char)0x7f); //************************************************************************** //** escapeXml //************************************************************************** /** Used to encode text data for use in an XML tag or attribute. */ public static String escapeXml(String content) { String result = content; if ((content != null) && (content.length() > 0)) { boolean modified = false; StringBuilder stringBuilder = new StringBuilder(content.length()); for (int i = 0, count = content.length(); i < count; ++i) { String character = content.substring(i, i + 1); int pos = ESCAPE_CHARS.indexOf(character); if (pos > -1) { stringBuilder.append(ESCAPE_STRINGS.get(pos)); modified = true; } else { if ( (character.compareTo(UNICODE_LOW) > -1) && (character.compareTo(UNICODE_HIGH) < 1) ) { stringBuilder.append(character); } else { stringBuilder.append("&#" + ((int)character.charAt(0)) + ";"); modified = true; } } } if (modified) { result = stringBuilder.toString(); } } return result; } }