1 19 20 package org.netbeans.api.xml.parsers; 21 22 import java.io.*; 23 import java.net.URL ; 24 import java.util.*; 25 26 import javax.swing.text.Document ; 27 28 import org.xml.sax.InputSource ; 29 30 import org.openide.ErrorManager; 31 import org.openide.filesystems.FileObject; 32 import org.openide.loaders.DataObject; 33 import org.openide.util.Lookup; 34 35 41 public final class DocumentInputSource extends InputSource { 42 43 private final Document doc; 44 45 51 public DocumentInputSource(Document doc) { 52 this.doc = doc; 53 } 54 55 public Reader getCharacterStream() { 57 String text = documentToString(doc); 58 return new StringReader(text); 59 } 60 61 66 public final void setCharacterStream(Reader reader) { 67 } 69 70 80 public String getSystemId() { 81 82 String system = super.getSystemId();; 83 84 87 if (system == null) { 88 Object obj = doc.getProperty(Document.StreamDescriptionProperty); 89 if (obj instanceof DataObject) { 90 try { 91 DataObject dobj = (DataObject) obj; 92 FileObject fo = dobj.getPrimaryFile(); 93 URL url = fo.getURL(); 94 system = url.toExternalForm(); 95 } catch (IOException io) { 96 ErrorManager emgr = (ErrorManager) Lookup.getDefault().lookup(ErrorManager.class); 97 emgr.notify(io); 98 } 99 } else { 100 ErrorManager emgr = (ErrorManager) Lookup.getDefault().lookup(ErrorManager.class); 101 emgr.log("XML:DocumentInputSource:Unknown stream description:" + obj); 102 } 103 } 104 105 return system; 106 } 107 108 109 112 private static String documentToString(final Document doc) { 113 114 final String [] str = new String [1]; 115 116 Runnable run = new Runnable () { 118 public void run () { 119 try { 120 str[0] = doc.getText(0, doc.getLength()); 121 } catch (javax.swing.text.BadLocationException e) { 122 e.printStackTrace(); 124 } 125 } 126 }; 127 128 doc.render(run); 129 return str[0]; 130 131 } 132 133 136 public String toString() { 137 return "DocumentInputSource SID:" + getSystemId(); 138 } 139 } 140 | Popular Tags |