1 19 20 package org.netbeans.api.convertor; 21 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyChangeSupport ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.OutputStream ; 27 import java.util.Properties ; 28 import java.util.Set ; 29 import javax.xml.parsers.DocumentBuilder ; 30 import javax.xml.parsers.DocumentBuilderFactory ; 31 import javax.xml.parsers.FactoryConfigurationError ; 32 import javax.xml.parsers.ParserConfigurationException ; 33 import org.netbeans.modules.convertor.Accessor; 34 import org.netbeans.modules.convertor.ConvertorsPool; 35 import org.netbeans.spi.convertor.Convertor; 36 import org.openide.ErrorManager; 37 import org.openide.xml.EntityCatalog; 38 import org.openide.xml.XMLUtil; 39 import org.w3c.dom.Document ; 40 import org.w3c.dom.Element ; 41 import org.xml.sax.InputSource ; 42 import org.xml.sax.SAXException ; 43 44 54 public final class Convertors { 55 56 57 public static final String CONVERTOR_DESCRIPTORS = "convertorDescriptors"; 59 private java.beans.PropertyChangeSupport support; 60 61 private static Convertors DEFAULT = new Convertors(); 62 63 private static DocumentBuilderFactory factory; 64 private static DocumentBuilder builder; 65 66 static { 68 Accessor.DEFAULT = new AccessorImpl(); 69 } 70 71 private Convertors() { 72 support = new PropertyChangeSupport (this); 73 } 74 75 80 public static Convertors getDefault() { 81 return DEFAULT; 82 } 83 84 85 private static DocumentBuilderFactory getDocumentBuilderFactory() { 86 if (factory == null) { 87 try { 91 String prop = "javax.xml.parsers.DocumentBuilderFactory"; Properties p = System.getProperties(); 93 String old = p.getProperty(prop); 94 try { 95 p.setProperty(prop, "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl"); factory = DocumentBuilderFactory.newInstance(); 97 } finally { 98 if (old != null) { 99 p.setProperty(prop, old); 100 } else { 101 p.remove(prop); 102 } 103 } 104 } catch (FactoryConfigurationError e) { 105 factory = DocumentBuilderFactory.newInstance(); 107 } 108 factory = DocumentBuilderFactory.newInstance(); 109 factory.setNamespaceAware(true); 110 factory.setValidating(false); 111 factory.setAttribute("http://apache.org/xml/properties/dom/document-class-name", "org.apache.xerces.dom.CoreDocumentImpl"); 114 factory.setAttribute("http://apache.org/xml/features/dom/defer-node-expansion", Boolean.FALSE); 115 } 116 return factory; 117 } 118 119 120 private static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException { 121 if (builder == null) { 122 builder = getDocumentBuilderFactory().newDocumentBuilder(); 123 builder.setEntityResolver(EntityCatalog.getDefault()); 124 } 125 return builder; 126 } 127 128 static Document createDocument() { 129 Document doc = null; 130 try { 131 doc = getDocumentBuilder().newDocument(); 132 } catch (ParserConfigurationException ex) { 133 ErrorManager.getDefault().log(ErrorManager.WARNING, "Could not create instance of new dom.Document.\n" + ex.toString()); 134 return null; 135 } 136 return doc; 137 } 138 139 145 public static boolean canRead(Element element) { 146 if (element == null) { 147 throw new IllegalArgumentException ("Element cannot be null."); } 149 return canRead(element.getNamespaceURI(), element.getNodeName()); 150 } 151 152 159 public static boolean canRead(String namespace, String element) { 160 if (namespace == null || element == null) { 161 throw new IllegalArgumentException ("Namespace and element cannot be null."); } 163 return ConvertorsPool.getDefault().getReadConvertor(namespace, element) != null; 164 } 165 166 172 public static boolean canWrite(Object o) { 173 if (o == null) { 174 throw new IllegalArgumentException ("Object cannot be null."); } 176 return ConvertorsPool.getDefault().getWriteConvertor(o) != null; 177 } 178 179 191 public static Object read(Element element) { 192 ConvertorDescriptor cd = ConvertorsPool.getDefault().getReadConvertor(element.getNamespaceURI(), element.getNodeName()); 193 if (cd == null) { 194 throw new ConvertorException("There is no convertor registered "+ "for element with namespace URI "+element.getNamespaceURI()); } 197 return cd.getConvertor().read(element); 198 } 199 200 215 public static Object read(InputStream is) throws SAXException , IOException { 216 InputSource iss = new InputSource (is); 217 Document doc = XMLUtil.parse(iss, false, true, null, null); 218 if (!canRead(doc.getDocumentElement())) { 219 throw new IOException ("Stream cannot be converted. "+ "There is no convertor for element "+doc.getDocumentElement()); } 222 return read(doc.getDocumentElement()); 223 } 224 225 235 public static Element write(Document doc, Object o) { 236 ConvertorDescriptor cd = ConvertorsPool.getDefault().getWriteConvertor(o); 237 if (cd == null) { 238 throw new ConvertorException("There is no convertor registered for instance "+o); } 240 return cd.getConvertor().write(doc, o); 241 } 242 243 254 public static void write(OutputStream os, Object o) throws IOException { 255 Document doc = createDocument(); 256 Element e = write(doc, o); 257 doc.appendChild(e); 258 XMLUtil.write(doc, os, "UTF-8"); } 260 261 267 public Set getConvertorDescriptors() { 268 return ConvertorsPool.getDefault().getDescriptors(); 269 } 270 271 276 public void addPropertyChangeListener(PropertyChangeListener listener) { 277 support.addPropertyChangeListener(listener); 278 } 279 280 285 public void removePropertyChangeListener(PropertyChangeListener listener) { 286 support.removePropertyChangeListener(listener); 287 } 288 289 final static void firePropertyChange(String propertyName, Object oldValue, Object newValue) { 290 DEFAULT.support.firePropertyChange(propertyName, oldValue, newValue); 291 } 292 293 } 294 | Popular Tags |