Search in sources :

Example 21 with DOMParser

use of org.apache.xerces.parsers.DOMParser in project ats-framework by Axway.

the class XmlUtils method getDomParser.

private static DOMParser getDomParser() throws SAXNotRecognizedException, SAXNotSupportedException {
    DOMParser parser = new DOMParser();
    // Required settings from the DomParser
    parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false);
    parser.setFeature("http://apache.org/xml/features/continue-after-fatal-error", true);
    parser.setFeature("http://apache.org/xml/features/allow-java-encodings", true);
    return parser;
}
Also used : DOMParser(org.apache.xerces.parsers.DOMParser)

Example 22 with DOMParser

use of org.apache.xerces.parsers.DOMParser in project ats-framework by Axway.

the class XmlUtils method loadXMLFile.

/**
 * Loads an XML file from an InputStream.
 * <br>
 * Note: the source stream is closed internally
 *
 * @param configurationFileStream the source stream
 * @return the loaded XML document
 * @throws IOException for IO error
 * @throws SAXException for parsing exception
 */
public static Document loadXMLFile(InputStream configurationFileStream) throws IOException, SAXException {
    try {
        DOMParser parser = getDomParser();
        parser.parse(new InputSource(configurationFileStream));
        return parser.getDocument();
    } finally {
        IoUtils.closeStream(configurationFileStream);
    }
}
Also used : InputSource(org.xml.sax.InputSource) DOMParser(org.apache.xerces.parsers.DOMParser)

Example 23 with DOMParser

use of org.apache.xerces.parsers.DOMParser in project gate-core by GateNLP.

the class ControllerMetadataViewer method setTarget.

@Override
public void setTarget(Object target) {
    if (target == null)
        throw new NullPointerException("received a null target");
    if (!(target instanceof Controller))
        throw new IllegalArgumentException("not a controller");
    Controller controller = (Controller) target;
    if (!controller.getFeatures().containsKey("gate.app.MetadataURL"))
        throw new IllegalArgumentException("no gate.app.MetadataURL feature");
    try {
        URL metadata = (URL) controller.getFeatures().get("gate.app.MetadataURL");
        URL longDesc = new URL(metadata, "long-desc.html");
        URL iconDesc = new URL(metadata, "icon.png");
        Document document = builder.parse(metadata.openStream());
        Node text = document.getDocumentElement().getElementsByTagName("pipeline-name").item(0).getFirstChild();
        Font font = Gate.getUserConfig().getFont(GateConstants.TEXT_COMPONENTS_FONT);
        StringBuilder page = new StringBuilder();
        page.append("<!DOCTYPE html>");
        page.append("<html>");
        page.append("<head>");
        page.append("<style type='text/css'>body { font-family: ").append(font.getFamily()).append("; font-size: ").append(font.getSize()).append("pt }</style>");
        page.append("</head>");
        page.append("<body>");
        page.append("<h1><img style='vertical-align: middle;' src='").append(StringEscapeUtils.escapeHtml(iconDesc.toString())).append("'/> ").append(StringEscapeUtils.escapeHtml(text.getTextContent())).append("</h1>");
        page.append(IOUtils.toString(longDesc, "UTF-8"));
        page.append("</body></html>");
        // parse using NekoHTML
        HTMLConfiguration config = new HTMLConfiguration();
        // Force element names to lower case to match XHTML requirements
        // as that is what Flying Saucer expects
        config.setProperty("http://cyberneko.org/html/properties/names/elems", "lower");
        DOMParser htmlParser = new DOMParser(config);
        htmlParser.parse(new InputSource(new StringReader(page.toString())));
        display.setDocument(htmlParser.getDocument(), longDesc.toString());
    } catch (Exception e) {
        throw new IllegalArgumentException(e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) Node(org.w3c.dom.Node) HTMLConfiguration(org.cyberneko.html.HTMLConfiguration) Controller(gate.Controller) Document(org.w3c.dom.Document) URL(java.net.URL) Font(java.awt.Font) ResourceInstantiationException(gate.creole.ResourceInstantiationException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) StringReader(java.io.StringReader) DOMParser(org.apache.xerces.parsers.DOMParser)

Example 24 with DOMParser

use of org.apache.xerces.parsers.DOMParser in project muikku by otavanopisto.

the class DeusNexMachinaController method determineEmbeddedAssignmentType.

private WorkspaceMaterialAssignmentType determineEmbeddedAssignmentType(HtmlMaterial material) throws DeusNexException {
    try {
        if (material.getHtml() == null) {
            return null;
        }
        StringReader htmlReader = new StringReader(material.getHtml());
        DOMParser parser = new DOMParser();
        InputSource inputSource = new InputSource(htmlReader);
        parser.parse(inputSource);
        org.w3c.dom.Document domDocument = parser.getDocument();
        List<Element> elements = DeusNexXmlUtils.getElementsByXPath(domDocument.getDocumentElement(), "//iframe[@data-type=\"embedded-document\"]");
        List<WorkspaceMaterialAssignmentType> assignmentTypes = new ArrayList<>();
        if (!elements.isEmpty()) {
            for (Element element : elements) {
                if ("EXERCISE".equals(element.getAttribute("data-assignment-type"))) {
                    assignmentTypes.add(WorkspaceMaterialAssignmentType.EXERCISE);
                }
                if ("EVALUATED".equals(element.getAttribute("data-assignment-type"))) {
                    assignmentTypes.add(WorkspaceMaterialAssignmentType.EVALUATED);
                }
            }
        }
        if (assignmentTypes.isEmpty() || (assignmentTypes.contains(WorkspaceMaterialAssignmentType.EXERCISE) && assignmentTypes.contains(WorkspaceMaterialAssignmentType.EVALUATED))) {
            return null;
        } else {
            return assignmentTypes.get(0);
        }
    } catch (SAXException | IOException | XPathExpressionException e) {
        throw new DeusNexInternalException("Embedded assignment type handling failed. ", e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Element(org.w3c.dom.Element) DeusNexInternalException(fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexInternalException) ArrayList(java.util.ArrayList) WorkspaceMaterialAssignmentType(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialAssignmentType) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) StringReader(java.io.StringReader) DOMParser(org.apache.xerces.parsers.DOMParser)

Example 25 with DOMParser

use of org.apache.xerces.parsers.DOMParser in project alfresco-repository by Alfresco.

the class ActivitiWorkflowEngine method getProcessKey.

private String getProcessKey(InputStream workflowDefinition) throws Exception {
    try {
        InputSource inputSource = new InputSource(workflowDefinition);
        DOMParser parser = new DOMParser();
        parser.parse(inputSource);
        Document document = parser.getDocument();
        NodeList elemnts = document.getElementsByTagName("process");
        if (elemnts.getLength() < 1) {
            throw new IllegalArgumentException("The input stream does not contain a process definition!");
        }
        NamedNodeMap attributes = elemnts.item(0).getAttributes();
        Node idAttrib = attributes.getNamedItem("id");
        if (idAttrib == null) {
            throw new IllegalAccessError("The process definition does not have an id!");
        }
        if (activitiUtil.isMultiTenantWorkflowDeploymentEnabled()) {
            // Workflow-definition is deployed tenant-aware, key should be altered
            return factory.getDomainProcessKey(idAttrib.getNodeValue());
        } else {
            return idAttrib.getNodeValue();
        }
    } finally {
        workflowDefinition.close();
    }
}
Also used : InputSource(org.xml.sax.InputSource) NamedNodeMap(org.w3c.dom.NamedNodeMap) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) DOMParser(org.apache.xerces.parsers.DOMParser) Document(org.w3c.dom.Document)

Aggregations

DOMParser (org.apache.xerces.parsers.DOMParser)25 InputSource (org.xml.sax.InputSource)14 Node (org.w3c.dom.Node)10 Document (org.w3c.dom.Document)9 StringReader (java.io.StringReader)5 Element (org.w3c.dom.Element)5 NodeList (org.w3c.dom.NodeList)5 SAXException (org.xml.sax.SAXException)5 IOException (java.io.IOException)4 HTMLConfiguration (org.cyberneko.html.HTMLConfiguration)4 ArrayList (java.util.ArrayList)3 HtmlMaterial (fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial)2 WorkspaceMaterial (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial)2 WorkspaceNode (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode)2 FileNotFoundException (java.io.FileNotFoundException)2 Transformer (javax.xml.transform.Transformer)2 XIncludeParserConfiguration (org.apache.xerces.parsers.XIncludeParserConfiguration)2 DTDConfiguration (org.cyberneko.dtd.DTDConfiguration)2 NamedNodeMap (org.w3c.dom.NamedNodeMap)2 AcsFileFinder (alma.acs.makesupport.AcsFileFinder)1