1 19 package org.netbeans.modules.xml.core.text; 20 21 import java.io.Reader ; 22 import java.io.StringReader ; 23 24 import javax.swing.text.Document ; 25 26 import org.xml.sax.*; 27 28 import org.netbeans.modules.xml.core.*; 29 import org.netbeans.modules.xml.core.sync.*; 30 import org.netbeans.modules.xml.core.lib.*; 31 import java.net.URL ; 32 import java.io.IOException ; 33 34 46 public abstract class TextRepresentation extends SyncRepresentation { 47 48 51 protected final TextEditorSupport editor; 52 53 54 public TextRepresentation(TextEditorSupport editor, Synchronizator sync) { 55 super(sync); 56 this.editor = editor; 57 } 58 59 62 public boolean represents(Class type) { 63 return Document .class.isAssignableFrom(type); 64 } 65 66 public int level() { 67 return 1; 68 } 69 70 73 public Object getChange(Class type) { 74 if (type == null || type.isAssignableFrom(Document .class)) { 75 return editor.getDocument(); 76 } else if (type.isAssignableFrom(String .class)) { 77 try { 78 return Convertors.documentToString(editor.openDocument()); 79 } catch (IOException ex) { 80 Util.THIS.debug(ex); 81 return null; 82 } 83 } else if (type.isAssignableFrom(InputSource.class)) { 84 85 InputSource in = null; 86 try { 87 in = Convertors.documentToInputSource(editor.openDocument()); 88 } catch (IOException ex) { 89 Util.THIS.debug(ex); 90 return null; 91 } 92 try { 93 URL baseURL = editor.getDataObject().getPrimaryFile().getURL(); 94 String systemId = baseURL.toExternalForm(); 95 in.setSystemId(systemId); 96 } catch (IOException ex) { 97 if ( Util.THIS.isLoggable() ) Util.THIS.debug("Warning: missing file object, external entities cannot be parsed."); } 100 return in; 101 } else if (type.isAssignableFrom(Reader .class)) { 102 try { 103 return new StringReader (Convertors.documentToString(editor.openDocument())); 104 } catch (IOException ex) { 105 Util.THIS.debug(ex); 106 return null; 107 } 108 } 109 110 throw new RuntimeException ("Unsupported type: " + type); } 112 113 117 public String getDisplayName() { 118 return Util.THIS.getString ("PROP_Text_representation"); 119 } 120 121 124 public Class getUpdateClass() { 125 return String .class; 126 } 127 128 } 129 | Popular Tags |