2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.xml.xdm.visitor;
44
import javax.swing.text.AbstractDocument;
45
import java.io.IOException;
46
import javax.swing.text.BadLocationException;
47
import org.netbeans.editor.BaseDocument;
48
import org.netbeans.modules.xml.text.syntax.XMLKit;
49
import org.netbeans.modules.xml.xdm.nodes.Document;
50
import org.netbeans.modules.xml.xdm.nodes.XMLSyntaxParser;
51
import org.w3c.dom.NodeList;
56
* Created on November 16, 2004, 3:21 PM
61
/** This method update document in editor after change in beans hierarchy.
62
* It takes old document and new document in String.
63
* To avoid regeneration of whole document in text editor following steps are done:
64
* 1) compare the begin of both documents (old one and new one)
65
* - find the first position where both documents differ
66
* 2) do the same from the ends of documents
67
* 3) remove old middle part of text (modified part) and insert new one
69
* @param doc original document
70
* @param newDoc new value of whole document
71
* @param prefixMark - beginning part of the document before this mark should be preserved
73
public static void replaceDocument(final javax.swing.text.Document doc, final String newDoc, String prefixMark)
74
throws javax.swing.text.BadLocationException {
78
final String origDocument = doc.getText(0, doc.getLength());
79
final String newDocument = newDoc;
81
if (origDocument.equals(newDocument)) {
82
// no change in document
86
final char[] origChars = origDocument.toCharArray();
87
final char[] newcChars = newDocument.toCharArray();
88
int tailIndex = origChars.length;
89
final int delta = newcChars.length - tailIndex;
90
int n = delta < 0 ? tailIndex + delta : tailIndex;
92
for (offset = 0; offset < n; offset++) {
93
if (origChars[offset] != newcChars[offset]) {
97
n = delta < 0 ? offset - delta : offset;
98
for (int i = tailIndex - 1; i >= n; i--) {
99
if (origChars[i] == newcChars[i + delta]) {
106
final String s = newDocument.substring(offset, tailIndex + delta);
107
final int length = tailIndex - offset;
108
if (doc instanceof AbstractDocument) {
109
((AbstractDocument) doc).replace(offset, length, s, null);
112
doc.remove(offset, length);
114
if (s.length() > 0) {
115
doc.insertString(offset, s, null);
120
public static void replaceDocument(javax.swing.text.Document doc, String newDoc) throws javax.swing.text.BadLocationException {
121
replaceDocument(doc,newDoc,null);
124
/** Filter characters #13 (CR) from the specified String
125
* @param str original string
126
* @return the string without #13 characters
128
public static String filterEndLines(String str) {
129
char[] text = str.toCharArray();
130
if (text.length==0) return "";
132
for (int i = 0; i < text.length; i++) {
140
return new String(text, 0, pos);
143
public static BaseDocument loadDocument(String text) throws IOException {
144
BaseDocument sd = new BaseDocument(XMLKit.class, false);
146
sd.insertString(0, text, null);
148
} catch (BadLocationException ble) {
149
throw new IOException(ble.getLocalizedMessage());
153
public static NodeList parseFragment(String text) throws IOException {
154
StringBuilder sb = new StringBuilder(text.length()+20);
155
// sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
159
XMLSyntaxParser parser = new XMLSyntaxParser();
161
Document dom = parser.parse(loadDocument(sb.toString()));
162
return dom.getDocumentElement().getChildNodes();
163
} catch (BadLocationException ble) {
164
throw new IOException(ble.getLocalizedMessage());