13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
16
- package org .metafacture .jdom ;
17
16
18
- import java .io .IOException ;
19
- import java .util .HashMap ;
20
- import java .util .Map ;
21
- import java .util .Map .Entry ;
22
- import java .util .Properties ;
23
- import java .util .regex .Pattern ;
17
+ package org .metafacture .jdom ;
24
18
25
- import org .jdom2 .Document ;
26
- import org .jdom2 .Element ;
27
- import org .jdom2 .Namespace ;
28
19
import org .metafacture .commons .ResourceUtil ;
29
20
import org .metafacture .framework .FluxCommand ;
30
21
import org .metafacture .framework .MetafactureException ;
35
26
import org .metafacture .framework .annotations .Out ;
36
27
import org .metafacture .framework .helpers .DefaultStreamPipe ;
37
28
29
+ import org .jdom2 .Document ;
30
+ import org .jdom2 .Element ;
31
+ import org .jdom2 .Namespace ;
32
+
33
+ import java .io .IOException ;
34
+ import java .util .HashMap ;
35
+ import java .util .Map .Entry ;
36
+ import java .util .Map ;
37
+ import java .util .Properties ;
38
+ import java .util .regex .Pattern ;
39
+
38
40
/**
39
41
* Converts a stream into a {@link Document}.
40
42
*
@@ -54,34 +56,33 @@ public final class StreamToJDomDocument
54
56
private Document document ;
55
57
private Element currentElement ;
56
58
private final String rootTagName ;
57
- private final Map <String , Namespace > namespaces = new HashMap <String ,Namespace >();
59
+ private final Map <String , Namespace > namespaces = new HashMap <String , Namespace >();
58
60
59
61
public StreamToJDomDocument (final String rootTagName , final String namespaceProperties ) {
60
62
this .rootTagName = rootTagName ;
61
63
namespaces .put (XML , Namespace .getNamespace (XML , "http://www.w3.org/XML/1998/namespace" ));
62
64
final Properties properties ;
63
65
try {
64
66
properties = ResourceUtil .loadProperties (namespaceProperties );
65
- } catch (IOException e ) {
67
+ }
68
+ catch (final IOException e ) {
66
69
throw new MetafactureException ("Cannot load namespaces list" , e );
67
70
}
68
- for (Entry <Object , Object > entry : properties .entrySet ()) {
71
+ for (final Entry <Object , Object > entry : properties .entrySet ()) {
69
72
namespaces .put (entry .getKey ().toString (), Namespace .getNamespace (entry .getKey ().toString (), entry .getValue ().toString ()));
70
73
}
71
74
}
72
75
73
-
74
76
@ Override
75
77
public void startRecord (final String identifier ) {
76
78
assert !isClosed ();
77
79
currentElement = createElement (rootTagName );
78
- for (Namespace namespace : namespaces .values ()) {
80
+ for (final Namespace namespace : namespaces .values ()) {
79
81
currentElement .addNamespaceDeclaration (namespace );
80
82
}
81
83
document = new Document (currentElement );
82
84
}
83
85
84
-
85
86
@ Override
86
87
public void startEntity (final String name ) {
87
88
assert !isClosed ();
@@ -98,36 +99,37 @@ private Element createElement(final String name) {
98
99
return new Element (name );
99
100
}
100
101
101
- private Namespace getNamespace (final String name ){
102
+ private Namespace getNamespace (final String name ) {
102
103
final Namespace namespace = namespaces .get (name );
103
- if (namespace == null ){
104
+ if (namespace == null ) {
104
105
throw new IllegalArgumentException ("Namespace " + name + " not registered" );
105
106
}
106
107
return namespace ;
107
108
}
108
109
109
-
110
110
@ Override
111
111
public void endEntity () {
112
112
assert !isClosed ();
113
113
currentElement = currentElement .getParentElement ();
114
114
}
115
115
116
-
117
116
@ Override
118
117
public void literal (final String name , final String value ) {
119
118
assert !isClosed ();
120
119
if (name .isEmpty ()) {
121
120
currentElement .addContent (value );
122
- } else if (name .startsWith (ATTRIBUTE_MARKER )) {
121
+ }
122
+ else if (name .startsWith (ATTRIBUTE_MARKER )) {
123
123
final String [] parts = NAMESPACE_DELIMITER .split (name );
124
124
if (parts .length == 2 ) {
125
125
currentElement .setAttribute (parts [1 ], value , getNamespace (parts [0 ].substring (1 )));
126
- } else {
126
+ }
127
+ else {
127
128
currentElement .setAttribute (name .substring (1 ), value );
128
129
}
129
130
130
- } else {
131
+ }
132
+ else {
131
133
final Element temp = createElement (name );
132
134
currentElement .addContent (temp );
133
135
temp .setText (value );
0 commit comments