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 f887409..ca2f513 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: java +dist: precise jdk: - oraclejdk7 - - openjdk7 before_install: - chmod +x gradlew 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: diff --git a/src/main/java/org/mitre/stix/DocumentUtilities.java b/src/main/java/org/mitre/stix/DocumentUtilities.java index 2393dc7..5163565 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,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/"; + 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 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. * @return String containing the XML mark-up. @@ -59,8 +83,14 @@ 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 * @return Document. @@ -71,7 +101,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 +112,7 @@ public static Document toDocument(JAXBElement jaxbElement, boolean prettyPrint) { Document document = null; - + try { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory .newInstance(); @@ -94,8 +124,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 = stixJaxbContext(); + } else { + jaxbContext = JAXBContext.newInstance(packName); + } Marshaller marshaller = jaxbContext.createMarshaller(); @@ -126,13 +161,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 +186,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 +199,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 +254,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 +283,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 */ @@ -318,7 +353,6 @@ public void visit(Element element) { namespaces.add(namespace); } } - }); traverse(element, new ElementVisitor() { @@ -347,13 +381,12 @@ public void visit(Element element) { element.removeAttributeNS(XML_NAMESPACE, localName); } } - }); } /** * Creates a Document from XML String - * + * * @param xml * The XML String * @return The Document representation @@ -392,7 +425,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. 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; } /** 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 +}