1 19 20 package org.netbeans.modules.xml.xdm.visitor; 21 22 import javax.swing.text.AbstractDocument ; 23 import java.io.IOException ; 24 import javax.swing.text.BadLocationException ; 25 import org.netbeans.editor.BaseDocument; 26 import org.netbeans.modules.xml.text.syntax.XMLKit; 27 import org.netbeans.modules.xml.xdm.nodes.Document; 28 import org.netbeans.modules.xml.xdm.nodes.XMLSyntaxParser; 29 import org.w3c.dom.NodeList ; 30 31 37 public class Utils { 38 39 51 public static void replaceDocument(final javax.swing.text.Document doc, final String newDoc, String prefixMark) 52 throws javax.swing.text.BadLocationException { 53 if (doc == null) { 54 return; 55 } 56 final String origDocument = doc.getText(0, doc.getLength()); 57 final String newDocument = newDoc; 58 59 if (origDocument.equals(newDocument)) { 60 return; 62 } 63 64 final char[] origChars = origDocument.toCharArray(); 65 final char[] newcChars = newDocument.toCharArray(); 66 int tailIndex = origChars.length; 67 final int delta = newcChars.length - tailIndex; 68 int n = delta < 0 ? tailIndex + delta : tailIndex; 69 int offset; 70 for (offset = 0; offset < n; offset++) { 71 if (origChars[offset] != newcChars[offset]) { 72 break; 73 } 74 } 75 n = delta < 0 ? offset - delta : offset; 76 for (int i = tailIndex - 1; i >= n; i--) { 77 if (origChars[i] == newcChars[i + delta]) { 78 tailIndex = i; 79 } else { 80 break; 81 } 82 } 83 84 final String s = newDocument.substring(offset, tailIndex + delta); 85 final int length = tailIndex - offset; 86 if (doc instanceof AbstractDocument ) { 87 ((AbstractDocument ) doc).replace(offset, length, s, null); 88 } else { 89 if (length > 0) { 90 doc.remove(offset, length); 91 } 92 if (s.length() > 0) { 93 doc.insertString(offset, s, null); 94 } 95 } 96 } 97 98 public static void replaceDocument(javax.swing.text.Document doc, String newDoc) throws javax.swing.text.BadLocationException { 99 replaceDocument(doc,newDoc,null); 100 } 101 102 106 public static String filterEndLines(String str) { 107 char[] text = str.toCharArray(); 108 if (text.length==0) return ""; 109 int pos = 0; 110 for (int i = 0; i < text.length; i++) { 111 char c = text[i]; 112 if (c != 13) { 113 if (pos != i) 114 text[pos] = c; 115 pos++; 116 } 117 } 118 return new String (text, 0, pos); 119 } 120 121 public static BaseDocument loadDocument(String text) throws IOException { 122 BaseDocument sd = new BaseDocument(XMLKit.class, false); 123 try { 124 sd.insertString(0, text, null); 125 return sd; 126 } catch (BadLocationException ble) { 127 throw new IOException (ble.getLocalizedMessage()); 128 } 129 } 130 131 public static NodeList parseFragment(String text) throws IOException { 132 StringBuilder sb = new StringBuilder (text.length()+20); 133 sb.append("<r>"); 135 sb.append(text); 136 sb.append("</r>"); 137 XMLSyntaxParser parser = new XMLSyntaxParser(); 138 try { 139 Document dom = parser.parse(loadDocument(sb.toString())); 140 return dom.getDocumentElement().getChildNodes(); 141 } catch (BadLocationException ble) { 142 throw new IOException (ble.getLocalizedMessage()); 143 } 144 } 145 146 } 147 | Popular Tags |