|
7 | 7 | import java.util.List;
|
8 | 8 |
|
9 | 9 | import org.xml.sax.Attributes;
|
10 |
| -import org.xml.sax.Locator; |
11 | 10 | import org.xml.sax.SAXException;
|
12 | 11 | import org.xml.sax.helpers.DefaultHandler;
|
13 | 12 |
|
|
16 | 15 | */
|
17 | 16 | public class DeploymentPlanHandler extends DefaultHandler {
|
18 | 17 | protected String file;
|
19 |
| - protected Locator locator; |
20 | 18 | protected List<DeploymentPlanTextNode> nodeList = new ArrayList<DeploymentPlanTextNode>();
|
21 | 19 |
|
22 | 20 | protected static final int START = 0, IN_ROOT_ELEMENT = 1,
|
@@ -48,48 +46,38 @@ public String getNodeValue(String nodeName) {
|
48 | 46 | }
|
49 | 47 | return null;
|
50 | 48 | }
|
51 |
| - |
52 |
| - public void setDocumentLocator(Locator locator) { |
53 |
| - this.locator = locator; |
54 |
| - } |
55 |
| - |
| 49 | + |
56 | 50 | public void characters(char[] ch, int start, int length)
|
57 | 51 | throws SAXException {
|
58 |
| - String value = null; |
59 | 52 | DeploymentPlanTextNode wtn = null;
|
| 53 | + String name = null; |
| 54 | + |
60 | 55 | switch (state) {
|
61 |
| - case IN_CONTEXT_ROOT: |
62 |
| - value = new String(ch, start, length); |
63 |
| - wtn = new DeploymentPlanTextNode(); |
64 |
| - try { |
65 |
| - int offset = getOffset(locator.getLineNumber(), locator |
66 |
| - .getColumnNumber()); |
67 |
| - wtn.setName(DeploymentPlanTextNode.CONTEXT_ROOT); |
68 |
| - wtn.setValue(value); |
69 |
| - wtn.setOffset(offset - length); |
70 |
| - nodeList.add(wtn); |
71 |
| - } catch (IOException e) { |
72 |
| - e.printStackTrace(); |
73 |
| - } |
74 |
| - break; |
75 |
| - case IN_ARTIFACTID: |
76 |
| - value = new String(ch, start, length); |
77 |
| - wtn = new DeploymentPlanTextNode(); |
78 |
| - try { |
79 |
| - int offset = getOffset(locator.getLineNumber(), locator |
80 |
| - .getColumnNumber()); |
81 |
| - wtn.setName(DeploymentPlanTextNode.ARTIFACT_ID); |
82 |
| - wtn.setValue(value); |
83 |
| - wtn.setOffset(offset - length); |
84 |
| - nodeList.add(wtn); |
85 |
| - |
86 |
| - } catch (IOException e) { |
87 |
| - e.printStackTrace(); |
88 |
| - } |
89 |
| - break; |
90 |
| - default: |
91 |
| - break; |
| 56 | + case IN_CONTEXT_ROOT: |
| 57 | + name = DeploymentPlanTextNode.CONTEXT_ROOT; |
| 58 | + break; |
| 59 | + case IN_ARTIFACTID: |
| 60 | + name = DeploymentPlanTextNode.ARTIFACT_ID; |
| 61 | + break; |
| 62 | + default: |
| 63 | + return; |
92 | 64 | }
|
| 65 | + |
| 66 | + String value = new String(ch, start, length); |
| 67 | + wtn = new DeploymentPlanTextNode(); |
| 68 | + |
| 69 | + wtn.setName(name); |
| 70 | + wtn.setValue(value); |
| 71 | + try { |
| 72 | + //ch doesn't contains XML declare statement at the beginning of deployment plan |
| 73 | + //get the character number of first line |
| 74 | + int xmlDeclareLength = getXMLDeclareLength(); |
| 75 | + wtn.setOffset(xmlDeclareLength + start - 1); |
| 76 | + } catch (IOException e) { |
| 77 | + e.printStackTrace(); |
| 78 | + } |
| 79 | + nodeList.add(wtn); |
| 80 | + |
93 | 81 | }
|
94 | 82 |
|
95 | 83 | public void startElement(String uri, String localName, String qName,
|
@@ -150,25 +138,18 @@ public void endElement(String uri, String localName, String qName)
|
150 | 138 | }
|
151 | 139 | }
|
152 | 140 |
|
153 |
| - // return the offset of the DeploymentPlanTextNode's end |
154 |
| - protected int getOffset(int lineNumber, int columnNumber) |
| 141 | + // return the character number of first line in deployment plan |
| 142 | + protected int getXMLDeclareLength() |
155 | 143 | throws IOException {
|
156 | 144 | BufferedReader br = new BufferedReader(new FileReader(file));
|
157 | 145 |
|
158 |
| - if (lineNumber < 1 || columnNumber < 1) |
159 |
| - return -1; |
160 |
| - |
161 | 146 | int current;
|
162 | 147 | int offset = 0;
|
163 | 148 |
|
164 |
| - for (int i = 1; i < lineNumber;) { |
165 |
| - do { |
166 |
| - current = br.read(); |
167 |
| - offset++; |
168 |
| - } while (current != '\n'); |
169 |
| - i++; |
170 |
| - } |
171 |
| - offset += (columnNumber - 1); |
| 149 | + do { |
| 150 | + current = br.read(); |
| 151 | + offset++; |
| 152 | + } while (current != '\n'); |
172 | 153 |
|
173 | 154 | br.close();
|
174 | 155 |
|
|
0 commit comments