Example usage for org.xml.sax SAXException SAXException

List of usage examples for org.xml.sax SAXException SAXException

Introduction

In this page you can find the example usage for org.xml.sax SAXException SAXException.

Prototype

public SAXException(Exception e) 

Source Link

Document

Create a new SAXException wrapping an existing exception.

Usage

From source file:nl.mpi.lamus.metadata.implementation.LamusMetadataApiBridgeTest.java

@Test
public void metadataDocumentIsNotValid()
        throws MalformedURLException, IOException, MetadataException, SAXException, URISyntaxException {

    final URL fileURL = new URL("file:/some/location/file.cmdi");

    final SAXException expectedException = new SAXException("some exception message");

    context.checking(new Expectations() {
        {//from  w ww  . ja v a2s. co m
            allowing(mockMetadataDocument).getFileLocation();
            will(returnValue(fileURL.toURI()));
            oneOf(mockMetadataAPI).validateMetadataDocument(with(same(mockMetadataDocument)),
                    with(any(DefaultHandler.class)));
            will(throwException(expectedException));
        }
    });

    boolean result = lamusMetadataApiBridge.isMetadataDocumentValid(mockMetadataDocument);

    assertFalse("Result should be false", result);
}

From source file:nl.nn.adapterframework.align.Json2Xml.java

@Override
public void startParse(JsonValue node) throws SAXException {
    if (node instanceof JsonObject) {
        JsonObject root = (JsonObject) node;
        if (StringUtils.isEmpty(getRootElement())) {
            if (root.isEmpty()) {
                throw new SAXException("no names found");
            }//www . ja va2 s  .co  m
            if (root.size() > 1) {
                String namesList = null;
                int i = 0;
                for (String name : root.keySet()) {
                    if (namesList == null) {
                        namesList = name;
                    } else {
                        namesList += "," + name;
                    }
                    if (i++ > 5) {
                        namesList += ", ...";
                        break;
                    }
                }
                throw new SAXException("too many names [" + namesList + "]");
            }
            setRootElement((String) root.keySet().toArray()[0]);
        }
        // determine somewhat heuristically whether the json contains a 'root' node:
        // if the outermost JsonObject contains only one key, that has the name of the root element, 
        // then we'll assume that that is the root element...
        if (root.size() == 1 && getRootElement().equals(root.keySet().toArray()[0])) {
            node = root.get(getRootElement());
        }
    }
    if (node instanceof JsonArray && !insertElementContainerElements && strictSyntax) {
        throw new SAXException(
                MSG_EXPECTED_SINGLE_ELEMENT + " [" + getRootElement() + "] or array element container");
    }
    super.startParse(node);
}

From source file:nl.nn.adapterframework.align.Json2Xml.java

@Override
public Map<String, String> getAttributes(XSElementDeclaration elementDeclaration, JsonValue node)
        throws SAXException {
    if (!readAttributes) {
        return null;
    }/*from  ww w .  j  a va2 s  . co  m*/
    if (!(node instanceof JsonObject)) {
        if (DEBUG)
            log.debug("getAttributes() parent node is not a JsonObject, but a [" + node.getClass().getName()
                    + "] isParentOfSingleMultipleOccurringChildElement ["
                    + isParentOfSingleMultipleOccurringChildElement() + "]  value [" + node
                    + "], returning null");
        return null;
    }
    JsonObject o = (JsonObject) node;
    if (o.isEmpty()) {
        if (DEBUG)
            log.debug("getAttributes() no children");
        return null;
    }
    try {
        Map<String, String> result = new HashMap<String, String>();
        for (String key : o.keySet()) {
            if (key.startsWith(attributePrefix)) {
                String attributeName = key.substring(attributePrefix.length());
                String value = getText(elementDeclaration, o.get(key));
                if (DEBUG)
                    log.debug("getAttributes() attribute [" + attributeName + "] = [" + value + "]");
                result.put(attributeName, value);
            }
        }
        return result;
    } catch (JsonException e) {
        throw new SAXException(e);
    }
}

From source file:nl.nn.adapterframework.align.Json2Xml.java

@Override
public Set<String> getAllNodeChildNames(XSElementDeclaration elementDeclaration, JsonValue node)
        throws SAXException {
    if (DEBUG)// w w  w .  j  a v  a 2s .  c o  m
        log.debug("getAllChildNames() node isParentOfSingleMultipleOccurringChildElement ["
                + isParentOfSingleMultipleOccurringChildElement() + "] [" + node.getClass().getName() + "]["
                + node + "]");
    try {
        if (isParentOfSingleMultipleOccurringChildElement()) {
            if ((insertElementContainerElements || !strictSyntax) && node instanceof JsonArray) {
                if (DEBUG)
                    log.debug(
                            "getAllChildNames() parentOfSingleMultipleOccurringChildElement,JsonArray,(insertElementContainerElements || !strictSyntax)");
                Set<String> result = new HashSet<String>();
                result.addAll(getMultipleOccurringChildElements());
                if (DEBUG)
                    log.debug("getAllChildNames() isParentOfSingleMultipleOccurringChildElement, result ["
                            + result + "]");
                return result;
            }
            if ((insertElementContainerElements && strictSyntax) && !(node instanceof JsonArray)) {
                throw new SAXException(MSG_FULL_INPUT_IN_STRICT_COMPACTING_MODE);
            }
        }
        if (!(node instanceof JsonObject)) {
            if (DEBUG)
                log.debug("getAllChildNames() parent node is not a JsonObject, but a ["
                        + node.getClass().getName() + "] isParentOfSingleMultipleOccurringChildElement ["
                        + isParentOfSingleMultipleOccurringChildElement() + "]  value [" + node
                        + "], returning null");
            return null;
        }
        JsonObject o = (JsonObject) node;
        if (o.isEmpty()) {
            if (DEBUG)
                log.debug("getAllChildNames() no children");
            return new HashSet<String>();
        }
        Set<String> result = new HashSet<String>();
        for (String key : o.keySet()) {
            if (!readAttributes || !key.startsWith(attributePrefix)) {
                result.add(key);
                if (DEBUG)
                    log.debug("getAllChildNames() key [" + key + "] added to set");
            }
        }
        return result;
    } catch (JsonException e) {
        throw new SAXException(e);
    }
}

From source file:nl.nn.adapterframework.align.Json2Xml.java

@Override
public Iterable<JsonValue> getNodeChildrenByName(JsonValue node, XSElementDeclaration childElementDeclaration)
        throws SAXException {
    String name = childElementDeclaration.getName();
    if (DEBUG)/*  w  ww  .jav  a2  s  .  c  o  m*/
        log.debug("getChildrenByName() childname [" + name + "] isParentOfSingleMultipleOccurringChildElement ["
                + isParentOfSingleMultipleOccurringChildElement() + "] isMultipleOccuringChildElement ["
                + isMultipleOccurringChildElement(name) + "] node [" + node + "]");
    try {
        if (!(node instanceof JsonObject)) {
            if (DEBUG)
                log.debug("getChildrenByName() parent node is not a JsonObject, but a ["
                        + node.getClass().getName() + "]");
            return null;
        }
        JsonObject o = (JsonObject) node;
        if (!o.containsKey(name)) {
            if (DEBUG)
                log.debug("getChildrenByName() no children named [" + name + "] node [" + node + "]");
            return null;
        }
        JsonValue child = o.get(name);
        List<JsonValue> result = new LinkedList<JsonValue>();
        if (child instanceof JsonArray) {
            if (DEBUG)
                log.debug("getChildrenByName() child named [" + name
                        + "] is a JsonArray, current node insertElementContainerElements ["
                        + insertElementContainerElements + "]");
            // if it could be necessary to insert elementContainers, we cannot return them as a list of individual elements now, because then the containing element would be duplicated
            // we also cannot use the isSingleMultipleOccurringChildElement, because it is not valid yet
            if (!isMultipleOccurringChildElement(name)) {
                if (insertElementContainerElements || !strictSyntax) {
                    result.add(child);
                    if (DEBUG)
                        log.debug("getChildrenByName() singleMultipleOccurringChildElement [" + name
                                + "] returning array node (insertElementContainerElements=true)");
                } else {
                    throw new SAXException(MSG_EXPECTED_SINGLE_ELEMENT + " [" + name + "]");
                }
            } else {
                if (DEBUG)
                    log.debug("getChildrenByName() childname [" + name
                            + "] returning elements of array node (insertElementContainerElements=false or not singleMultipleOccurringChildElement)");
                result.addAll((JsonArray) child);
            }
            return result;
        }
        result.add(child);
        if (DEBUG)
            log.debug("getChildrenByName() name [" + name + "] returning [" + child + "]");
        return result;
    } catch (JsonException e) {
        throw new SAXException(e);
    }
}

From source file:nl.nn.adapterframework.align.Json2Xml.java

public static String translate(JsonStructure json, URL schemaURL, boolean compactJsonArrays, String rootElement,
        boolean strictSyntax, boolean deepSearch, String targetNamespace, Map<String, Object> overrideValues)
        throws SAXException, IOException {

    // create the ValidatorHandler
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(schemaURL);
    ValidatorHandler validatorHandler = schema.newValidatorHandler();

    // create the XSModel
    XMLSchemaLoader xsLoader = new XMLSchemaLoader();
    XSModel xsModel = xsLoader.loadURI(schemaURL.toExternalForm());
    List<XSModel> schemaInformation = new LinkedList<XSModel>();
    schemaInformation.add(xsModel);/*  w  w w.j  av a 2s  . c om*/

    // create the validator, setup the chain
    Json2Xml j2x = new Json2Xml(validatorHandler, schemaInformation, compactJsonArrays, rootElement,
            strictSyntax);
    if (overrideValues != null) {
        j2x.setOverrideValues(overrideValues);
    }
    if (targetNamespace != null) {
        //if (DEBUG) System.out.println("setting targetNamespace ["+targetNamespace+"]");
        j2x.setTargetNamespace(targetNamespace);
    }
    j2x.setDeepSearch(deepSearch);
    Source source = j2x.asSource(json);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    String xml = null;
    try {
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.transform(source, result);
        writer.flush();
        xml = writer.toString();
    } catch (TransformerConfigurationException e) {
        SAXException se = new SAXException(e);
        se.initCause(e);
        throw se;
    } catch (TransformerException e) {
        SAXException se = new SAXException(e);
        se.initCause(e);
        throw se;
    }
    return xml;
}

From source file:nl.nn.adapterframework.align.ToXml.java

/**
 * Pushes node through validator./* w w  w  . j  a v a2 s.  co m*/
 * 
 * Must push all nodes through validatorhandler, recursively, respecting the alignment request.
 * Must set current=node before calling validatorHandler.startElement(), in order to get the right argument for the onStartElement / performAlignment callbacks.
 */
public void handleNode(C container, String name, String nodeNamespace) throws SAXException {
    if (DEBUG)
        log.debug("handleNode() name [" + name + "] namespace [" + nodeNamespace + "]");
    N rootNode = getRootNode(container);
    if (StringUtils.isEmpty(nodeNamespace)) {
        nodeNamespace = getNodeNamespaceURI(rootNode);
    }
    XSElementDeclaration elementDeclaration = findElementDeclarationForName(nodeNamespace, name);
    if (elementDeclaration == null) {
        throw new SAXException(MSG_CANNOT_NOT_FIND_ELEMENT_DECLARATION + " for [" + name + "] in namespace ["
                + nodeNamespace + "]");
        //         if (DEBUG) log.debug("node ["+name+"] did not find elementDeclaration, assigning targetNamespace ["+getTargetNamespace()+"]");
        //         nodeNamespace=getTargetNamespace();
    }
    handleElement(elementDeclaration, rootNode);
}

From source file:nl.nn.adapterframework.align.ToXml.java

protected void handleComplexTypedElement(XSElementDeclaration elementDeclaration, N node) throws SAXException {
    String name = elementDeclaration.getName();
    //List<XSParticle> childParticles = getChildElementDeclarations(typeDefinition);
    if (DEBUG)//ww w .  j a  v a 2  s  . c  om
        log.debug("ToXml.handleComplexTypedElement() search for best path for available children of element ["
                + name + "]");
    List<XSParticle> childParticles = getBestChildElementPath(elementDeclaration, node, false);
    if (DEBUG) {
        if (childParticles == null) {
            log.debug("Examined node [" + name + "] deepSearch [" + isDeepSearch() + "] path found is null");
        } else {
            String msg = "Examined node [" + name + "] deepSearch [" + isDeepSearch() + "] found path length ["
                    + childParticles.size() + "]: ";
            boolean tail = false;
            for (XSParticle particle : childParticles) {
                if (tail) {
                    msg += ", ";
                } else {
                    tail = true;
                }
                msg += particle.getTerm().getName();
            }
            log.debug(msg);
        }
    }
    Set<String> processedChildren = new HashSet<String>();

    if (childParticles != null) {
        //         if (DEBUG) log.debug("ToXml.handleComplexTypedElement() iterating over childParticles, size ["+childParticles.size()+"]"); 
        //         if (DEBUG) {
        //            for (int i=0;i<childParticles.size();i++) {
        //               XSParticle childParticle=childParticles.get(i);
        //               XSElementDeclaration childElementDeclaration = (XSElementDeclaration)childParticle.getTerm();
        //               if (DEBUG) log.debug("ToXml.handleComplexTypedElement() list children ["+i+"], name ["+childElementDeclaration.getName()+"]");
        //            }
        //         }
        for (int i = 0; i < childParticles.size(); i++) {
            XSParticle childParticle = childParticles.get(i);
            XSElementDeclaration childElementDeclaration = (XSElementDeclaration) childParticle.getTerm();
            if (DEBUG)
                log.debug("ToXml.handleComplexTypedElement() processing child [" + i + "], name ["
                        + childElementDeclaration.getName() + "]");
            processChildElement(node, name, childElementDeclaration, childParticle.getMinOccurs() > 0,
                    processedChildren);
        }
    }

    Set<String> unProcessedChildren = getUnprocessedChildElementNames(elementDeclaration, node,
            processedChildren);

    if (unProcessedChildren != null && !unProcessedChildren.isEmpty()) {
        Set<String> unProcessedChildrenWorkingCopy = new HashSet<String>(unProcessedChildren);
        log.warn("processing [" + unProcessedChildren.size() + "] unprocessed child elements"
                + (unProcessedChildren.size() > 0 ? ", first [" + unProcessedChildren.iterator().next() + "]"
                        : ""));
        // this loop is required to handle for mixed content element containing globally defined elements
        for (String childName : unProcessedChildrenWorkingCopy) {
            log.warn("processing unprocessed child element [" + childName + "]");
            XSElementDeclaration childElementDeclaration = findElementDeclarationForName(null, childName);
            if (childElementDeclaration == null) {
                // this clause is hit for mixed content element containing elements that are not defined
                throw new SAXException(MSG_CANNOT_NOT_FIND_ELEMENT_DECLARATION + " [" + childName + "]");
            }
            processChildElement(node, name, childElementDeclaration, false, processedChildren);
        }
    }
    // the below is used for mixed content nodes containing text
    if (processedChildren.isEmpty()) {
        if (DEBUG)
            log.debug("ToXml.handleComplexTypedElement() handle element [" + name
                    + "] as simple, because none processed");
        handleSimpleTypedElement(elementDeclaration, null, node);
    }

}

From source file:nl.nn.adapterframework.align.ToXml.java

public void handleError(String msg) throws SAXException {
    ErrorHandler errorHandler = validatorHandler.getErrorHandler();
    if (errorHandler != null) {
        errorHandler.error(new SAXParseException(msg, null));
    } else {//from  w ww .ja va  2s  .  c o  m
        throw new SAXException(msg);
    }
}

From source file:nl.nn.adapterframework.align.ToXml.java

public XSElementDeclaration findElementDeclarationForName(String namespace, String name) throws SAXException {
    Set<XSElementDeclaration> elementDeclarations = findElementDeclarationsForName(namespace, name);
    if (elementDeclarations == null) {
        log.warn("No element declarations found for [" + namespace + "]:[" + name + "]");
        return null;
    }//from www .  j  ava  2 s  .  c  om
    if (elementDeclarations.size() > 1) {
        XSElementDeclaration[] XSElementDeclarationArray = elementDeclarations
                .toArray(new XSElementDeclaration[0]);
        throw new SAXException("multiple [" + elementDeclarations.size() + "] elementDeclarations found for ["
                + namespace + "]:[" + name + "]: first two [" + XSElementDeclarationArray[0].getNamespace()
                + ":" + XSElementDeclarationArray[0].getName() + "]["
                + XSElementDeclarationArray[1].getNamespace() + ":" + XSElementDeclarationArray[1].getName()
                + "]");
    }
    if (elementDeclarations.size() == 1) {
        return (XSElementDeclaration) elementDeclarations.toArray()[0];
    }
    return null;
}