KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xdm > visitor > Utils


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.xdm.visitor;
21
22 import javax.swing.text.AbstractDocument JavaDoc;
23 import java.io.IOException JavaDoc;
24 import javax.swing.text.BadLocationException JavaDoc;
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 JavaDoc;
30
31 /**
32  * Utils.java
33  *
34  * Created on November 16, 2004, 3:21 PM
35  * @author mkuchtiak
36  */

37 public class Utils {
38     
39     /** This method update document in editor after change in beans hierarchy.
40      * It takes old document and new document in String.
41      * To avoid regeneration of whole document in text editor following steps are done:
42      * 1) compare the begin of both documents (old one and new one)
43      * - find the first position where both documents differ
44      * 2) do the same from the ends of documents
45      * 3) remove old middle part of text (modified part) and insert new one
46      *
47      * @param doc original document
48      * @param newDoc new value of whole document
49      * @param prefixMark - beginning part of the document before this mark should be preserved
50      */

51   public static void replaceDocument(final javax.swing.text.Document JavaDoc doc, final String JavaDoc newDoc, String JavaDoc prefixMark)
52   throws javax.swing.text.BadLocationException JavaDoc {
53       if (doc == null) {
54           return;
55       }
56       final String JavaDoc origDocument = doc.getText(0, doc.getLength());
57       final String JavaDoc newDocument = newDoc;
58       
59       if (origDocument.equals(newDocument)) {
60           // no change in document
61
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 JavaDoc s = newDocument.substring(offset, tailIndex + delta);
85       final int length = tailIndex - offset;
86       if (doc instanceof AbstractDocument JavaDoc) {
87           ((AbstractDocument JavaDoc) 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 JavaDoc doc, String JavaDoc newDoc) throws javax.swing.text.BadLocationException JavaDoc {
99         replaceDocument(doc,newDoc,null);
100     }
101     
102     /** Filter characters #13 (CR) from the specified String
103      * @param str original string
104      * @return the string without #13 characters
105      */

106     public static String JavaDoc filterEndLines(String JavaDoc 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 JavaDoc(text, 0, pos);
119     }
120
121     public static BaseDocument loadDocument(String JavaDoc text) throws IOException JavaDoc {
122         BaseDocument sd = new BaseDocument(XMLKit.class, false);
123         try {
124             sd.insertString(0, text, null);
125             return sd;
126         } catch (BadLocationException JavaDoc ble) {
127             throw new IOException JavaDoc(ble.getLocalizedMessage());
128         }
129     }
130     
131     public static NodeList JavaDoc parseFragment(String JavaDoc text) throws IOException JavaDoc {
132         StringBuilder JavaDoc sb = new StringBuilder JavaDoc(text.length()+20);
133 // sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
134
sb.append("<r>");
135         sb.append(text);
136         sb.append("</r>");
137         XMLSyntaxParser parser = new XMLSyntaxParser();
138         try {
139             Document JavaDoc dom = parser.parse(loadDocument(sb.toString()));
140             return dom.getDocumentElement().getChildNodes();
141         } catch (BadLocationException JavaDoc ble) {
142             throw new IOException JavaDoc(ble.getLocalizedMessage());
143         }
144     }
145     
146 }
147
Popular Tags