Skip to content

Commit 2edc3fa

Browse files
committed
change to allow validation to thrown SAXException, when no ErrorHandler is set
1 parent 8eaadd7 commit 2edc3fa

6 files changed

Lines changed: 41 additions & 18 deletions

File tree

README.MD

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ see TODO.txt for a tentative roadmap. Releases will be announced on the [STIX
2525
discussion list][list]. A java-stix for a particular schema release will match the
2626
STIX schema version in Semantic Versioning form. Follow up patches of java-stix
2727
for a particular schema release will increment a 4th digit. For example, the first
28-
patch release for java-stix `v1.2.0` would be `v1.2.0.1`. Patches will be worked
28+
patch release for java-stix `v1.2.0` would be `v1.2.0.2`. Patches will be worked
2929
in a branch prior to being tagged and released.
3030

3131
## <a name="releases"></a>Releases
@@ -37,16 +37,16 @@ Releases are distributed via the Maven Central Repository.
3737
<dependency>
3838
<groupId>org.mitre</groupId>
3939
<artifactId>stix</artifactId>
40-
<version>1.2.0.1</version>
40+
<version>1.2.0.2</version>
4141
</dependency>
4242

4343
### <a name="gradle_dependency_info"></a>Gradle:
4444

45-
compile 'org.mitre:stix:1.2.0.1'
45+
compile 'org.mitre:stix:1.2.0.2'
4646

4747
### <a name="ivy__dependency_info"></a>Apache Ivy:
4848

49-
<dependency org="org.mitre" name="stix" rev="1.2.0.1" />
49+
<dependency org="org.mitre" name="stix" rev="1.2.0.2" />
5050

5151
## <a name="snapshots"></a>Snapshots
5252

@@ -69,7 +69,7 @@ Users using Apache Maven for example can simply retrieve java-stix directly via
6969
<dependency>
7070
<groupId>org.mitre</groupId>
7171
<artifactId>stix</artifactId>
72-
<version>1.2.0.1-SNAPSHOT</version>
72+
<version>1.2.0.2-SNAPSHOT</version>
7373
</dependency>
7474
</dependencies>
7575

@@ -356,17 +356,17 @@ Then create a container using the image you just created via:
356356
To retreive the jar archives from the running docker container use following
357357
from the command-line of your docker host, not the container:
358358

359-
docker cp <container id>:/java-stix/build/libs/stix-1.2.0.1-SNAPSHOT-javadoc.jar .
360-
docker cp <container id>:/java-stix/build/libs/stix-1.2.0.1-SNAPSHOT-sources.jar .
361-
docker cp <container id>:/java-stix/build/libs/stix-1.2.0.1-SNAPSHOT.jar .
359+
docker cp <container id>:/java-stix/build/libs/stix-1.2.0.2-SNAPSHOT-javadoc.jar .
360+
docker cp <container id>:/java-stix/build/libs/stix-1.2.0.2-SNAPSHOT-sources.jar .
361+
docker cp <container id>:/java-stix/build/libs/stix-1.2.0.2-SNAPSHOT.jar .
362362

363363
If the containder ID is not obvious, but you can also retrieve it via:
364364

365365
docker ps
366366

367367
An example of retrieving the snapshot jar would be the following:
368368

369-
docker cp 83ad9afb6096:/java-stix/build/libs/stix-1.2.0.1-SNAPSHOT.jar .
369+
docker cp 83ad9afb6096:/java-stix/build/libs/stix-1.2.0.2-SNAPSHOT.jar .
370370

371371
By default the Docker container will build using OpenJdk 7. You can modify
372372
the Dockerfile to other JVM's noted in the file. Please, note Gradle targets

buildSrc/src/main/groovy/org/mitre/stix/GeneratedSourceTransformationTask.groovy

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,16 +304,20 @@ class GeneratedSourceTransformationTask extends DefaultTask {
304304
[
305305
imports:
306306
[
307+
"org.xml.sax.SAXException"
307308
],
308309
template:
309310
"""\
310311
/**
311312
* Validates the XML representation of this \${name} instance
312313
* Returning true indicating a successful validation, false if not.
313314
*
314-
* @return boolean
315+
* @return boolean True If it validates against the schema
316+
* @throws SAXException
317+
* If the a validation ErrorHandler has not been set, and
318+
* validation throws a SAXException
315319
*/
316-
public boolean validate() {
320+
public boolean validate() throws SAXException {
317321
return STIXSchema.getInstance().validate(toXMLString());
318322
}
319323
"""

src/main/java/org/mitre/stix/STIXSchema.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ private STIXSchema() {
9494

9595
try {
9696
schemaResources = patternResolver
97-
.getResources("classpath:schemas/v" + version
98-
+ "/**/*.xsd");
97+
.getResources("classpath:schemas/v" + version + "/**/*.xsd");
9998

10099
prefixSchemaBindings = new HashMap<String, String>();
101100

@@ -192,8 +191,12 @@ public String getVersion() {
192191
*
193192
* @param url
194193
* The URL object for the XML to be validated.
194+
* @return boolean True If the xmlText validates against the schema
195+
* @throws SAXException
196+
* If the a validation ErrorHandler has not been set, and
197+
* validation throws a SAXException
195198
*/
196-
public boolean validate(URL url) {
199+
public boolean validate(URL url) throws SAXException {
197200

198201
String xmlText = null;
199202

@@ -211,8 +214,12 @@ public boolean validate(URL url) {
211214
*
212215
* @param xmlText
213216
* A string of XML text to be validated
217+
* @return boolean True If the xmlText validates against the schema
218+
* @throws SAXException
219+
* If the a validation ErrorHandler has not been set, and
220+
* validation throws a SAXException
214221
*/
215-
public boolean validate(String xmlText) {
222+
public boolean validate(String xmlText) throws SAXException {
216223

217224
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
218225
factory.setNamespaceAware(true);
@@ -251,12 +258,15 @@ public boolean validate(String xmlText) {
251258
try {
252259
validator.validate(new StreamSource(new ByteArrayInputStream(
253260
xmlText.getBytes(StandardCharsets.UTF_8))));
254-
255261
} catch (IOException e) {
256262
throw new RuntimeException(e);
257263
} catch (SAXException e) {
258-
System.out.println("---------\n" + e + "--------\n");
259-
return false;
264+
if (this.validator.getErrorHandler() != null) {
265+
return false;
266+
} else {
267+
//re-throw the SAXException
268+
throw e;
269+
}
260270
}
261271

262272
return true;

src/main/java/org/mitre/stix/examples/CIQIdentity.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.mitre.stix.stix_1.IndicatorsType;
4444
import org.mitre.stix.stix_1.STIXHeaderType;
4545
import org.mitre.stix.stix_1.STIXPackage;
46+
import org.xml.sax.SAXException;
4647

4748
/**
4849
* An example of how to add CIQ Identity information to a STIX Indicator.
@@ -190,6 +191,8 @@ public static void main(String[] args) {
190191

191192
} catch (DatatypeConfigurationException e) {
192193
throw new RuntimeException(e);
194+
} catch (SAXException e) {
195+
e.printStackTrace();
193196
}
194197
}
195198
}

src/main/java/org/mitre/stix/examples/CreationToolMetadata.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.mitre.stix.common_1.StructuredTextType;
2121
import org.mitre.stix.stix_1.STIXHeaderType;
2222
import org.mitre.stix.stix_1.STIXPackage;
23+
import org.xml.sax.SAXException;
2324

2425
/**
2526
* Build a STIX Document with Tool Information
@@ -69,6 +70,8 @@ public static void main(String[] args) {
6970

7071
} catch (DatatypeConfigurationException e) {
7172
throw new RuntimeException(e);
73+
} catch (SAXException e) {
74+
e.printStackTrace();
7275
}
7376
}
7477
}

src/main/java/org/mitre/stix/examples/IndicatorHash.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.mitre.stix.stix_1.IndicatorsType;
3333
import org.mitre.stix.stix_1.STIXHeaderType;
3434
import org.mitre.stix.stix_1.STIXPackage;
35+
import org.xml.sax.SAXException;
3536

3637
/**
3738
* Build a STIX Indicator document containing a File observable with an
@@ -129,6 +130,8 @@ public static void main(String[] args) {
129130

130131
} catch (DatatypeConfigurationException e) {
131132
throw new RuntimeException(e);
133+
} catch (SAXException e) {
134+
e.printStackTrace();
132135
}
133136
}
134137
}

0 commit comments

Comments
 (0)