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-2007 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: package org.netbeans.modules.visualweb.insync;
042:
043: import java.io.File;
044: import java.io.FileReader;
045: import java.io.FileWriter;
046: import java.io.Reader;
047: import java.io.Writer;
048: import java.util.Enumeration;
049:
050: import javax.swing.text.AbstractDocument;
051: import javax.swing.text.Element;
052: import javax.swing.text.StringContent;
053:
054: /**
055: * A concrete swing AbstractDocument implementation to allow easy file access to SourceUnits
056: * expecting an AbstractDocument. Used primarily for unit testing and other debugging.
057: * @author Carl Quinn
058: */
059: public class FileDocument extends AbstractDocument {
060:
061: /**
062: *
063: */
064: private static final long serialVersionUID = 3833183653189988409L;
065:
066: String name;
067: RootElement root = new RootElement();
068:
069: //------------------------------------------------------------------------------------- Events
070:
071: public FileDocument(String filename) throws java.io.IOException {
072: super (new StringContent());
073: name = filename;
074: read();
075: }
076:
077: /**
078: * Read from the associated file into this document
079: */
080: public void read() throws java.io.IOException {
081: File f = new File(name);
082: int len = (int) f.length();
083: Reader r = new FileReader(f);
084: char[] filebuf = new char[len];
085: r.read(filebuf);
086: r.close();
087:
088: try {
089: replace(0, getLength(), new String(filebuf), null);
090: } catch (javax.swing.text.BadLocationException e) {
091: // we know the location we passed is good...
092: }
093: }
094:
095: /**
096: * Write to the associated file from this document
097: */
098: public void write() throws java.io.IOException {
099: File f = new File(name);
100: //int len = (int)f.length();
101: Writer w = new FileWriter(f);
102:
103: try {
104: String text = getText(0, getLength());
105:
106: w.write(text);
107: w.close();
108: } catch (javax.swing.text.BadLocationException e) {
109: // we know the location we passed is good...
110: }
111: }
112:
113: class RootElement extends AbstractDocument.AbstractElement {
114:
115: /**
116: *
117: */
118: private static final long serialVersionUID = 3545234717587551797L;
119:
120: RootElement() {
121: super (null, null);
122: }
123:
124: public int getStartOffset() {
125: return 0;
126: }
127:
128: public int getEndOffset() {
129: return getLength();
130: }
131:
132: public boolean isLeaf() {
133: return true;
134: }
135:
136: public boolean getAllowsChildren() {
137: return false;
138: }
139:
140: public Enumeration children() {
141: return null;
142: }
143:
144: public Element getElement(int i) {
145: return null;
146: }
147:
148: public int getElementCount() {
149: return 0;
150: }
151:
152: public int getElementIndex(int offset) {
153: return -1;
154: }
155:
156: }
157:
158: public Element getDefaultRootElement() {
159: return root;
160: }
161:
162: public Element getParagraphElement(int pos) {
163: return root;
164: }
165: }
|