From 5ee2b78e86ce1614932ca7e19104da6f55a5d3e2 Mon Sep 17 00:00:00 2001 From: Andrew Rueckert Date: Fri, 24 Mar 2017 13:36:48 -0700 Subject: [PATCH 01/10] Use JUL logger instead of System.{out,err}.println --- .../mitre/stix/ValidationErrorHandler.java | 19 +++++++----- .../mitre/stix/ValidationEventHandler.java | 29 ++++++++++--------- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/mitre/stix/ValidationErrorHandler.java b/src/main/java/org/mitre/stix/ValidationErrorHandler.java index c04d133..07eb256 100644 --- a/src/main/java/org/mitre/stix/ValidationErrorHandler.java +++ b/src/main/java/org/mitre/stix/ValidationErrorHandler.java @@ -8,20 +8,23 @@ import org.xml.sax.SAXParseException; import org.xml.sax.SAXException; +import java.util.logging.Logger; + /** * Parsing and validating error handler * * @author nemonik (Michael Joseph Walsh ) * */ public class ValidationErrorHandler implements ErrorHandler { - + private static final Logger LOGGER = Logger.getLogger(ValidationErrorHandler.class.getName()); + private void log(String type, SAXParseException e) { - System.err.println("SAXParseException " + type); - System.err.println("\tPublic ID: " + e.getPublicId()); - System.err.println("\tSystem ID: " + e.getSystemId()); - System.err.println("\tLine : " + e.getLineNumber()); - System.err.println("\tColumn : " + e.getColumnNumber()); - System.err.println("\tMessage : " + e.getMessage()); + LOGGER.warning("SAXParseException " + type); + LOGGER.warning("\tPublic ID: " + e.getPublicId()); + LOGGER.warning("\tSystem ID: " + e.getSystemId()); + LOGGER.warning("\tLine : " + e.getLineNumber()); + LOGGER.warning("\tColumn : " + e.getColumnNumber()); + LOGGER.warning("\tMessage : " + e.getMessage()); } /* (non-Javadoc) @@ -50,4 +53,4 @@ public void warning(SAXParseException e) throws SAXException { log("WARNING", e); throw e; } -} \ No newline at end of file +} diff --git a/src/main/java/org/mitre/stix/ValidationEventHandler.java b/src/main/java/org/mitre/stix/ValidationEventHandler.java index 49d13f6..8b9851d 100644 --- a/src/main/java/org/mitre/stix/ValidationEventHandler.java +++ b/src/main/java/org/mitre/stix/ValidationEventHandler.java @@ -5,26 +5,29 @@ package org.mitre.stix; import javax.xml.bind.ValidationEvent; +import java.util.logging.Logger; public class ValidationEventHandler implements javax.xml.bind.ValidationEventHandler { + private static final Logger LOGGER = Logger.getLogger(ValidationErrorHandler.class.getName()); + /* (non-Javadoc) * @see javax.xml.bind.ValidationEventHandler#handleEvent(javax.xml.bind.ValidationEvent) */ public boolean handleEvent(ValidationEvent event) { - System.out.println(""); - System.out.println("EventT"); - System.out.println("\tSeverity: " + event.getSeverity()); - System.out.println("\tMessage: " + event.getMessage()); - System.out.println("\tLinked Excpetion: " + event.getLinkedException()); - System.out.println("\tLocator"); - System.out.println("\tLine Number: " + LOGGER.info(""); + LOGGER.info("EventT"); + LOGGER.info("\tSeverity: " + event.getSeverity()); + LOGGER.info("\tMessage: " + event.getMessage()); + LOGGER.info("\tLinked Excpetion: " + event.getLinkedException()); + LOGGER.info("\tLocator"); + LOGGER.info("\tLine Number: " + event.getLocator().getLineNumber()); - System.out.println("\tColumn Number: " + LOGGER.info("\tColumn Number: " + event.getLocator().getColumnNumber()); - System.out.println("\tOffset: " + event.getLocator().getOffset()); - System.out.println("\tObject: " + event.getLocator().getObject()); - System.out.println("\tNode: " + event.getLocator().getNode()); - System.out.println("\tURL: " + event.getLocator().getURL()); + LOGGER.info("\tOffset: " + event.getLocator().getOffset()); + LOGGER.info("\tObject: " + event.getLocator().getObject()); + LOGGER.info("\tNode: " + event.getLocator().getNode()); + LOGGER.info("\tURL: " + event.getLocator().getURL()); return true; } -} \ No newline at end of file +} From f68e486315a302e61a585e5f24e1b8c469d5069d Mon Sep 17 00:00:00 2001 From: Kirill Yankov Date: Wed, 16 Aug 2017 23:58:06 +0300 Subject: [PATCH 02/10] made jaxb context cached #12 --- .../org/mitre/stix/DocumentUtilities.java | 53 ++++++++++++------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/src/main/java/org/mitre/stix/DocumentUtilities.java b/src/main/java/org/mitre/stix/DocumentUtilities.java index 2393dc7..348b1bd 100644 --- a/src/main/java/org/mitre/stix/DocumentUtilities.java +++ b/src/main/java/org/mitre/stix/DocumentUtilities.java @@ -35,7 +35,7 @@ /** * A collection of utility helper methods. - * + * * @author nemonik (Michael Joseph Walsh ) */ public class DocumentUtilities { @@ -43,13 +43,23 @@ public class DocumentUtilities { private static final String XML_SCHEMA_INSTANCE = "http://www.w3.org/2001/XMLSchema-instance"; private static final String XML_NAMESPACE = "http://www.w3.org/2000/xmlns/"; + private static final JAXBContext STIX_CONTEXT = initDefaultContext(); + + private static JAXBContext initDefaultContext(){ + try { + return JAXBContext.newInstance("org.mitre.stix.stix_1"); + } catch(JAXBException e) { + throw new RuntimeException("Exception initializing default JAXBContext" , e); + } + } + @SuppressWarnings("unused") private static final Logger LOGGER = Logger .getLogger(DocumentUtilities.class.getName()); /** * Returns a pretty printed String for a JAXBElement - * + * * @param jaxbElement * JAXB representation of an Xml Element to be printed. * @return String containing the XML mark-up. @@ -60,7 +70,7 @@ public static String toXMLString(JAXBElement jaxbElement) { /** * Returns Document that is not formatted for a JAXBElement - * + * * @param jaxbElement * JAXB representation of an XML Element * @return Document. @@ -71,7 +81,7 @@ public static Document toDocument(JAXBElement jaxbElement) { /** * Returns a Document for a JAXBElement - * + * * @param jaxbElement * JAXB representation of an XML Element * @param prettyPrint @@ -82,7 +92,7 @@ public static Document toDocument(JAXBElement jaxbElement, boolean prettyPrint) { Document document = null; - + try { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory .newInstance(); @@ -94,8 +104,13 @@ public static Document toDocument(JAXBElement jaxbElement, document = documentBuilderFactory.newDocumentBuilder() .newDocument(); - JAXBContext jaxbContext = JAXBContext.newInstance(jaxbElement - .getDeclaredType().getPackage().getName()); + String packName = jaxbElement.getDeclaredType().getPackage().getName(); + JAXBContext jaxbContext; + if (packName.startsWith("org.mitre")){ + jaxbContext = STIX_CONTEXT; + } else { + jaxbContext = JAXBContext.newInstance(packName); + } Marshaller marshaller = jaxbContext.createMarshaller(); @@ -126,13 +141,13 @@ public static Document toDocument(JAXBElement jaxbElement, } catch (JAXBException e) { throw new RuntimeException(e); } - + return document; } /** * Returns a String for a JAXBElement - * + * * @param jaxbElement * JAXB representation of an XML Element to be printed. * @param prettyPrint @@ -151,10 +166,10 @@ public static String toXMLString(JAXBElement jaxbElement, /** * Returns String that is not formatted for a Document object representing the * entire XML document. - * + * * @param document * Document object representing the entire XML document - * + * * @return Pretty printed String containing the XML mark-up. */ public static String toXMLString(Document document) { @@ -164,12 +179,12 @@ public static String toXMLString(Document document) { /** * Returns a String for a Document object representing the entire XML * document. - * + * * @param document * Document object representing the entire XML document * @param prettyPrint * True for pretty print, otherwise false - * + * * @return String containing the XML mark-up. */ public static String toXMLString(Document document, boolean prettyPrint) { @@ -219,12 +234,12 @@ private interface ElementVisitor { /** * Used to traverse an XML document. - * + * * @param element * Represents an element in an XML document. * @param visitor * Code to be executed. - * + * */ private final static void traverse(Element element, ElementVisitor visitor) { @@ -248,12 +263,12 @@ private final static void traverse(Element element, ElementVisitor visitor) { * (http://java.net/ * jira/browse/JAXB-103?focusedCommentId=64411&page=com.atlassian * .jira.plugin.system.issuetabpanels%3Acomment-tabpanel#action_64411). - * + * * This helper method based on Reboot's * (http://stackoverflow.com/users/392730/reboot) response to a * stackoverflow question on the subject. I've modified it slightly, but it * will prune down the namespaces to only those used. - * + * * @param document * Document object representing the entire XML document */ @@ -353,7 +368,7 @@ public void visit(Element element) { /** * Creates a Document from XML String - * + * * @param xml * The XML String * @return The Document representation @@ -392,7 +407,7 @@ public static Document toDocument(String xml) { /** * Strips formatting from an XML String - * + * * @param xml * The XML String to reformatted * @return The XML String as on line. From bebcad2cd63977f055b4560311e31210d6af3b46 Mon Sep 17 00:00:00 2001 From: Kirill Yankov Date: Thu, 17 Aug 2017 00:15:20 +0300 Subject: [PATCH 03/10] made StixSchema.getInstance nonblocking --- src/main/java/org/mitre/stix/STIXSchema.java | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/mitre/stix/STIXSchema.java b/src/main/java/org/mitre/stix/STIXSchema.java index f57ae81..cfa4597 100644 --- a/src/main/java/org/mitre/stix/STIXSchema.java +++ b/src/main/java/org/mitre/stix/STIXSchema.java @@ -56,28 +56,25 @@ public class STIXSchema { private String version; - private static STIXSchema instance; - private Map prefixSchemaBindings; private Validator validator; private javax.xml.validation.Schema schema; + private static class SchemaHolder { + public static final STIXSchema instance = new STIXSchema(); + } + /** * Returns STIXSchema object representing the STIX schema. * * @return Always returns a STIXSchema object representing the STIX schema. */ - public synchronized static STIXSchema getInstance() { - - if (instance != null) { - return instance; - } else { - instance = new STIXSchema(); - } - - return instance; + public static STIXSchema getInstance() { + // Here is safe lazy initialization trick, revealed in the book: + // Java Concurrency in Practice, Goetz, 2006. Chapter 16.2.3 + return SchemaHolder.instance; } /** From 19099632ef15622c2d9d7d30a731cf91c5685633 Mon Sep 17 00:00:00 2001 From: Kirill Yankov Date: Fri, 18 Aug 2017 00:30:24 +0300 Subject: [PATCH 04/10] made initialization of JAXBContext lazy and added comments --- .../org/mitre/stix/DocumentUtilities.java | 43 ++++++++++++++----- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/mitre/stix/DocumentUtilities.java b/src/main/java/org/mitre/stix/DocumentUtilities.java index 348b1bd..b374959 100644 --- a/src/main/java/org/mitre/stix/DocumentUtilities.java +++ b/src/main/java/org/mitre/stix/DocumentUtilities.java @@ -43,22 +43,37 @@ public class DocumentUtilities { private static final String XML_SCHEMA_INSTANCE = "http://www.w3.org/2001/XMLSchema-instance"; private static final String XML_NAMESPACE = "http://www.w3.org/2000/xmlns/"; - private static final JAXBContext STIX_CONTEXT = initDefaultContext(); + public static JAXBContext stixJaxbContext() { + // Here is safe lazy initialization trick, revealed in the book: + // Java Concurrency in Practice, Goetz, 2006. Chapter 16.2.3 + return ContextHolder.instance; + } - private static JAXBContext initDefaultContext(){ - try { - return JAXBContext.newInstance("org.mitre.stix.stix_1"); - } catch(JAXBException e) { - throw new RuntimeException("Exception initializing default JAXBContext" , e); - } - } + private static class ContextHolder { + public static final JAXBContext instance = initDefaultContext(); + + private static JAXBContext initDefaultContext(){ + try { + return JAXBContext.newInstance("org.mitre.stix.stix_1"); + } catch(JAXBException e) { + throw new RuntimeException("Exception initializing default JAXBContext" , e); + } + } + + } @SuppressWarnings("unused") private static final Logger LOGGER = Logger .getLogger(DocumentUtilities.class.getName()); /** - * Returns a pretty printed String for a JAXBElement + * Returns a pretty printed String for a JAXBElement. + * + *

+ * !!!NOTE!!! + * This method is optimized for use with elements from STIX schema. + * Use of elements from other schemas may cause serious overhead and perform slowly. + *

* * @param jaxbElement * JAXB representation of an Xml Element to be printed. @@ -69,7 +84,13 @@ public static String toXMLString(JAXBElement jaxbElement) { } /** - * Returns Document that is not formatted for a JAXBElement + * Returns Document that is not formatted for a JAXBElement. + * + *

+ * !!!NOTE!!! + * This method is optimized for use with elements from STIX schema. + * Use of elements from other schemas may cause serious overhead and perform slowly. + *

* * @param jaxbElement * JAXB representation of an XML Element @@ -107,7 +128,7 @@ public static Document toDocument(JAXBElement jaxbElement, String packName = jaxbElement.getDeclaredType().getPackage().getName(); JAXBContext jaxbContext; if (packName.startsWith("org.mitre")){ - jaxbContext = STIX_CONTEXT; + jaxbContext = stixJaxbContext(); } else { jaxbContext = JAXBContext.newInstance(packName); } From efe21c901fe6a30be4c7b218665c3d78cf6cd529 Mon Sep 17 00:00:00 2001 From: Michael Joseph Walsh Date: Fri, 18 Aug 2017 00:29:06 -0400 Subject: [PATCH 05/10] Fix indenting --- .../org/mitre/stix/DocumentUtilities.java | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/mitre/stix/DocumentUtilities.java b/src/main/java/org/mitre/stix/DocumentUtilities.java index b374959..eea8700 100644 --- a/src/main/java/org/mitre/stix/DocumentUtilities.java +++ b/src/main/java/org/mitre/stix/DocumentUtilities.java @@ -68,12 +68,12 @@ private static JAXBContext initDefaultContext(){ /** * Returns a pretty printed String for a JAXBElement. - * - *

- * !!!NOTE!!! - * This method is optimized for use with elements from STIX schema. - * Use of elements from other schemas may cause serious overhead and perform slowly. - *

+ * + *

+ * !!!NOTE!!! + * This method is optimized for use with elements from STIX schema. + * Use of elements from other schemas may cause serious overhead and perform slowly. + *

* * @param jaxbElement * JAXB representation of an Xml Element to be printed. @@ -85,12 +85,12 @@ public static String toXMLString(JAXBElement jaxbElement) { /** * Returns Document that is not formatted for a JAXBElement. - * - *

- * !!!NOTE!!! - * This method is optimized for use with elements from STIX schema. - * Use of elements from other schemas may cause serious overhead and perform slowly. - *

+ * + *

+ * !!!NOTE!!! + * This method is optimized for use with elements from STIX schema. + * Use of elements from other schemas may cause serious overhead and perform slowly. + *

* * @param jaxbElement * JAXB representation of an XML Element From 3882631adad2ffe047bf90635f76f15a3be5f883 Mon Sep 17 00:00:00 2001 From: Michael Joseph Walsh Date: Fri, 18 Aug 2017 00:30:05 -0400 Subject: [PATCH 06/10] Fixed typo --- README.MD | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.MD b/README.MD index 71af681..311fe94 100644 --- a/README.MD +++ b/README.MD @@ -294,7 +294,7 @@ path. To execute the tests: - /gradlew -x signArchives test + ./gradlew -x signArchives test Consider using `-d`/`--debug` or `-i`/`--info` for more details during test execution like so: From 5fc7c60c43f55592488abcc7338687285ecaab31 Mon Sep 17 00:00:00 2001 From: Michael Joseph Walsh Date: Fri, 18 Aug 2017 00:37:24 -0400 Subject: [PATCH 07/10] formatting corrections --- src/main/java/org/mitre/stix/DocumentUtilities.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/org/mitre/stix/DocumentUtilities.java b/src/main/java/org/mitre/stix/DocumentUtilities.java index eea8700..5163565 100644 --- a/src/main/java/org/mitre/stix/DocumentUtilities.java +++ b/src/main/java/org/mitre/stix/DocumentUtilities.java @@ -59,7 +59,6 @@ private static JAXBContext initDefaultContext(){ throw new RuntimeException("Exception initializing default JAXBContext" , e); } } - } @SuppressWarnings("unused") @@ -354,7 +353,6 @@ public void visit(Element element) { namespaces.add(namespace); } } - }); traverse(element, new ElementVisitor() { @@ -383,7 +381,6 @@ public void visit(Element element) { element.removeAttributeNS(XML_NAMESPACE, localName); } } - }); } From d15475cf3e5204becf0cbaa877eefc54598316e6 Mon Sep 17 00:00:00 2001 From: "Michael J. Walsh" Date: Thu, 28 Sep 2017 18:15:12 -0400 Subject: [PATCH 08/10] travis ci not longer supports oraclejdk7 --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index f887409..1bbe0d5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,5 @@ language: java jdk: - - oraclejdk7 - openjdk7 before_install: From 71215fd458dd59a59b44560173807dadbeaa8ee2 Mon Sep 17 00:00:00 2001 From: "Michael J. Walsh" Date: Thu, 28 Sep 2017 18:36:29 -0400 Subject: [PATCH 09/10] need to fall back to precise for travis to build --- .gitignore | 2 ++ .travis.yml | 2 ++ 2 files changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index 27c4384..25a067a 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ src/generated .gradle/2.1/taskArtifacts src/main/resources/namespace-prefix.xjb src/main/resource/schemas +.gradle +buildSrc/.gradle diff --git a/.travis.yml b/.travis.yml index 1bbe0d5..7c15951 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,7 @@ language: java +dist: precise jdk: + - oraclejdk7 - openjdk7 before_install: From 796b1314253fac3ebafca347f6eeb2c51ba4b009 Mon Sep 17 00:00:00 2001 From: "Michael J. Walsh" Date: Thu, 28 Sep 2017 18:52:12 -0400 Subject: [PATCH 10/10] travis precise is not capable of building openjdk7 --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 7c15951..ca2f513 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,6 @@ language: java dist: precise jdk: - oraclejdk7 - - openjdk7 before_install: - chmod +x gradlew