1 16 17 package org.apache.naming.util; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.io.StringReader ; 22 23 import javax.xml.parsers.DocumentBuilder ; 24 import javax.xml.parsers.DocumentBuilderFactory ; 25 import javax.xml.parsers.ParserConfigurationException ; 26 27 import org.w3c.dom.Document ; 28 import org.w3c.dom.Node ; 29 import org.xml.sax.EntityResolver ; 30 import org.xml.sax.InputSource ; 31 import org.xml.sax.SAXException ; 32 33 35 39 public class DomXml { 40 String file; 41 String name; 42 43 45 47 public void setFile( String file ) { 48 this.file=file; 49 } 50 51 53 public void setName( String name ) { 54 this.name=name; 55 } 56 57 Node domN; 59 60 62 public Node getNode() { 63 return domN; 64 } 65 66 68 public void execute() { 69 try { 70 if( file== null) { 71 log.error("No file attribute"); 72 return; 73 } 74 75 File docF=new File (file); 76 77 Document doc=readXml(docF); 78 if( doc == null ) return; 79 80 domN = doc.getDocumentElement(); 81 if( domN==null ) { 82 log.error("Can't find the root node"); 83 return; 84 } 85 86 } catch( Exception ex ) { 87 ex.printStackTrace(); 88 } 89 } 90 91 private static org.apache.commons.logging.Log log= 92 org.apache.commons.logging.LogFactory.getLog( DomXml.class ); 93 94 95 97 99 public static String getContent(Node n ) { 100 if( n==null ) return null; 101 Node n1=n.getFirstChild(); 102 104 String s1=n1.getNodeValue(); 105 return s1.trim(); 106 } 107 108 110 public static Node getChild( Node parent, String name ) { 111 if( parent==null ) return null; 112 Node first=parent.getFirstChild(); 113 if( first==null ) return null; 114 for (Node node = first; node != null; 115 node = node.getNextSibling()) { 116 if( name.equals( node.getNodeName() ) ) { 118 return node; 119 } 120 } 121 return null; 122 } 123 124 126 public static String getChildContent( Node parent, String name ) { 127 Node first=parent.getFirstChild(); 128 if( first==null ) return null; 129 for (Node node = first; node != null; 130 node = node.getNextSibling()) { 131 if( name.equals( node.getNodeName() ) ) { 133 return getContent( node ); 134 } 135 } 136 return null; 137 } 138 139 141 public static Node getNext( Node current ) { 142 Node first=current.getNextSibling(); 143 String name=current.getNodeName(); 144 if( first==null ) return null; 145 for (Node node = first; node != null; 146 node = node.getNextSibling()) { 147 if( name.equals( node.getNodeName() ) ) { 149 return node; 150 } 151 } 152 return null; 153 } 154 155 public static class NullResolver implements EntityResolver { 156 public InputSource resolveEntity (String publicId, 157 String systemId) 158 throws SAXException , IOException 159 { 160 if( log.isTraceEnabled()) 161 log.trace("ResolveEntity: " + publicId + " " + systemId); 162 return new InputSource (new StringReader ("")); 163 } 164 } 165 166 public void saveXml( Node n, File xmlF ) { 167 168 } 169 170 public static Document readXml(File xmlF) 171 throws SAXException , IOException , ParserConfigurationException 172 { 173 if( ! xmlF.exists() ) { 174 log.error("No xml file " + xmlF ); 175 return null; 176 } 177 DocumentBuilderFactory dbf = 178 DocumentBuilderFactory.newInstance(); 179 180 dbf.setValidating(false); 181 dbf.setIgnoringComments(false); 182 dbf.setIgnoringElementContentWhitespace(true); 183 186 DocumentBuilder db = null; 187 db = dbf.newDocumentBuilder(); 188 db.setEntityResolver( new NullResolver() ); 189 190 192 Document doc = db.parse(xmlF); 193 return doc; 194 } 195 196 } 197 | Popular Tags |