|
|||||||||||||||||||
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 | |||||||||||||||
SDLResourceParser.java | 100% | 94.4% | 100% | 95.5% |
|
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.BufferedInputStream;
|
|
18 |
import java.io.IOException;
|
|
19 |
import java.io.InputStream;
|
|
20 |
|
|
21 |
import org.apache.commons.logging.Log;
|
|
22 |
import org.apache.commons.logging.LogFactory;
|
|
23 |
import org.apache.hivemind.HiveMind;
|
|
24 |
import org.apache.hivemind.Resource;
|
|
25 |
import org.apache.hivemind.sdl.parser.SimpleDataLanguageParser;
|
|
26 |
import org.apache.hivemind.sdl.parser.TokenMgrError;
|
|
27 |
import org.xml.sax.ContentHandler;
|
|
28 |
import org.xml.sax.Locator;
|
|
29 |
import org.xml.sax.SAXParseException;
|
|
30 |
|
|
31 |
/**
|
|
32 |
* Wrapper around {@link org.apache.hivemind.sdl.parser.SimpleDataLanguageParser}
|
|
33 |
* (which is generated code). Parses resources that contained Simple Data Language - a language
|
|
34 |
* inspired by XML, but rationalized for storing hiearachical data (a frequent application of XML,
|
|
35 |
* even though XML is better suited for rich document markup than storing of data). SDL
|
|
36 |
* is isomorphic to a subset of XML: It has equivalents for XML elements, XML attributes,
|
|
37 |
* and parsable character data (text within an element), but does not support processing
|
|
38 |
* instructions, namespaces, internal or external entities, doctype, etc.
|
|
39 |
*
|
|
40 |
* @author Howard Lewis Ship
|
|
41 |
*/
|
|
42 |
public class SDLResourceParser |
|
43 |
{ |
|
44 |
private static final Log LOG = LogFactory.getLog(SDLResourceParser.class); |
|
45 |
|
|
46 |
/**
|
|
47 |
* Parses the resource, passing SAX parse events to the handler. SDL does
|
|
48 |
* not (at this time) support namespaces or other constructs of XML, so only
|
|
49 |
* the following methods of ContentHandler will be invoked:
|
|
50 |
* <ul>
|
|
51 |
* <li>{@link ContentHandler#setDocumentLocator(org.xml.sax.Locator)} - before any other calls
|
|
52 |
* <ii>{@link ContentHandler#startDocument()}
|
|
53 |
* <li>{@link ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
|
|
54 |
* - passing only the local name (namespace URL and qname will be null)
|
|
55 |
* <li>{@link ContentHandler#characters(char[], int, int) for literal (and extended literal) strings
|
|
56 |
* <li>{@link ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String) - again, with the namespace
|
|
57 |
* parameters passed as null
|
|
58 |
* <li>{@link ContentHandler#endDocument() - last
|
|
59 |
* </ul>
|
|
60 |
*
|
|
61 |
* <p>
|
|
62 |
* At some future point, SDL may support more XML-like things, including names spaces.
|
|
63 |
*/
|
|
64 | 110 |
public void parseResource(Resource resource, ContentHandler handler) |
65 |
throws SAXParseException, IOException
|
|
66 |
{ |
|
67 | 110 |
BufferedInputStream bis = null;
|
68 | 110 |
SimpleDataLanguageParser parser = null;
|
69 | 110 |
Locator locator = null;
|
70 |
|
|
71 | 110 |
try
|
72 |
{ |
|
73 | 110 |
InputStream rawStream = resource.getResourceURL().openStream(); |
74 |
|
|
75 | 110 |
bis = new BufferedInputStream(rawStream);
|
76 |
|
|
77 | 110 |
parser = new SimpleDataLanguageParser(rawStream);
|
78 | 110 |
locator = parser.getLocator(); |
79 |
|
|
80 | 110 |
parser.parse(handler); |
81 |
|
|
82 | 108 |
bis.close(); |
83 | 108 |
bis = null;
|
84 |
} |
|
85 |
catch (TokenMgrError ex)
|
|
86 |
{ |
|
87 | 1 |
throw new SAXParseException(SDLMessages.parseError(resource, ex), locator); |
88 |
} |
|
89 |
catch (Exception ex)
|
|
90 |
{ |
|
91 | 1 |
throw new SAXParseException(SDLMessages.parseError(resource, ex), locator, ex); |
92 |
} |
|
93 |
finally
|
|
94 |
{ |
|
95 | 110 |
close(resource, bis); |
96 |
} |
|
97 |
} |
|
98 |
|
|
99 | 110 |
private void close(Resource resource, InputStream stream) |
100 |
{ |
|
101 | 110 |
try
|
102 |
{ |
|
103 | 110 |
if (stream != null) |
104 | 2 |
stream.close(); |
105 |
} |
|
106 |
catch (IOException ex)
|
|
107 |
{ |
|
108 | 0 |
LOG.error(SDLMessages.unableToCloseStream(resource, ex), ex); |
109 |
} |
|
110 |
} |
|
111 |
} |
|
112 |
|
|