本文整理汇总了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());
}
}
示例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;
}
示例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);
}
}
}
示例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);
}
示例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);
}
}
示例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);
}
}
示例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;
}
示例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);
}
}
示例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);
}
}
示例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;
}
示例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();
}
示例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);
}
示例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);
}
}
示例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);
}
}
示例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;
}