KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > builders > SchemaHandler


1 /*******************************************************************************
2  * Copyright (c) 2003, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.builders;
12
13 import java.io.*;
14 import java.util.*;
15
16 import javax.xml.parsers.*;
17
18 import org.eclipse.core.resources.*;
19 import org.eclipse.jface.text.*;
20 import org.eclipse.jface.text.Document;
21 import org.w3c.dom.*;
22 import org.xml.sax.*;
23
24 /**
25  * @author melhem
26  *
27  */

28 public class SchemaHandler extends PluginErrorReporter {
29     
30     private org.w3c.dom.Document JavaDoc fDocument;
31     private IDocument fTextDocument;
32     private FindReplaceDocumentAdapter fFindReplaceAdapter;
33     private Locator fLocator;
34     private Hashtable fLineTable;
35     private Element fRootElement;
36     
37     private Stack fElementStack = new Stack();
38     
39     public SchemaHandler(IFile file) {
40         super(file);
41         createTextDocument(file);
42         fLineTable = new Hashtable();
43     }
44     
45     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes attributes)
46         throws SAXException {
47         Element element = fDocument.createElement(qName);
48         for (int i = 0; i < attributes.getLength(); i++) {
49             element.setAttribute(attributes.getQName(i), attributes.getValue(i));
50         }
51         
52         Integer JavaDoc lineNumber = new Integer JavaDoc(fLocator.getLineNumber());
53         try {
54             lineNumber = getCorrectStartLine(qName);
55         } catch (BadLocationException e) {
56         }
57         Integer JavaDoc[] range = new Integer JavaDoc[] {lineNumber, new Integer JavaDoc(-1)};
58         fLineTable.put(element, range);
59         if (fRootElement == null)
60             fRootElement = element;
61         else
62             ((Element)fElementStack.peek()).appendChild(element);
63         fElementStack.push(element);
64     }
65     
66     public void endElement(String JavaDoc arg0, String JavaDoc arg1, String JavaDoc arg2) throws SAXException {
67         Integer JavaDoc[] range = (Integer JavaDoc[])fLineTable.get(fElementStack.pop());
68         range[1] = new Integer JavaDoc(fLocator.getLineNumber());
69     }
70     
71     /* (non-Javadoc)
72      * @see org.xml.sax.helpers.DefaultHandler#setDocumentLocator(org.xml.sax.Locator)
73      */

74     public void setDocumentLocator(Locator locator) {
75         fLocator = locator;
76     }
77
78     /* (non-Javadoc)
79      * @see org.xml.sax.helpers.DefaultHandler#startDocument()
80      */

81     public void startDocument() throws SAXException {
82         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
83         try {
84             fDocument = factory.newDocumentBuilder().newDocument();
85         } catch (ParserConfigurationException e) {
86         }
87     }
88     
89     /* (non-Javadoc)
90      * @see org.xml.sax.helpers.DefaultHandler#endDocument()
91      */

92     public void endDocument() throws SAXException {
93         fDocument.appendChild(fRootElement);
94     }
95     
96     /* (non-Javadoc)
97      * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
98      */

99     public void characters(char[] characters, int offset, int length) throws SAXException {
100         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
101         for (int i = 0; i < length; i++) {
102             buff.append(characters[offset + i]);
103         }
104         Text text = fDocument.createTextNode(buff.toString());
105         if (fRootElement == null)
106             fDocument.appendChild(text);
107         else
108             ((Element)fElementStack.peek()).appendChild(text);
109     }
110     
111     public Node getDocumentElement() {
112         if (fDocument != null) {
113             Element docElement = fDocument.getDocumentElement();
114             if (docElement != null) {
115                 docElement.normalize();
116                 return docElement;
117             }
118         }
119         return null;
120     }
121     
122     public Hashtable getLineTable() {
123         return fLineTable;
124     }
125     
126     private void createTextDocument(IFile file) {
127         try {
128             BufferedReader reader =
129                 new BufferedReader(new FileReader(file.getLocation().toOSString()));
130             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
131             while (reader.ready()) {
132                 String JavaDoc line = reader.readLine();
133                 if (line != null) {
134                     buffer.append(line);
135                     buffer.append(System.getProperty("line.separator")); //$NON-NLS-1$
136
}
137             }
138             fTextDocument = new Document(buffer.toString());
139             fFindReplaceAdapter = new FindReplaceDocumentAdapter(fTextDocument);
140             reader.close();
141         } catch (FileNotFoundException e) {
142         } catch (IOException e) {
143         }
144     }
145     
146     private Integer JavaDoc getCorrectStartLine(String JavaDoc elementName) throws BadLocationException{
147         int lineNumber = fLocator.getLineNumber();
148         int colNumber = fLocator.getColumnNumber();
149         if (colNumber < 0)
150             colNumber = getLastCharColumn(lineNumber);
151         int offset = fTextDocument.getLineOffset(lineNumber - 1) + colNumber - 1;
152         IRegion region = fFindReplaceAdapter.find(offset, "<" + elementName, false, false, false, false); //$NON-NLS-1$
153
return new Integer JavaDoc(fTextDocument.getLineOfOffset(region.getOffset()) + 1);
154     }
155     
156     private int getLastCharColumn(int line) throws BadLocationException {
157         String JavaDoc lineDelimiter = fTextDocument.getLineDelimiter(line - 1);
158         int lineDelimiterLength = lineDelimiter != null ? lineDelimiter.length() : 0;
159         return fTextDocument.getLineLength(line - 1) - lineDelimiterLength;
160     }
161     
162 }
163
Popular Tags