|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
SDLDocumentAdaptor.java | 100% | 100% | 100% | 100% |
|
1 |
// Copyright 2004 The Apache Software Foundation
|
|
2 |
//
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
5 |
// You may obtain a copy of the License at
|
|
6 |
//
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
//
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
// See the License for the specific language governing permissions and
|
|
13 |
// limitations under the License.
|
|
14 |
|
|
15 |
package org.apache.hivemind.sdl;
|
|
16 |
|
|
17 |
import java.io.IOException;
|
|
18 |
import java.util.ArrayList;
|
|
19 |
import java.util.List;
|
|
20 |
|
|
21 |
import org.apache.hivemind.Resource;
|
|
22 |
import org.w3c.dom.Document;
|
|
23 |
import org.w3c.dom.Element;
|
|
24 |
import org.w3c.dom.Node;
|
|
25 |
import org.xml.sax.Attributes;
|
|
26 |
import org.xml.sax.SAXException;
|
|
27 |
import org.xml.sax.SAXParseException;
|
|
28 |
import org.xml.sax.helpers.DefaultHandler;
|
|
29 |
|
|
30 |
/**
|
|
31 |
* An adaptor that allows the content of an SDL file (as a {@link Resource}) to
|
|
32 |
* be parsed into an existing {@link org.w3c.dom.Document}.
|
|
33 |
*
|
|
34 |
* @author Howard Lewis Ship
|
|
35 |
*/
|
|
36 |
public class SDLDocumentAdaptor extends DefaultHandler |
|
37 |
{ |
|
38 |
private Document _document;
|
|
39 |
private List _nodeStack = new ArrayList(); |
|
40 |
|
|
41 | 11 |
public SDLDocumentAdaptor(Document document)
|
42 |
{ |
|
43 | 11 |
_document = document; |
44 | 11 |
_nodeStack.add(document); |
45 |
} |
|
46 |
|
|
47 | 8 |
public void parse(Resource resource) throws SAXParseException, IOException |
48 |
{ |
|
49 | 8 |
SDLResourceParser parser = new SDLResourceParser();
|
50 |
|
|
51 | 8 |
parser.parseResource(resource, this);
|
52 |
} |
|
53 |
|
|
54 | 1067 |
private Node peek()
|
55 |
{ |
|
56 | 1067 |
return (Node) _nodeStack.get(_nodeStack.size() - 1);
|
57 |
} |
|
58 |
|
|
59 | 245 |
public void characters(char[] ch, int start, int length) throws SAXException |
60 |
{ |
|
61 | 245 |
Node currentElement = peek(); |
62 |
|
|
63 | 245 |
String data = new String(ch, start, length);
|
64 |
|
|
65 | 245 |
Node textNode = _document.createCDATASection(data); |
66 |
|
|
67 | 245 |
currentElement.appendChild(textNode); |
68 |
} |
|
69 |
|
|
70 | 822 |
public void endElement(String uri, String localName, String qName) throws SAXException |
71 |
{ |
|
72 | 822 |
pop(); |
73 |
} |
|
74 |
|
|
75 | 822 |
private void pop() |
76 |
{ |
|
77 | 822 |
int count = _nodeStack.size();
|
78 |
|
|
79 | 822 |
_nodeStack.remove(count - 1); |
80 |
} |
|
81 |
|
|
82 | 822 |
public void startElement(String uri, String localName, String qName, Attributes attributes) |
83 |
throws SAXException
|
|
84 |
{ |
|
85 | 822 |
Node parentElement = peek(); |
86 |
|
|
87 |
// The SDL parser provides just localName, never qName
|
|
88 | 822 |
Element newElement = _document.createElement(localName); |
89 |
|
|
90 | 822 |
int count = attributes.getLength();
|
91 | 822 |
for (int i = 0; i < count; i++) |
92 |
{ |
|
93 | 913 |
String name = attributes.getLocalName(i); |
94 | 913 |
String value = attributes.getValue(i); |
95 |
|
|
96 | 913 |
newElement.setAttribute(name, value); |
97 |
} |
|
98 |
|
|
99 | 822 |
parentElement.appendChild(newElement); |
100 |
|
|
101 | 822 |
push(newElement); |
102 |
} |
|
103 |
|
|
104 | 822 |
private void push(Node node) |
105 |
{ |
|
106 | 822 |
_nodeStack.add(node); |
107 |
} |
|
108 |
} |
|
109 |
|
|