1 19 20 package org.netbeans.core.registry; 21 22 import org.openide.ErrorManager; 23 import org.openide.filesystems.FileLock; 24 import org.openide.filesystems.FileObject; 25 import org.openide.xml.EntityCatalog; 26 import org.openide.xml.XMLUtil; 27 import org.w3c.dom.*; 28 import org.xml.sax.InputSource ; 29 import org.xml.sax.EntityResolver ; 30 import org.xml.sax.SAXException ; 31 32 import javax.xml.parsers.DocumentBuilderFactory ; 33 import javax.xml.parsers.FactoryConfigurationError ; 34 import javax.xml.parsers.ParserConfigurationException ; 35 import java.io.IOException ; 36 import java.io.InputStream ; 37 import java.io.OutputStream ; 38 import java.io.ByteArrayInputStream ; 39 import java.lang.ref.Reference ; 40 import java.lang.ref.WeakReference ; 41 import java.util.Properties ; 42 import javax.xml.parsers.DocumentBuilder ; 43 44 48 public class DocumentUtils { 49 50 private static DocumentBuilderFactory factory; 51 private static DocumentBuilder builder; 52 53 private DocumentUtils() { 54 } 55 56 private static DocumentBuilderFactory getDocumentBuilderFactory() { 57 if (factory == null) { 58 try { 62 String prop = "javax.xml.parsers.DocumentBuilderFactory"; Properties p = System.getProperties(); 64 String old = p.getProperty(prop); 65 try { 66 p.setProperty(prop, "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl"); factory = DocumentBuilderFactory.newInstance(); 68 } finally { 69 if (old != null) { 70 p.setProperty(prop, old); 71 } else { 72 p.remove(prop); 73 } 74 } 75 } catch (FactoryConfigurationError e) { 76 factory = DocumentBuilderFactory.newInstance(); 78 } 79 factory = DocumentBuilderFactory.newInstance(); 80 factory.setNamespaceAware(true); 81 factory.setValidating(false); 82 factory.setAttribute("http://apache.org/xml/properties/dom/document-class-name", "org.apache.xerces.dom.CoreDocumentImpl"); 85 factory.setAttribute("http://apache.org/xml/features/dom/defer-node-expansion", Boolean.FALSE); 86 } 87 return factory; 88 } 89 90 91 private static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException { 92 if (builder == null) { 93 builder = getDocumentBuilderFactory().newDocumentBuilder(); 94 builder.setEntityResolver(EntityCatalog.getDefault()); 95 } 96 return builder; 97 } 98 99 static Document createDocument() { 100 Document doc = null; 101 try { 102 doc = getDocumentBuilder().newDocument(); 103 } catch (ParserConfigurationException ex) { 104 ErrorManager.getDefault().log(ErrorManager.WARNING, "Could not create instance of new dom.Document.\n" + ex.toString()); 105 return null; 106 } 107 return doc; 108 } 109 110 static void writeDocument(FileObject fo, Document doc) throws IOException { 111 FileLock lock = fo.lock(); 112 OutputStream os = fo.getOutputStream(lock); 113 boolean ok = false; 114 try { 115 XMLUtil.write(doc, os, "UTF-8"); ok = true; 117 } finally { 118 os.close(); 119 lock.releaseLock(); 120 if (!ok) { 121 fo.delete(); 123 } 124 } 125 } 126 127 public static String getTextValue(Element element) { 128 StringBuffer sb = new StringBuffer (); 129 NodeList nodes = element.getChildNodes(); 130 for (int i = 0; i < nodes.getLength(); i++) { 131 Node node = nodes.item(i); 132 if (node.getNodeType() == Node.TEXT_NODE) { 133 sb.append(((Text)node).getData()); 134 } 135 if (node.getNodeType() == Node.CDATA_SECTION_NODE) { 136 sb.append(((CDATASection)node).getData()); 137 } 138 } 139 return sb.toString(); 140 } 141 142 final static class DocumentRef { 143 private Reference docRef; 144 private static final Object [] document = new Object [2]; 145 146 Document getDocument(FileObject fo) { 147 assert fo != null; 148 149 Document d = null; 150 synchronized (this) { 151 if (docRef == null || (d = (Document) docRef.get()) == null) { 152 d = getDOM(fo); 153 docRef = new WeakReference (d); 154 } 155 } 156 157 if (d.getDocumentElement() == null) { 159 docRef = null; 160 document[0] = null; 161 document[1] = null; 162 synchronized (this) { 164 if (docRef == null || (d = (Document) docRef.get()) == null) { 165 d = getDOM(fo); 166 docRef = new WeakReference (d); 167 } 168 } 169 if (d.getDocumentElement() == null) { 170 printFile(fo); 171 throw new IllegalStateException ("Cannot parse file " + fo); 172 } 173 174 } 175 176 return d; 177 } 178 179 180 static synchronized Document getDOM(FileObject fo) { 181 Document retVal = (Document) ((document[0] == null) ? null : ((Reference ) (document[0])).get()); 182 if (retVal != null) { 183 FileObject foRef = (FileObject) ((document[1] == null) ? null : ((Reference ) (document[1])).get()); 184 if (foRef != null && foRef == fo) { 185 return retVal; 186 } 187 } 188 try { 189 if (fo.getSize() == 0) { 190 return null; 191 } 192 InputStream is = fo.getInputStream(); 193 try { 194 InputSource iss = new InputSource (is); 195 retVal = XMLUtil.parse(iss, false, true, null, new EntityResolver () { 196 public InputSource resolveEntity(String publicId,String systemId) 197 throws SAXException , IOException { 198 InputSource retVal = EntityCatalog.getDefault().resolveEntity(publicId,systemId); 199 return (retVal != null) ? retVal : new InputSource (new ByteArrayInputStream (new byte[0])); 200 } 201 }); 202 if (retVal != null) { 203 document[0] = new WeakReference (retVal); 204 document[1] = new WeakReference (fo); 205 } 206 return retVal; 207 } catch (Exception e) { 208 if (ErrorManager.getDefault().isLoggable(ErrorManager.INFORMATIONAL)) { 209 ErrorManager.getDefault().log(ErrorManager.WARNING, "Could not parse file [" + fo + "].\n" + e.toString()); 210 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 211 } 212 } finally { 213 is.close(); 214 } 215 } catch (IOException e) { 216 if (ErrorManager.getDefault().isLoggable(ErrorManager.INFORMATIONAL)) { 217 ErrorManager.getDefault().log(ErrorManager.WARNING, "Could not parse file [" + fo + "].\n" + e.toString()); 218 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 219 } 220 } 221 return null; 222 } 223 } 224 225 226 private static void printFile(FileObject f) { 227 try { 228 System.err.println("Printing problem file: " + f); java.io.InputStream in = f.getInputStream(); 230 int ch = 0; 231 while ( (ch = in.read()) != -1) { 232 System.err.write(ch); 233 } 234 in.close(); 235 } catch (java.io.IOException ioe) { 236 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe); 237 } 238 } 239 } 240 | Popular Tags |