KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > text > syntax > UniKit


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 package org.netbeans.modules.xml.text.syntax;
20
21 import java.util.*;
22 import java.io.*;
23
24 import javax.swing.text.Document JavaDoc;
25 import javax.swing.text.BadLocationException JavaDoc;
26
27 import org.openide.windows.*;
28
29 import org.netbeans.editor.Syntax;
30 import org.netbeans.editor.BaseDocument;
31 import org.netbeans.modules.editor.NbEditorKit;
32 import org.netbeans.modules.editor.*;
33
34 import org.netbeans.modules.xml.core.lib.EncodingHelper;
35
36 /**
37  * Editor kit implementation for xml content type.
38  * It translates encoding used by document to Unicode encoding.
39  * It makes sence for org.epenide.loaders.XMLDataObject that does
40  * use default EditorSupport.
41  *
42  * @author Petr Kuzel
43  */

44 public class UniKit extends NbEditorKit {
45
46     /** Serial Version UID */
47     private static final long serialVersionUID = -940485353900594155L;
48     
49     /** Read document. */
50     public void read(InputStream in, Document JavaDoc doc, int pos) throws IOException, BadLocationException JavaDoc {
51
52         // predetect it to get optimalized XmlReader if utf-8
53
String JavaDoc enc = EncodingHelper.detectEncoding(in);
54         if ( enc == null ) {
55             enc = "UTF8"; //!!! // NOI18N
56
}
57         // System.err.println("UniKit.reading as " + enc);
58
Reader r = new InputStreamReader(in, enc);
59         super.read(r, doc, pos);
60
61     }
62
63     /** Hope that it is called by knowing (encoding).
64     */

65     public void read(Reader in, Document JavaDoc doc, int pos) throws IOException, BadLocationException JavaDoc {
66         super.read(in, doc, pos);
67     }
68
69     /** Write document. */
70     public void write(OutputStream out, Document JavaDoc doc, int pos, int len) throws IOException, BadLocationException JavaDoc {
71         String JavaDoc enc = EncodingHelper.detectEncoding(doc);
72         if ( enc == null ) {
73             enc = "UTF8"; //!!! // NOI18N
74
}
75         // System.err.println("UniKit.writing as " + enc);
76
super.write( new OutputStreamWriter(out, enc), doc, pos, len);
77     }
78
79     /** Hope that it is called by knowing (encoding).
80     */

81     public void write(Writer out, Document JavaDoc doc, int pos, int len) throws IOException, BadLocationException JavaDoc {
82         super.write(out, doc, pos, len);
83     }
84
85 }
86
Popular Tags