当前位置: 首页>>代码示例>>Java>>正文


Java XMLReaders类代码示例

本文整理汇总了Java中org.jdom2.input.sax.XMLReaders的典型用法代码示例。如果您正苦于以下问题:Java XMLReaders类的具体用法?Java XMLReaders怎么用?Java XMLReaders使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


XMLReaders类属于org.jdom2.input.sax包,在下文中一共展示了XMLReaders类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: merge

import org.jdom2.input.sax.XMLReaders; //导入依赖的package包/类
/**
 * Merges a given XML file with a patch. This API is designed for CobiGen
 * @see com.github.maybeec.lexeme.LeXeMerger#merge(Element, Element, ConflictHandlingType)
 * @param base
 *            File
 * @param patch
 *            String
 * @param charSet
 *            target charset of the file to be read and write
 * @param conflictHandling
 *            {@link ConflictHandlingType} specifying how conflicts will be handled during the merge
 *            process. If null the default for this LeXeMerger will be used
 * @return Document the merged result of base and patch
 * @throws XMLMergeException
 *             when the Documents can't be properly merged
 * @author sholzer (23.04.2015)
 */
public Document merge(File base, String patch, String charSet, ConflictHandlingType conflictHandling)
    throws XMLMergeException {
    if (conflictHandling == null) {
        conflictHandling = conflictHandlingType;
    }
    try {
        SAXBuilder builder = new SAXBuilder(XMLReaders.NONVALIDATING);
        builder.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        String baseString = JDom2Util.getInstance().readFile(base.getPath(), charSet);
        Document baseDoc =
            builder.build(new InputSource(new BufferedReader(new StringReader(baseString))));
        Document patchDoc = builder.build(new InputSource(new BufferedReader(new StringReader(patch))));
        return merge(baseDoc, patchDoc, conflictHandling);
    } catch (IOException | JDOMException e) {
        logger.error("Caught unexcpected {}:{}", e.getClass().getName(), e.getMessage());
        throw new XMLMergeException(e.getMessage());
    }

}
 
开发者ID:maybeec,项目名称:lexeme,代码行数:37,代码来源:LeXeMerger.java

示例2: parseXHTMLDocument

import org.jdom2.input.sax.XMLReaders; //导入依赖的package包/类
public static Document parseXHTMLDocument(String xhtml, JDOMFactory factory) throws IOException, JDOMException
{
    //DTD ersetzen, da die originale nicht erreichbar bzw. nur sehr langsam ist,
    xhtml = xhtml.replace("http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd", "http://localhost:8777/dtd/www.w3.org/TR/xhtml11/DTD/xhtml11.dtd");
    ByteArrayInputStream bais = new ByteArrayInputStream(xhtml.getBytes("UTF-8"));
    SAXBuilder builder = new SAXBuilder(XMLReaders.NONVALIDATING);
    builder.setFeature("http://xml.org/sax/features/external-general-entities", false);
    builder.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
    builder.setFeature("http://xml.org/sax/features/resolve-dtd-uris", false);
    builder.setFeature("http://xml.org/sax/features/validation", false);
    builder.setExpandEntities(false);
    if (factory != null)
    {
        builder.setJDOMFactory(factory);
    }
    Document document = builder.build(bais);
    return document;
}
 
开发者ID:finanzer,项目名称:epubfx,代码行数:19,代码来源:XHTMLUtils.java

示例3: importXml

import org.jdom2.input.sax.XMLReaders; //导入依赖的package包/类
protected void importXml(String text) {
    if (isNotBlank(text)) {
        SAXBuilder builder = new SAXBuilder();
        builder.setXMLReaderFactory(XMLReaders.NONVALIDATING);
        builder.setFeature("http://xml.org/sax/features/validation", false);
        try {
            Document document = builder.build(new StringReader(text));
            String rootName = document.getRootElement().getName();
            if (rootName.equals("definitions")) {
                importFromWsdl(text);
            } else if (rootName.equals("schema")) {
                importFromXsd(text);
            } else {
                Notification note = new Notification("Unrecognized Content", "The XML file has a root element of " + rootName
                        + ", but expected \"definitions\" for WSDL or \"schema\" for XSD.");
                note.show(Page.getCurrent());
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
 
开发者ID:JumpMind,项目名称:metl,代码行数:23,代码来源:ImportXmlTemplateWindow.java

示例4: buildXpathChoices

import org.jdom2.input.sax.XMLReaders; //导入依赖的package包/类
protected void buildXpathChoices() {
    SAXBuilder builder = new SAXBuilder();
    builder.setXMLReaderFactory(XMLReaders.NONVALIDATING);
    builder.setFeature("http://xml.org/sax/features/validation", false);
    Setting setting = component.findSetting(XmlFormatter.XML_FORMATTER_TEMPLATE);
    xpathChoices = new TreeSet<String>();
    if (StringUtils.isNotBlank(setting.getValue())) {
        try {
            Document document = builder.build(new StringReader(setting.getValue()));
            
            buildXpathChoicesFromElement("/" + document.getRootElement().getName(),
                    document.getRootElement());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    ComboBox combo = (ComboBox) grid.getColumn("xpath").getEditorField();
    combo.removeAllItems();
    combo.addItems(xpathChoices);
}
 
开发者ID:JumpMind,项目名称:metl,代码行数:22,代码来源:EditXmlFormatPanel.java

示例5: parseVisualTree

import org.jdom2.input.sax.XMLReaders; //导入依赖的package包/类
private VisualTreeNode parseVisualTree(ComponentContainer parent, Reader inputStream, Object eventHandlerTarget) throws ParserException {

		SAXBuilder builder = new SAXBuilder(XMLReaders.NONVALIDATING);

		try {
			Document doc = builder.build(inputStream);
			//FIXME: I think this is not needed with XSD schema. Please use schema validation instead!
			if (!doc.hasRootElement() && (doc.getRootElement().getName().equalsIgnoreCase(Constants.XAADIN_ROOT_ELEMENT_NAME))) {
				throw new ParserException("could not find root rootElement with name xaadin");
			}

			Element rootElement = doc.getRootElement();
			xaadinNamespace = getDefaultXaadinNamespace(rootElement);
			List<Element> childrenList = rootElement.getChildren();
			if (childrenList.isEmpty()) {
				throw new ParserException("xaadin root element is empty");
			}
			return parseVisualTreeInt(childrenList.get(0), null, eventHandlerTarget, parent);
		} catch (Exception e) {
			throw new ParserException(e);
		}
	}
 
开发者ID:xaadin,项目名称:xaadin,代码行数:23,代码来源:Parser.java

示例6: doFilter

import org.jdom2.input.sax.XMLReaders; //导入依赖的package包/类
private LinkedList<String> doFilter(String in, Set<String> xpaths) throws IOException {
    LinkedList<String> result = new LinkedList<String>();
    try {
        Document doc = new SAXBuilder(XMLReaders.NONVALIDATING).build(new StringReader(in));
        XMLOutputter out = new XMLOutputter();

        for (String xp : xpaths) {
            XPathExpression<Content> xpath = XPathFactory.instance().compile(xp, Filters.content());
            for (Content node : xpath.evaluate(doc)) {
                if(node instanceof Element) {
                    result.add(out.outputString((Element) node));
                } else if(node instanceof Text) {
                    result.add(out.outputString((Text) node));
                }
            }
        }
        return result;
    } catch (JDOMException xpe) {
        throw new IllegalArgumentException("error while processing xpath expressions: '" + xpaths + "'", xpe);
    }
}
 
开发者ID:apache,项目名称:marmotta,代码行数:22,代码来源:XPathFunction.java

示例7: parseLauncherTemplate

import org.jdom2.input.sax.XMLReaders; //导入依赖的package包/类
protected void parseLauncherTemplate(JnlpTemplate launcher) throws ServletErrorException, IOException {

        // Parse JNLP launcher as JDOM
        Element rootElt = null;
        BufferedReader reader = null;

        try {
            // Assume the template has UTF-8 encoding
            reader = new BufferedReader(
                new InputStreamReader(launcher.realPathURL.toURL().openConnection().getInputStream(), "UTF-8"));

            rootElt = new SAXBuilder(XMLReaders.NONVALIDATING, null, null).build(reader).getRootElement();
        } catch (JDOMException e) {
            throw new ServletErrorException(HttpServletResponse.SC_NOT_ACCEPTABLE, "Can't parse launcher template", e);
        } finally {
            FileUtil.safeClose(reader);
        }

        if (!rootElt.getName().equals(JNLP_TAG_ELT_ROOT)) {
            throw new ServletErrorException(HttpServletResponse.SC_NOT_ACCEPTABLE, "Invalid JNLP launcher template");
        }

        launcher.rootElt = rootElt;
    }
 
开发者ID:nroduit,项目名称:weasis-pacs-connector,代码行数:25,代码来源:JnlpLauncher.java

示例8: loadFromFile

import org.jdom2.input.sax.XMLReaders; //导入依赖的package包/类
@MCRCommand(syntax = "load mods document from file {0} for project {1}",
    help = "Load MODS document {0} as MyCoRe Object for project {1}",
    order = 20)
public static void loadFromFile(String modsFileName, String projectID) throws JDOMException, IOException,
    MCRActiveLinkException, SAXException, MCRPersistenceException, MCRAccessException {
    File modsFile = new File(modsFileName);
    if (!modsFile.isFile()) {
        throw new MCRException(MessageFormat.format("File {0} is not a file.", modsFile.getAbsolutePath()));
    }
    SAXBuilder s = new SAXBuilder(XMLReaders.NONVALIDATING, null, null);
    Document modsDoc = s.build(modsFile);
    //force validation against MODS XSD
    MCRXMLHelper.validate(modsDoc, MODS_V3_XSD_URI);
    Element modsRoot = modsDoc.getRootElement();
    if (!modsRoot.getNamespace().equals(MCRConstants.MODS_NAMESPACE)) {
        throw new MCRException(
            MessageFormat.format("File {0} is not a MODS document.", modsFile.getAbsolutePath()));
    }
    if (modsRoot.getName().equals("modsCollection")) {
        List<Element> modsElements = modsRoot.getChildren("mods", MCRConstants.MODS_NAMESPACE);
        for (Element mods : modsElements) {
            saveAsMyCoReObject(projectID, mods);
        }
    } else {
        saveAsMyCoReObject(projectID, modsRoot);
    }
}
 
开发者ID:MyCoRe-Org,项目名称:mycore,代码行数:28,代码来源:MCRMODSCommands.java

示例9: loadFromFileWithFiles

import org.jdom2.input.sax.XMLReaders; //导入依赖的package包/类
@MCRCommand(syntax = "load mods document from file {0} with files from directory {1} for project {2}",
    help = "Load MODS document {0} as MyCoRe Object with files from direcory {1} for project {2}",
    order = 10)
public static void loadFromFileWithFiles(String modsFileName, String fileDirName, String projectID)
    throws JDOMException, IOException,
    MCRActiveLinkException, SAXException, MCRPersistenceException, MCRAccessException {
    File modsFile = new File(modsFileName);
    if (!modsFile.isFile()) {
        throw new MCRException(MessageFormat.format("File {0} is not a file.", modsFile.getAbsolutePath()));
    }

    File fileDir = new File(fileDirName);
    if (!fileDir.isDirectory()) {
        throw new MCRException(
            MessageFormat.format("Directory {0} is not a directory.", fileDir.getAbsolutePath()));
    }

    SAXBuilder s = new SAXBuilder(XMLReaders.NONVALIDATING, null, null);
    Document modsDoc = s.build(modsFile);
    //force validation against MODS XSD
    MCRXMLHelper.validate(modsDoc, MODS_V3_XSD_URI);
    Element modsRoot = modsDoc.getRootElement();
    if (!modsRoot.getNamespace().equals(MCRConstants.MODS_NAMESPACE)) {
        throw new MCRException(
            MessageFormat.format("File {0} is not a MODS document.", modsFile.getAbsolutePath()));
    }
    if (modsRoot.getName().equals("modsCollection")) {
        throw new MCRException(
            MessageFormat.format("File {0} contains a mods collection witch not supported by this command.",
                modsFile.getAbsolutePath()));
    } else {
        createDerivate(saveAsMyCoReObject(projectID, modsRoot), fileDir);
    }
}
 
开发者ID:MyCoRe-Org,项目名称:mycore,代码行数:35,代码来源:MCRMODSCommands.java

示例10: createSaxBuilder

import org.jdom2.input.sax.XMLReaders; //导入依赖的package包/类
public static SAXBuilder createSaxBuilder() {
    SAXBuilder sb = new SAXBuilder();
    // don't validate and don't load dtd
    sb.setXMLReaderFactory(XMLReaders.NONVALIDATING);
    sb.setFeature("http://xml.org/sax/features/validation", false);
    sb.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
    sb.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    // JNLP needs DOCTYPE
    //sb.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
    sb.setFeature("http://xml.org/sax/features/external-general-entities", false);
    sb.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
    return sb;
}
 
开发者ID:alancnet,项目名称:artifactory,代码行数:14,代码来源:XmlUtils.java

示例11: getTransformedXml

import org.jdom2.input.sax.XMLReaders; //导入依赖的package包/类
public static String getTransformedXml(String inputXml, String stylesheetXml, String xmlFormat, boolean omitXmlDeclaration) {
    StringWriter writer = new StringWriter();
    SAXBuilder builder = new SAXBuilder();
    builder.setXMLReaderFactory(XMLReaders.NONVALIDATING);
    builder.setFeature("http://xml.org/sax/features/validation", false);
    try {
        Document inputDoc = builder.build(new StringReader(inputXml));
        StringReader reader = new StringReader(stylesheetXml);
        XSLTransformer transformer = new XSLTransformer(reader);
        Document outputDoc = transformer.transform(inputDoc);
        XMLOutputter xmlOutput = new XMLOutputter();
        Format format = null;
        if (xmlFormat.equals(COMPACT_FORMAT)) {
            format = Format.getCompactFormat();
        } else if (xmlFormat.equals(RAW_FORMAT)) {
            format = Format.getRawFormat();
        } else {
            format = Format.getPrettyFormat();
        }
        
        format.setOmitDeclaration(omitXmlDeclaration);
        xmlOutput.setFormat(format);
        xmlOutput.output(outputDoc, writer);
        writer.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return writer.toString();
}
 
开发者ID:JumpMind,项目名称:metl,代码行数:30,代码来源:XsltProcessor.java

示例12: parseDocument

import org.jdom2.input.sax.XMLReaders; //导入依赖的package包/类
private void parseDocument() throws JDOMException, IOException {
    lastModified = getLastModified();
    LOGGER.info("Parsing: {}", docURL);
    parsedDocument = new SAXBuilder(XMLReaders.NONVALIDATING).build(docURL);
}
 
开发者ID:MyCoRe-Org,项目名称:mycore,代码行数:6,代码来源:MCRLayoutUtilities.java

示例13: handleUsingXPath

import org.jdom2.input.sax.XMLReaders; //导入依赖的package包/类
@SuppressWarnings("unchecked")
protected void handleUsingXPath(Message inputMessage, ISendMessageCallback callback, boolean unitOfWorkBoundaryReached) {
    ArrayList<String> inputRows = ((TextMessage) inputMessage).getPayload();
    ArrayList<EntityData> payload = new ArrayList<EntityData>();
    if (inputRows != null) {
        for (String xml : inputRows) {
            SAXBuilder builder = new SAXBuilder();
            builder.setXMLReaderFactory(XMLReaders.NONVALIDATING);
            builder.setFeature("http://xml.org/sax/features/validation", false);
            try {
                Document document = builder.build(new StringReader(xml));
                removeNamespaces(document);
                for (XmlFormatterEntitySetting entitySetting : entitySettings) {
                    List<XmlFormatterAttributeSetting> attributeSettings = entitySetting.getAttributeSettings();
                    List<Element> entityMatches = (List<Element>) entitySetting.getExpression().evaluate(document.getRootElement());
                    for (Element element : entityMatches) {
                        String text = toXML(element);
                        Document childDocument = builder.build(new ByteArrayInputStream(text.getBytes(Charset.forName("utf-8"))));
                        getComponentStatistics().incrementNumberEntitiesProcessed(threadNumber);
                        EntityData data = new EntityData();
                        for (XmlFormatterAttributeSetting attributeSetting : attributeSettings) {
                            boolean resultsFound = false;
                            Element targetElement = element;
                            Document targetDocument = childDocument;
                            do {
                                List<Object> attributeMatches = (List<Object>) attributeSetting.getExpression().evaluate(targetDocument);
                                for (Object object : attributeMatches) {
                                    resultsFound = true;
                                    if (object instanceof Attribute) {
                                        data.put(attributeSetting.getSetting().getAttributeId(), ((Attribute) object).getValue());
                                    } else if (object instanceof Content) {
                                        data.put(attributeSetting.getSetting().getAttributeId(), ((Content) object).getValue());
                                    } else if (object instanceof Element) {
                                        data.put(attributeSetting.getSetting().getAttributeId(), ((Element) object).getTextTrim());
                                    } else {
                                        data.put(attributeSetting.getSetting().getAttributeId(), object);
                                    }
                                }

                                if (!resultsFound && !attributeSetting.getExpression().getExpression().startsWith("/" + element.getName())
                                        && targetElement.getParentElement() != null) {
                                    targetElement = targetElement.getParentElement();
                                    targetDocument = builder
                                            .build(new ByteArrayInputStream(toXML(targetElement).getBytes(Charset.forName("utf-8"))));
                                } else if (!resultsFound) {
                                    info("Did not find a match for: %s\n in:\n %s", attributeSetting.getExpression().getExpression(),
                                            text);
                                    targetDocument = null;
                                    targetElement = null;
                                }
                            } while (!resultsFound && targetElement != null);
                        }
                        if (data.size() > 0) {
                            payload.add(data);
                        } else {
                            log(LogLevel.WARN,
                                    "Found entity element: <%s/> with no matching attributes.  Please make sure your xpath expressions match",
                                    element.getName());
                        }
                    }
                }

                if (payload.size() > rowsPerMessage) {
                    callback.sendEntityDataMessage(null, payload);
                    payload = new ArrayList<>();
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

    if (payload.size() > 0) {
        callback.sendEntityDataMessage(null, payload);
    }

}
 
开发者ID:JumpMind,项目名称:metl,代码行数:78,代码来源:XmlParser.java

示例14: handleUsingXPath

import org.jdom2.input.sax.XMLReaders; //导入依赖的package包/类
@SuppressWarnings("unchecked")
protected void handleUsingXPath(Message inputMessage, ISendMessageCallback callback, boolean unitOfWorkBoundaryReached) {
    ArrayList<String> inputRows = ((TextMessage) inputMessage).getPayload();
    ArrayList<EntityData> payload = new ArrayList<EntityData>();
    if (inputRows != null) {
        for (String xml : inputRows) {
            SAXBuilder builder = new SAXBuilder();
            builder.setXMLReaderFactory(XMLReaders.NONVALIDATING);
            builder.setFeature("http://xml.org/sax/features/validation", false);
            try {
                Document document = builder.build(new StringReader(xml));
                removeNamespaces(document);
                for (XmlFormatterEntitySetting entitySetting : entitySettings) {
                    List<XmlFormatterAttributeSetting> attributeSettings = entitySetting.getAttributeSettings();
                    List<Element> entityMatches = (List<Element>) entitySetting.getExpression().evaluate(document.getRootElement());
                    for (Element element : entityMatches) {
                        
                        getComponentStatistics().incrementNumberEntitiesProcessed(threadNumber);
                        EntityData data = new EntityData();
                        for (XmlFormatterAttributeSetting attributeSetting : attributeSettings) {
                            Element targetElement = element;

                            List<Object> attributeMatches = (List<Object>) attributeSetting.getExpression().evaluate(targetElement);
                            
                            for (Object object : attributeMatches) {
                                if (object instanceof Attribute) {
                                    data.put(attributeSetting.getSetting().getAttributeId(), ((Attribute) object).getValue());
                                } else if (object instanceof Content) {
                                    data.put(attributeSetting.getSetting().getAttributeId(), ((Content) object).getValue());
                                } else if (object instanceof Element) {
                                    data.put(attributeSetting.getSetting().getAttributeId(), ((Element) object).getTextTrim());
                                }
                            }
                            
                            if (attributeMatches.size() == 0) {
                                info("Did not find a match for: %s\n in:\n %s", attributeSetting.getExpression().getExpression(),
                                        toXML(element));
                            }
                        }
                        if (data.size() > 0) {
                            payload.add(data);
                        } else {
                            log(LogLevel.WARN,
                                    "Found entity element: <%s/> with no matching attributes.  Please make sure your xpath expressions match",
                                    element.getName());
                        }
                    }
                }

                if (payload.size() > rowsPerMessage) {
                    callback.sendEntityDataMessage(null, payload);
                    payload = new ArrayList<>();
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

    if (payload.size() > 0) {
        callback.sendEntityDataMessage(null, payload);
    }
}
 
开发者ID:JumpMind,项目名称:metl,代码行数:64,代码来源:XPathXmlParser.java

示例15: createSAXBuilder

import org.jdom2.input.sax.XMLReaders; //导入依赖的package包/类
/**
 * Creates and sets up a org.jdom2.input.SAXBuilder for parsing.
 *
 * @return a new org.jdom2.input.SAXBuilder object
 */
protected SAXBuilder createSAXBuilder() {
    SAXBuilder saxBuilder;
    if (validate) {
        saxBuilder = new SAXBuilder(XMLReaders.DTDVALIDATING);
    } else {
        saxBuilder = new SAXBuilder(XMLReaders.NONVALIDATING);
    }
    saxBuilder.setEntityResolver(RESOLVER);

    //
    // This code is needed to fix the security problem outlined in
    // http://www.securityfocus.com/archive/1/297714
    //
    // Unfortunately there isn't an easy way to check if an XML parser
    // supports a particular feature, so
    // we need to set it and catch the exception if it fails. We also need
    // to subclass the JDom SAXBuilder
    // class in order to get access to the underlying SAX parser - otherwise
    // the features don't get set until
    // we are already building the document, by which time it's too late to
    // fix the problem.
    //
    // Crimson is one parser which is known not to support these features.
    try {
        
        final XMLReader parser = saxBuilder.createParser();
        
        setFeature(saxBuilder, parser, "http://xml.org/sax/features/external-general-entities", false);
        setFeature(saxBuilder, parser, "http://xml.org/sax/features/external-parameter-entities", false);
        setFeature(saxBuilder, parser, "http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

        if(!allowDoctypes) {
            setFeature(saxBuilder, parser, "http://apache.org/xml/features/disallow-doctype-decl", true);
        }

    } catch (final JDOMException e) {
        throw new IllegalStateException("JDOM could not create a SAX parser", e);
    }
    
    saxBuilder.setExpandEntities(false);

    return saxBuilder;

}
 
开发者ID:rometools,项目名称:rome,代码行数:50,代码来源:WireFeedInput.java


注:本文中的org.jdom2.input.sax.XMLReaders类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。