001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.xml.xdm.visitor;
043:
044: import javax.swing.text.AbstractDocument;
045: import java.io.IOException;
046: import javax.swing.text.BadLocationException;
047: import org.netbeans.api.lexer.Language;
048: import org.netbeans.api.xml.lexer.XMLTokenId;
049: import org.netbeans.editor.BaseDocument;
050: import org.netbeans.modules.xml.text.syntax.XMLKit;
051: import org.netbeans.modules.xml.xdm.nodes.Document;
052: import org.netbeans.modules.xml.xdm.nodes.XMLSyntaxParser;
053: import org.w3c.dom.NodeList;
054:
055: /**
056: * Utils.java
057: *
058: * Created on November 16, 2004, 3:21 PM
059: * @author mkuchtiak
060: */
061: public class Utils {
062:
063: /** This method update document in editor after change in beans hierarchy.
064: * It takes old document and new document in String.
065: * To avoid regeneration of whole document in text editor following steps are done:
066: * 1) compare the begin of both documents (old one and new one)
067: * - find the first position where both documents differ
068: * 2) do the same from the ends of documents
069: * 3) remove old middle part of text (modified part) and insert new one
070: *
071: * @param doc original document
072: * @param newDoc new value of whole document
073: * @param prefixMark - beginning part of the document before this mark should be preserved
074: */
075: public static void replaceDocument(
076: final javax.swing.text.Document doc, final String newDoc,
077: String prefixMark)
078: throws javax.swing.text.BadLocationException {
079: if (doc == null) {
080: return;
081: }
082: final String origDocument = doc.getText(0, doc.getLength());
083: final String newDocument = newDoc;
084:
085: if (origDocument.equals(newDocument)) {
086: // no change in document
087: return;
088: }
089:
090: final char[] origChars = origDocument.toCharArray();
091: final char[] newcChars = newDocument.toCharArray();
092: int tailIndex = origChars.length;
093: final int delta = newcChars.length - tailIndex;
094: int n = delta < 0 ? tailIndex + delta : tailIndex;
095: int offset;
096: for (offset = 0; offset < n; offset++) {
097: if (origChars[offset] != newcChars[offset]) {
098: break;
099: }
100: }
101: n = delta < 0 ? offset - delta : offset;
102: for (int i = tailIndex - 1; i >= n; i--) {
103: if (origChars[i] == newcChars[i + delta]) {
104: tailIndex = i;
105: } else {
106: break;
107: }
108: }
109:
110: final String s = newDocument.substring(offset, tailIndex
111: + delta);
112: final int length = tailIndex - offset;
113: if (doc instanceof AbstractDocument) {
114: ((AbstractDocument) doc).replace(offset, length, s, null);
115: } else {
116: if (length > 0) {
117: doc.remove(offset, length);
118: }
119: if (s.length() > 0) {
120: doc.insertString(offset, s, null);
121: }
122: }
123: }
124:
125: public static void replaceDocument(javax.swing.text.Document doc,
126: String newDoc) throws javax.swing.text.BadLocationException {
127: replaceDocument(doc, newDoc, null);
128: }
129:
130: /** Filter characters #13 (CR) from the specified String
131: * @param str original string
132: * @return the string without #13 characters
133: */
134: public static String filterEndLines(String str) {
135: char[] text = str.toCharArray();
136: if (text.length == 0)
137: return "";
138: int pos = 0;
139: for (int i = 0; i < text.length; i++) {
140: char c = text[i];
141: if (c != 13) {
142: if (pos != i)
143: text[pos] = c;
144: pos++;
145: }
146: }
147: return new String(text, 0, pos);
148: }
149:
150: public static BaseDocument loadDocument(String text)
151: throws IOException {
152: BaseDocument sd = new BaseDocument(XMLKit.class, false);
153: sd.putProperty(Language.class, XMLTokenId.language());
154: try {
155: sd.insertString(0, text, null);
156: return sd;
157: } catch (BadLocationException ble) {
158: throw new IOException(ble.getLocalizedMessage());
159: }
160: }
161:
162: public static NodeList parseFragment(String text)
163: throws IOException {
164: StringBuilder sb = new StringBuilder(text.length() + 20);
165: // sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
166: sb.append("<r>");
167: sb.append(text);
168: sb.append("</r>");
169: XMLSyntaxParser parser = new XMLSyntaxParser();
170: try {
171: Document dom = parser.parse(loadDocument(sb.toString()));
172: return dom.getDocumentElement().getChildNodes();
173: } catch (BadLocationException ble) {
174: throw new IOException(ble.getLocalizedMessage());
175: }
176: }
177:
178: }
|