KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mc4j > console > swing > editor > xml > EditorDocument


1 /*
2  * Copyright 2002-2004 Greg Hinkle
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.mc4j.console.swing.editor.xml;
18
19 import org.w3c.dom.Document JavaDoc;
20 import org.xml.sax.ErrorHandler JavaDoc;
21 import org.xml.sax.SAXException JavaDoc;
22 import org.xml.sax.SAXParseException JavaDoc;
23
24 import javax.swing.text.AttributeSet JavaDoc;
25 import javax.swing.text.BadLocationException JavaDoc;
26 import javax.swing.text.DefaultStyledDocument JavaDoc;
27 import javax.swing.text.Style JavaDoc;
28 import javax.swing.text.StyleConstants JavaDoc;
29 import javax.xml.parsers.DocumentBuilder JavaDoc;
30 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
31 import javax.xml.parsers.ParserConfigurationException JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.InputStream JavaDoc;
34 import java.io.StringBufferInputStream JavaDoc;
35
36 /**
37  * @author Greg Hinkle (ghinkle@users.sourceforge.net), Nov 16, 2004
38  * @version $Revision: 570 $($Author: ghinkl $ / $Date: 2006-04-12 15:14:16 -0400 (Wed, 12 Apr 2006) $)
39  */

40 public class EditorDocument extends DefaultStyledDocument JavaDoc implements ErrorHandler JavaDoc {
41
42     protected StyleTokens types;
43
44     protected boolean validating = false;
45
46     public EditorDocument(StyleTokens types) {
47         Style JavaDoc defaultStyle = getStyle("default");
48         StyleConstants.setFontSize(defaultStyle, 12);
49         StyleConstants.setFontFamily(defaultStyle, "Courier New");
50
51         Style JavaDoc tagStyle = addStyle("tag", defaultStyle);
52         StyleConstants.setBold(tagStyle,true);
53
54
55         this.types = types;
56         types.setStyles(this);
57     }
58
59     public void insertString(int offset, String JavaDoc text, AttributeSet JavaDoc style)
60         throws BadLocationException JavaDoc {
61         super.insertString(offset, text, style);
62
63         fireUpdate();
64     }
65
66     public void remove(int offset, int length)
67         throws BadLocationException JavaDoc {
68         super.remove(offset, length);
69         fireUpdate();
70     }
71
72     private Thread JavaDoc updateThread = new Thread JavaDoc(new Runnable JavaDoc() {
73         public void run() {
74             while (true) {
75                 if (validating && needsUpdate) {
76                     needsUpdate = false;
77                     highlightSyntax();
78                     validateSyntax();
79                 }
80                 try {
81                     Thread.sleep(500);
82                 } catch (InterruptedException JavaDoc e) {
83                     e.printStackTrace();
84                 }
85             }
86         }
87     });
88
89     private boolean needsUpdate = false;
90
91
92
93     public void fireUpdate() {
94         needsUpdate = true;
95     }
96
97     public void warning(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
98         System.out.println(exception);
99         highlightError(exception);
100     }
101
102     public void error(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
103         System.out.println(exception);
104         highlightError(exception);
105     }
106
107     public void fatalError(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
108         System.out.println(exception);
109         highlightError(exception);
110     }
111
112     public void validateSyntax() {
113         long start = System.currentTimeMillis();
114         try {
115             System.out.println("VALIDATING");
116             String JavaDoc text = getText(0, getLength());
117             InputStream JavaDoc s = new StringBufferInputStream JavaDoc(text);
118
119
120             DocumentBuilderFactory JavaDoc documentBuilderFactory = DocumentBuilderFactory.newInstance();
121 // documentBuilderFactory.setValidating(true);
122
DocumentBuilder JavaDoc documentBuilder = documentBuilderFactory.newDocumentBuilder();
123
124             documentBuilder.setErrorHandler(this);
125             Document JavaDoc doc = documentBuilder.parse(s);
126
127 // System.out.println("VER: " + doc.getXmlVersion());
128

129
130         } catch (BadLocationException JavaDoc e) {
131 // e.printStackTrace();
132
} catch (IOException JavaDoc e) {
133 // e.printStackTrace();
134
} catch (ParserConfigurationException JavaDoc e) {
135 // e.printStackTrace();
136
} catch (SAXException JavaDoc e) {
137 // e.printStackTrace();
138
} finally {
139             System.out.println("Validation took: " + (System.currentTimeMillis() - start) + "ms");
140         }
141     }
142
143     public void highlightError(SAXException JavaDoc se) {
144         if (se instanceof SAXParseException JavaDoc) {
145             try {
146                 String JavaDoc text = getText(0, getLength());
147
148                 SAXParseException JavaDoc spe = (SAXParseException JavaDoc) se;
149                 int line = spe.getLineNumber();
150                 int column = spe.getColumnNumber();
151
152                 if (line < 1 || column < 1)
153                     return;
154
155                 String JavaDoc[] lines = text.split("\n");
156                 int l = 0;
157                 for (int i=0;i<line-1;i++) {
158                      l += lines[i].length()+1;
159                 }
160
161                 int position = l + column;
162
163                 setCharacterAttributes(position,1,getStyle("error"),false);
164
165
166             } catch (BadLocationException JavaDoc e) {
167                 e.printStackTrace();
168             }
169         }
170
171     }
172
173     public void highlightSyntax() {
174         try {
175             String JavaDoc text = getText(0, getLength());
176             // Clear the existing styling
177
setCharacterAttributes(0, getLength(), getStyle("default"), true);
178
179             RETokenizer.Token token;
180             RETokenizer tokenizer = new RETokenizer(types, text);
181             while ((token = tokenizer.nextToken()) != null) {
182                 int pos = token.getPosition();
183                 String JavaDoc type = token.getType();
184                 String JavaDoc word = token.getText();
185                 int len = word.length();
186                 for (int i = 0; i < types.getTokens().length; i++) {
187                     StyleTokens.StyleToken styleToken = types.getTokens()[i];
188                     String JavaDoc name = styleToken.name;
189                     if (type.equals(name)) {
190                         setCharacterAttributes(pos, len, getStyle(name), false);
191                     }
192                 }
193             }
194         } catch (Exception JavaDoc e) {
195             e.printStackTrace();
196         }
197     }
198
199     public boolean isValidating() {
200         return validating;
201     }
202
203     public void setValidating(boolean validating) {
204         this.validating = validating;
205
206         if (validating) {
207             fireUpdate();
208             updateThread.start();
209         }
210     }
211 }
Popular Tags