1 19 20 package org.netbeans.spi.xml.cookies; 21 22 import java.io.Reader ; 23 import java.net.URI ; 24 import java.net.URISyntaxException ; 25 import java.net.URL ; 26 import javax.swing.text.Document ; 27 import javax.xml.parsers.ParserConfigurationException ; 28 import javax.xml.parsers.SAXParser ; 29 import javax.xml.parsers.SAXParserFactory ; 30 import javax.xml.transform.Source ; 31 import javax.xml.transform.sax.SAXSource ; 32 import org.netbeans.api.xml.parsers.DocumentInputSource; 33 import org.netbeans.api.xml.services.UserCatalog; 34 import org.openide.cookies.EditorCookie; 35 import org.openide.filesystems.FileObject; 36 import org.openide.filesystems.FileStateInvalidException; 37 import org.openide.loaders.DataObject; 38 import org.xml.sax.EntityResolver ; 39 import org.xml.sax.InputSource ; 40 import org.xml.sax.SAXException ; 41 import org.xml.sax.SAXNotRecognizedException ; 42 import org.xml.sax.SAXNotSupportedException ; 43 import org.xml.sax.XMLReader ; 44 45 51 public final class DataObjectAdapters { 52 53 54 private static final String SAX_FEATURES_NAMESPACES = "http://xml.org/sax/features/namespaces"; 56 57 private static SAXParserFactory saxParserFactory; 58 59 private DataObjectAdapters() { 60 } 61 62 67 public static InputSource inputSource (DataObject dataObject) { 68 if (dataObject == null) throw new NullPointerException (); 69 return new DataObjectInputSource(dataObject); 70 } 71 72 75 private static class DataObjectInputSource extends InputSource { 76 77 private final DataObject dataObject; 78 79 public DataObjectInputSource (DataObject dataObject) { 80 this.dataObject = dataObject; 81 } 82 83 public String getSystemId() { 84 return DataObjectAdapters.getSystemId (dataObject); 85 } 86 87 public Reader getCharacterStream() { 88 89 EditorCookie editor = (EditorCookie) dataObject.getCookie(EditorCookie.class); 90 91 if (editor != null) { 92 Document doc = editor.getDocument(); 93 if (doc != null) { 94 return new DocumentInputSource(doc).getCharacterStream(); 95 } 96 } 97 98 return null; 99 } 100 101 } 102 103 104 109 public static Source source (DataObject dataObject) { 110 if (dataObject == null) throw new NullPointerException (); 111 return new DataObjectSAXSource(dataObject); 112 } 113 114 117 private static class DataObjectSAXSource extends SAXSource { 118 119 private final DataObject dataObject; 120 121 public DataObjectSAXSource(DataObject dataObject) { 122 this.dataObject = dataObject; 123 } 124 125 public String getSystemId() { 126 return DataObjectAdapters.getSystemId (dataObject); 127 } 128 129 public XMLReader getXMLReader() { 130 try { 131 XMLReader reader = newXMLReader(); 132 reader.setEntityResolver (getEntityResolver()); 133 return reader; 134 } catch (ParserConfigurationException ex) { 135 Util.THIS.debug(ex); 136 } catch (SAXNotRecognizedException ex) { 137 Util.THIS.debug(ex); 138 } catch (SAXNotSupportedException ex) { 139 Util.THIS.debug(ex); 140 } catch (SAXException ex) { 141 Util.THIS.debug(ex); 142 } 143 return null; 144 } 145 146 public InputSource getInputSource() { 147 return inputSource (dataObject); 148 } 149 150 } 152 153 156 private static String getSystemId (DataObject dataObject) { 157 String systemId = null; 158 try { 159 FileObject fileObject = dataObject.getPrimaryFile(); 160 URL url = fileObject.getURL(); 161 try { 162 systemId = new URI (url.toString()).toASCIIString(); 163 } catch (URISyntaxException ex) { 164 systemId = url.toExternalForm(); 167 Util.THIS.debug(ex); 168 } 169 } catch (FileStateInvalidException exc) { 170 if ( Util.THIS.isLoggable() ) Util.THIS.debug (exc); 171 172 } 174 return systemId; 175 } 176 177 private static synchronized SAXParserFactory getSAXParserFactory () throws ParserConfigurationException , SAXNotRecognizedException , SAXNotSupportedException { 178 if ( saxParserFactory == null ) { 179 saxParserFactory = SAXParserFactory.newInstance(); 180 saxParserFactory.setFeature (SAX_FEATURES_NAMESPACES, true); 181 } 182 return saxParserFactory; 183 } 184 185 191 private static XMLReader newXMLReader () throws ParserConfigurationException , SAXException { 192 SAXParser parser = getSAXParserFactory().newSAXParser(); return parser.getXMLReader(); 194 } 195 196 private static EntityResolver getEntityResolver () { 197 UserCatalog catalog = UserCatalog.getDefault(); 198 EntityResolver res = (catalog == null ? null : catalog.getEntityResolver()); 199 return res; 200 } 201 } 202 | Popular Tags |