1 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 28 public class SchemaHandler extends PluginErrorReporter { 29 30 private org.w3c.dom.Document 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 uri, String localName, String 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 lineNumber = new Integer (fLocator.getLineNumber()); 53 try { 54 lineNumber = getCorrectStartLine(qName); 55 } catch (BadLocationException e) { 56 } 57 Integer [] range = new Integer [] {lineNumber, new Integer (-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 arg0, String arg1, String arg2) throws SAXException { 67 Integer [] range = (Integer [])fLineTable.get(fElementStack.pop()); 68 range[1] = new Integer (fLocator.getLineNumber()); 69 } 70 71 74 public void setDocumentLocator(Locator locator) { 75 fLocator = locator; 76 } 77 78 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 92 public void endDocument() throws SAXException { 93 fDocument.appendChild(fRootElement); 94 } 95 96 99 public void characters(char[] characters, int offset, int length) throws SAXException { 100 StringBuffer buff = new StringBuffer (); 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 buffer = new StringBuffer (); 131 while (reader.ready()) { 132 String line = reader.readLine(); 133 if (line != null) { 134 buffer.append(line); 135 buffer.append(System.getProperty("line.separator")); } 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 getCorrectStartLine(String 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); return new Integer (fTextDocument.getLineOfOffset(region.getOffset()) + 1); 154 } 155 156 private int getLastCharColumn(int line) throws BadLocationException { 157 String 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 |