Search in sources :

Example 41 with ParserConfigurationException

use of javax.xml.parsers.ParserConfigurationException in project OpenAM by OpenRock.

the class MarshallerImpl method marshal.

public void marshal(Object obj, Result result) throws JAXBException {
    //XMLSerializable so = Util.toXMLSerializable(obj);
    XMLSerializable so = context.getGrammarInfo().castToXMLSerializable(obj);
    if (so == null)
        throw new MarshalException(Messages.format(Messages.NOT_MARSHALLABLE));
    if (result instanceof SAXResult) {
        write(so, ((SAXResult) result).getHandler());
        return;
    }
    if (result instanceof DOMResult) {
        Node node = ((DOMResult) result).getNode();
        if (node == null) {
            try {
                DocumentBuilder db = XMLUtils.getSafeDocumentBuilder(false);
                Document doc = db.newDocument();
                ((DOMResult) result).setNode(doc);
                write(so, new SAX2DOMEx(doc));
            } catch (ParserConfigurationException pce) {
                throw new JAXBAssertionError(pce);
            }
        } else {
            write(so, new SAX2DOMEx(node));
        }
        return;
    }
    if (result instanceof StreamResult) {
        StreamResult sr = (StreamResult) result;
        XMLWriter w = null;
        if (sr.getWriter() != null)
            w = createWriter(sr.getWriter());
        else if (sr.getOutputStream() != null)
            w = createWriter(sr.getOutputStream());
        else if (sr.getSystemId() != null) {
            String fileURL = sr.getSystemId();
            if (fileURL.startsWith("file:///")) {
                if (fileURL.substring(8).indexOf(":") > 0)
                    fileURL = fileURL.substring(8);
                else
                    fileURL = fileURL.substring(7);
            }
            try {
                w = createWriter(new FileOutputStream(fileURL));
            } catch (IOException e) {
                throw new MarshalException(e);
            }
        }
        if (w == null)
            throw new IllegalArgumentException();
        write(so, w);
        return;
    }
    // unsupported parameter type
    throw new MarshalException(Messages.format(Messages.UNSUPPORTED_RESULT));
}
Also used : MarshalException(javax.xml.bind.MarshalException) DOMResult(javax.xml.transform.dom.DOMResult) StreamResult(javax.xml.transform.stream.StreamResult) Node(org.w3c.dom.Node) IOException(java.io.IOException) Document(org.w3c.dom.Document) XMLWriter(com.sun.xml.bind.marshaller.XMLWriter) SAXResult(javax.xml.transform.sax.SAXResult) JAXBAssertionError(com.sun.xml.bind.JAXBAssertionError) DocumentBuilder(javax.xml.parsers.DocumentBuilder) FileOutputStream(java.io.FileOutputStream) SAX2DOMEx(com.sun.xml.bind.marshaller.SAX2DOMEx) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 42 with ParserConfigurationException

use of javax.xml.parsers.ParserConfigurationException in project OpenAM by OpenRock.

the class MarshallerImpl method marshal.

public void marshal(Object obj, Result result) throws JAXBException {
    //XMLSerializable so = Util.toXMLSerializable(obj);
    XMLSerializable so = context.getGrammarInfo().castToXMLSerializable(obj);
    if (so == null)
        throw new MarshalException(Messages.format(Messages.NOT_MARSHALLABLE));
    if (result instanceof SAXResult) {
        write(so, ((SAXResult) result).getHandler());
        return;
    }
    if (result instanceof DOMResult) {
        Node node = ((DOMResult) result).getNode();
        if (node == null) {
            try {
                DocumentBuilder db = XMLUtils.getSafeDocumentBuilder(false);
                Document doc = db.newDocument();
                ((DOMResult) result).setNode(doc);
                write(so, new SAX2DOMEx(doc));
            } catch (ParserConfigurationException pce) {
                throw new JAXBAssertionError(pce);
            }
        } else {
            write(so, new SAX2DOMEx(node));
        }
        return;
    }
    if (result instanceof StreamResult) {
        StreamResult sr = (StreamResult) result;
        XMLWriter w = null;
        if (sr.getWriter() != null)
            w = createWriter(sr.getWriter());
        else if (sr.getOutputStream() != null)
            w = createWriter(sr.getOutputStream());
        else if (sr.getSystemId() != null) {
            String fileURL = sr.getSystemId();
            if (fileURL.startsWith("file:///")) {
                if (fileURL.substring(8).indexOf(":") > 0)
                    fileURL = fileURL.substring(8);
                else
                    fileURL = fileURL.substring(7);
            }
            try {
                w = createWriter(new FileOutputStream(fileURL));
            } catch (IOException e) {
                throw new MarshalException(e);
            }
        }
        if (w == null)
            throw new IllegalArgumentException();
        write(so, w);
        return;
    }
    // unsupported parameter type
    throw new MarshalException(Messages.format(Messages.UNSUPPORTED_RESULT));
}
Also used : MarshalException(javax.xml.bind.MarshalException) DOMResult(javax.xml.transform.dom.DOMResult) StreamResult(javax.xml.transform.stream.StreamResult) Node(org.w3c.dom.Node) IOException(java.io.IOException) Document(org.w3c.dom.Document) XMLWriter(com.sun.xml.bind.marshaller.XMLWriter) SAXResult(javax.xml.transform.sax.SAXResult) JAXBAssertionError(com.sun.xml.bind.JAXBAssertionError) DocumentBuilder(javax.xml.parsers.DocumentBuilder) FileOutputStream(java.io.FileOutputStream) SAX2DOMEx(com.sun.xml.bind.marshaller.SAX2DOMEx) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 43 with ParserConfigurationException

use of javax.xml.parsers.ParserConfigurationException in project OpenAM by OpenRock.

the class AMViewConfig method parseDocument.

private Document parseDocument(String fileName) {
    Document document = null;
    InputStream is = getClass().getClassLoader().getResourceAsStream(fileName);
    try {
        DocumentBuilder documentBuilder = XMLUtils.getSafeDocumentBuilder(false);
        document = documentBuilder.parse(is);
    } catch (UnsupportedEncodingException e) {
        AMModelBase.debug.error("AMViewConfig.parseDocument", e);
    } catch (ParserConfigurationException e) {
        AMModelBase.debug.error("AMViewConfig.parseDocument", e);
    } catch (SAXException e) {
        AMModelBase.debug.error("AMViewConfig.parseDocument", e);
    } catch (IOException e) {
        AMModelBase.debug.error("AMViewConfig.parseDocument", e);
    }
    return document;
}
Also used : DocumentBuilder(javax.xml.parsers.DocumentBuilder) InputStream(java.io.InputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException)

Example 44 with ParserConfigurationException

use of javax.xml.parsers.ParserConfigurationException in project OpenAM by OpenRock.

the class SmsJsonConverter method getHiddenAttributeNames.

protected List<String> getHiddenAttributeNames() {
    ArrayList<String> hiddenAttributeNames = null;
    try {
        InputStream resource = getClass().getClassLoader().getResourceAsStream("amConsoleConfig.xml");
        Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(resource);
        NodeList nodes = (NodeList) XPathFactory.newInstance().newXPath().evaluate("//consoleconfig/servicesconfig/consoleservice/@realmEnableHideAttrName", doc, XPathConstants.NODESET);
        String rawList = nodes.item(0).getNodeValue();
        hiddenAttributeNames = new ArrayList<String>(Arrays.asList(rawList.split(" ")));
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return hiddenAttributeNames;
}
Also used : InputStream(java.io.InputStream) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException)

Example 45 with ParserConfigurationException

use of javax.xml.parsers.ParserConfigurationException in project OpenAM by OpenRock.

the class SmsServerPropertiesResource method getSchema.

private JsonValue getSchema(Properties syntaxProperties, Properties titleProperties, boolean isDefault) {
    JsonValue template = json(object());
    for (String tabName : getTabNames()) {
        try {
            Document propertySheet = getPropertySheet(tabName);
            Map<String, Set<String>> options = getOptions(propertySheet, tabName);
            List<String> sectionNames = getSectionNames(propertySheet);
            Set<String> optionalAttributes = getOptionalAttributes(propertySheet, tabName);
            int sectionOrder = 0;
            for (String sectionName : sectionNames) {
                final String sectionPath = "/properties/" + tabName + "/" + sectionName;
                template.putPermissive(new JsonPointer(sectionPath + "/title"), titleProperties.getProperty(sectionName));
                template.putPermissive(new JsonPointer(sectionPath + "/propertyOrder"), sectionOrder++);
                int attributeOrder = 0;
                for (String attributeName : getAttributeNamesForSection(sectionName, propertySheet)) {
                    final String title = titleProperties.getProperty(attributeName);
                    String property = syntaxProperties.getProperty(attributeName);
                    if (property == null) {
                        property = "";
                    }
                    final String type = syntaxRawToReal.get(property);
                    final Set<String> attributeOptions = getAttributeOptions(options, attributeName, type);
                    final boolean isOptional;
                    if (isDefault) {
                        isOptional = optionalAttributes.contains(attributeName);
                    } else {
                        isOptional = true;
                    }
                    final String path = sectionPath + "/" + attributeName;
                    if (attributeOptions != null && !attributeOptions.isEmpty()) {
                        template.putPermissive(new JsonPointer(path + "/type/enum"), attributeOptions);
                    } else {
                        template.putPermissive(new JsonPointer(path + "/type"), type);
                    }
                    template.putPermissive(new JsonPointer(path + "/title"), title);
                    template.putPermissive(new JsonPointer(path + "/propertyOrder"), attributeOrder++);
                    template.putPermissive(new JsonPointer(path + "/required"), !isOptional);
                    template.putPermissive(new JsonPointer(path + "/pattern"), ".+");
                    allAttributeNamesInNamedTabs.add(attributeName);
                }
            }
        } catch (ParserConfigurationException | IOException | XPathExpressionException | SAXException e) {
            logger.error("Error reading property sheet for tab " + tabName, e);
        }
    }
    return template;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) XPathExpressionException(javax.xml.xpath.XPathExpressionException) JsonValue(org.forgerock.json.JsonValue) JsonPointer(org.forgerock.json.JsonPointer) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1435 SAXException (org.xml.sax.SAXException)1039 IOException (java.io.IOException)951 Document (org.w3c.dom.Document)751 DocumentBuilder (javax.xml.parsers.DocumentBuilder)687 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)622 Element (org.w3c.dom.Element)389 InputSource (org.xml.sax.InputSource)260 NodeList (org.w3c.dom.NodeList)248 Node (org.w3c.dom.Node)225 SAXParser (javax.xml.parsers.SAXParser)185 File (java.io.File)171 InputStream (java.io.InputStream)170 TransformerException (javax.xml.transform.TransformerException)167 SAXParserFactory (javax.xml.parsers.SAXParserFactory)144 ByteArrayInputStream (java.io.ByteArrayInputStream)141 StringReader (java.io.StringReader)127 ArrayList (java.util.ArrayList)122 DOMSource (javax.xml.transform.dom.DOMSource)114 StreamResult (javax.xml.transform.stream.StreamResult)98