1 19 package org.netbeans.modules.xml.tax.parser; 20 21 import java.io.StringReader ; 22 import java.io.IOException ; 23 import java.net.URL ; 24 import java.lang.ref.WeakReference ; 25 26 import javax.swing.text.Document ; 27 28 import org.xml.sax.InputSource ; 29 30 import org.openide.nodes.*; import org.openide.ErrorManager; 32 import org.openide.filesystems.FileObject; 33 34 import org.netbeans.tax.TreeException; 35 import org.netbeans.tax.TreeDocumentRoot; 36 37 44 public abstract class ParsingSupport { 45 46 49 public abstract TreeDocumentRoot parse(InputSource in) throws IOException , TreeException; 50 51 52 55 protected TreeDocumentRoot parse(TreeDocumentRoot doc) throws IOException , TreeException { 56 57 return doc; 58 59 } 63 64 65 68 protected TreeDocumentRoot parse(final Document doc) throws IOException , TreeException { 69 70 InputSource in = new InputSource (); 71 72 75 final String [] str = new String [1]; 76 77 Runnable run = new Runnable () { 78 public void run () { 79 try { 80 str[0] = doc.getText(0, doc.getLength()); 81 } catch (javax.swing.text.BadLocationException e) { 82 e.printStackTrace(); 84 } 85 } 86 }; 87 88 doc.render(run); 89 in.setCharacterStream(new StringReader (str[0])); 90 return parse(in); 91 } 92 93 94 97 protected TreeDocumentRoot parse(FileObject fo) throws IOException , TreeException{ 98 99 try { 100 URL url = fo.getURL(); 101 InputSource in = new InputSource (url.toExternalForm()); in.setByteStream(fo.getInputStream()); 103 return parse(in); 104 105 } catch (IOException ex) { 106 ErrorManager emgr = ErrorManager.getDefault(); 107 emgr.annotate(ex, Util.THIS.getString("MSG_can_not_create_URL")); 108 emgr.notify(ex); 109 } 110 return null; 111 } 112 113 } 114 | Popular Tags |