28
28
package org .citydb .core .database .schema .util ;
29
29
30
30
import org .citydb .core .database .schema .mapping .*;
31
+ import org .citydb .util .xml .SecureXMLProcessors ;
32
+ import org .xml .sax .InputSource ;
31
33
import org .xml .sax .SAXException ;
34
+ import org .xml .sax .XMLReader ;
32
35
33
36
import javax .xml .XMLConstants ;
34
37
import javax .xml .bind .*;
38
+ import javax .xml .parsers .ParserConfigurationException ;
39
+ import javax .xml .parsers .SAXParserFactory ;
35
40
import javax .xml .validation .Schema ;
36
41
import javax .xml .validation .SchemaFactory ;
37
42
import java .io .*;
38
43
import java .net .URL ;
39
44
import java .nio .charset .StandardCharsets ;
45
+ import java .nio .file .Files ;
40
46
41
47
public class SchemaMappingUtil {
42
48
private static SchemaMappingUtil instance ;
@@ -53,56 +59,53 @@ public static synchronized SchemaMappingUtil getInstance() throws JAXBException
53
59
return instance ;
54
60
}
55
61
56
- private SchemaMapping unmarshal (SchemaMapping schemaMapping , Object input ) throws SchemaMappingException , SchemaMappingValidationException , JAXBException {
57
- Unmarshaller um = context .createUnmarshaller ();
58
- um .setAdapter (new AppSchemaAdapter (schemaMapping ));
59
- um .setAdapter (new ComplexAttributeTypeAdapter (schemaMapping ));
60
- um .setAdapter (new ComplexTypeAdapter (schemaMapping ));
61
- um .setAdapter (new FeatureTypeAdapter (schemaMapping ));
62
- um .setAdapter (new ObjectTypeAdapter (schemaMapping ));
63
- um .setListener (new UnmarshalListener ());
62
+ private SchemaMapping unmarshal (SchemaMapping schemaMapping , InputSource input ) throws SchemaMappingException , SchemaMappingValidationException , JAXBException {
63
+ Unmarshaller unmarshaller = context .createUnmarshaller ();
64
+ unmarshaller .setAdapter (new AppSchemaAdapter (schemaMapping ));
65
+ unmarshaller .setAdapter (new ComplexAttributeTypeAdapter (schemaMapping ));
66
+ unmarshaller .setAdapter (new ComplexTypeAdapter (schemaMapping ));
67
+ unmarshaller .setAdapter (new FeatureTypeAdapter (schemaMapping ));
68
+ unmarshaller .setAdapter (new ObjectTypeAdapter (schemaMapping ));
69
+ unmarshaller .setListener (new UnmarshalListener ());
64
70
65
71
// validate schema mapping
66
72
ValidationEvent [] events = new ValidationEvent [1 ];
67
- um .setSchema (readSchema ());
68
- um .setEventHandler (new ValidationEventHandler () {
69
- public boolean handleEvent (ValidationEvent event ) {
70
- events [0 ] = event ;
71
- return false ;
72
- }
73
+ unmarshaller .setSchema (readSchema ());
74
+ unmarshaller .setEventHandler (event -> {
75
+ events [0 ] = event ;
76
+ return false ;
73
77
});
74
78
75
- // unmarshal schema mapping
76
- Object result = null ;
77
- try {
78
- if (input instanceof InputStream )
79
- result = um .unmarshal ((InputStream ) input );
80
- else if (input instanceof Reader )
81
- result = um .unmarshal ((Reader ) input );
79
+ UnmarshallerHandler handler = unmarshaller .getUnmarshallerHandler ();
82
80
83
- if (!(result instanceof SchemaMapping ))
84
- throw new SchemaMappingException ("Failed to unmarshal input resource into a schema mapping." );
85
-
86
- return (SchemaMapping )result ;
87
- } catch (JAXBException e ) {
88
- if (events [0 ] != null )
81
+ try {
82
+ SAXParserFactory saxParserFactory = SecureXMLProcessors .newSAXParserFactory ();
83
+ saxParserFactory .setNamespaceAware (true );
84
+ XMLReader reader = saxParserFactory .newSAXParser ().getXMLReader ();
85
+ reader .setContentHandler (handler );
86
+ reader .parse (input );
87
+ } catch (SAXException | ParserConfigurationException | IOException e ) {
88
+ if (events [0 ] != null ) {
89
89
throw new SchemaMappingValidationException (events [0 ].getMessage ());
90
+ } else {
91
+ throw new SchemaMappingException ("Failed to parse the schema mapping." , e );
92
+ }
93
+ }
90
94
91
- throw e ;
92
- } catch ( Throwable e ) {
93
- if (e . getCause () instanceof SchemaMappingException )
94
- throw ( SchemaMappingException ) e . getCause ( );
95
+ // unmarshal schema mapping
96
+ Object result = handler . getResult ();
97
+ if (!( result instanceof SchemaMapping ) )
98
+ throw new SchemaMappingException ( "Failed to unmarshal input resource into a schema mapping." );
95
99
96
- throw e ;
97
- }
100
+ return (SchemaMapping ) result ;
98
101
}
99
102
100
103
public SchemaMapping unmarshal (SchemaMapping schemaMapping , InputStream inputStream ) throws SchemaMappingException , SchemaMappingValidationException , JAXBException {
101
- return unmarshal (schemaMapping , ( Object ) inputStream );
104
+ return unmarshal (schemaMapping , new InputSource ( inputStream ) );
102
105
}
103
106
104
107
public SchemaMapping unmarshal (InputStream inputStream ) throws SchemaMappingException , SchemaMappingValidationException , JAXBException {
105
- return unmarshal (null , ( Object ) inputStream );
108
+ return unmarshal (null , new InputSource ( inputStream ) );
106
109
}
107
110
108
111
public SchemaMapping unmarshal (SchemaMapping schemaMapping , URL resource ) throws SchemaMappingException , SchemaMappingValidationException , JAXBException {
@@ -118,7 +121,7 @@ public SchemaMapping unmarshal(URL resource) throws SchemaMappingException, Sche
118
121
}
119
122
120
123
public SchemaMapping unmarshal (SchemaMapping schemaMapping , File file ) throws SchemaMappingException , SchemaMappingValidationException , JAXBException {
121
- try (InputStream inputStream = new FileInputStream (file )) {
124
+ try (InputStream inputStream = Files . newInputStream (file . toPath () )) {
122
125
return unmarshal (schemaMapping , inputStream );
123
126
} catch (IOException e ) {
124
127
throw new JAXBException ("Failed to open schema mapping resource from file '" + file .getAbsolutePath () + "'." );
@@ -130,11 +133,7 @@ public SchemaMapping unmarshal(File file) throws SchemaMappingException, SchemaM
130
133
}
131
134
132
135
public SchemaMapping unmarshal (SchemaMapping schemaMapping , String xml ) throws SchemaMappingException , SchemaMappingValidationException , JAXBException {
133
- try (Reader reader = new StringReader (xml )) {
134
- return unmarshal (schemaMapping , reader );
135
- } catch (IOException e ) {
136
- throw new JAXBException ("Failed to open schema mapping resource from XML string." );
137
- }
136
+ return unmarshal (schemaMapping , new InputSource (new StringReader (xml )));
138
137
}
139
138
140
139
public SchemaMapping unmarshal (String xml ) throws SchemaMappingException , SchemaMappingValidationException , JAXBException {
@@ -151,11 +150,9 @@ public void marshal(SchemaMapping schemaMapping, Writer writer, boolean prettyPr
151
150
// validate schema mapping
152
151
ValidationEvent [] events = new ValidationEvent [1 ];
153
152
m .setSchema (readSchema ());
154
- m .setEventHandler (new ValidationEventHandler () {
155
- public boolean handleEvent (ValidationEvent event ) {
156
- events [0 ] = event ;
157
- return false ;
158
- }
153
+ m .setEventHandler (event -> {
154
+ events [0 ] = event ;
155
+ return false ;
159
156
});
160
157
161
158
try {
@@ -178,7 +175,7 @@ public void marshal(SchemaMapping schemaMapping, Writer writer) throws SchemaMap
178
175
}
179
176
180
177
public void marshal (SchemaMapping schemaMapping , File file ) throws SchemaMappingException , SchemaMappingValidationException , JAXBException {
181
- try (Writer writer = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (file ), StandardCharsets .UTF_8 ))) {
178
+ try (Writer writer = new BufferedWriter (new OutputStreamWriter (Files . newOutputStream (file . toPath () ), StandardCharsets .UTF_8 ))) {
182
179
marshal (schemaMapping , writer );
183
180
} catch (UnsupportedEncodingException e ) {
184
181
throw new JAXBException ("Failed to marshal schema mapping." , e );
@@ -189,8 +186,9 @@ public void marshal(SchemaMapping schemaMapping, File file) throws SchemaMapping
189
186
190
187
private Schema readSchema () throws JAXBException {
191
188
try {
192
- return SchemaFactory .newInstance (XMLConstants .W3C_XML_SCHEMA_NS_URI )
193
- .newSchema (SchemaMappingUtil .class .getResource ("/org/citydb/core/database/schema/3dcitydb-schema.xsd" ));
189
+ SchemaFactory schemaFactory = SchemaFactory .newInstance (XMLConstants .W3C_XML_SCHEMA_NS_URI );
190
+ schemaFactory .setProperty (XMLConstants .ACCESS_EXTERNAL_DTD , "" );
191
+ return schemaFactory .newSchema (SchemaMappingUtil .class .getResource ("/org/citydb/core/database/schema/3dcitydb-schema.xsd" ));
194
192
} catch (SAXException e ) {
195
193
throw new JAXBException ("Failed to parse the schema mapping XSD schema. " +
196
194
"Could not find '/org/citydb/core/database/schema/3dcitydb-schema.xsd' on the classpath." , e );
0 commit comments