Git Product home page Git Product logo

jcabi-xml's Introduction

logo

EO principles respected here Managed by Zerocracy DevOps By Rultor.com

mvn PDD status codecov Javadoc jpeek report Maven Central

More details are here: xml.jcabi.com. Also, read this blog post: Java XML Parsing Made Easy.

It's a simple wrapper around DOM that makes XML parsing and printing easy and simple, for example:

XML xml = new XMLDocument(
  "<cart><item id="4">Coffee to go</item></cart>"
);
XML item = xml.nodes("//item[@id=4]").get(0);
String name = item.xpath("text()").get(0);
System.out.println(xml.toString());

How to contribute?

Fork the repository, make changes, submit a pull request. We promise to review your changes same day and apply to the master branch, if they look correct.

Please run Maven build before submitting a pull request:

$ mvn clean install -Pqulice

jcabi-xml's People

Contributors

adamsiemion avatar amihaiemil avatar carlosmiranda avatar dependabot[bot] avatar dmzaytsev avatar gvlasov avatar ilyakharlamov avatar l3r8yj avatar maurezen avatar piddubnyi avatar pinaf avatar prondzyn avatar renovate[bot] avatar rultor avatar simonjenga avatar skapral avatar valery1707 avatar volodya-lombrozo avatar yegor256 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jcabi-xml's Issues

StrictXML should be able to look up XSDs from classpath

Related to yegor256/qulice#273. StrictXML should be able to find XSDs in the classpath in addition to URL lookups. In the example below, it should be able to validate the XML against a classpath resource spring-beans-2.0.xsd.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans spring-beans-2.0.xsd">
</beans>

See here for more details: https://stackoverflow.com/questions/2342808/problem-validating-an-xml-file-using-java-with-an-xsd-having-an-include. Note that we have to be able to support classpath and external URL lookups with our LSResourceResolver implementation.

Indie tech review no.1

Review our existing source code and all other technical artifacts and report at least 8 bugs. More about such reviews you can read here. The task will be closed once we have that bugs reported and classified. Don't forget to add itr tag to each of them.

non-apache DOM implementation causes runtime exception

Caused by: java.lang.IllegalStateException: javax.xml.parsers.ParserConfigurationException: Unsupported feature or value: http://apache.org/xml/features/nonvalidating/load-external-dtd
    at com.jcabi.xml.XMLDocument.<clinit>(XMLDocument.java:120)
    ... 35 more
Caused by: javax.xml.parsers.ParserConfigurationException: Unsupported feature or value: http://apache.org/xml/features/nonvalidating/load-external-dtd
    at net.sf.saxon.dom.DocumentBuilderFactoryImpl.setFeature(DocumentBuilderFactoryImpl.java:154)
    at com.jcabi.xml.XMLDocument.<clinit>(XMLDocument.java:114)

xpath() doesn't work with processing instructions

This code works fine in version 0.7.8:

String xml = "<?test foo?><root/>";
assert new XMLDocument(xml).xpath(
  "/processing-instruction('test')[contains(.,'foo')]"
).size() == 1;

However it doesn't work in version 0.8

XSL params

Let's enable XSL params:

new XSLDocument().withParameter("alpha", "foo")

XSL class

Let's introduce XSL class and remove XML#xslt() method:

XSL xsl = new XSL(new FileInputStream(new File("my.xsl")));
XML first = new XMLDocument("<hey/>");
XML second = xsl.transform(first);

pom.xml:98-100: Get rid of commons-lang3 dependency,...

Puzzle 2-4b48606c in pom.xml:98-100 has to be resolved: Get rid of commons-lang3 dependency, or move it to "test" scope. It's not efficient for such a small and lightweight library to have a big dependency of this kind.

NPE in XMLDocument#toString()

In multi-threaded execution NPE surfaces sometimes in XMLDocument#toString():

java.lang.NullPointerException
    at net.sf.saxon.dom.DOMSender.walkNode(DOMSender.java:138)
    at net.sf.saxon.dom.DOMSender.outputElement(DOMSender.java:228)
    at net.sf.saxon.dom.DOMSender.sendElement(DOMSender.java:122)
    at net.sf.saxon.dom.DOMSender.send(DOMSender.java:93)
    at net.sf.saxon.dom.DOMObjectModel.sendSource(DOMObjectModel.java:260)
    at net.sf.saxon.event.Sender.send(Sender.java:243)
    at net.sf.saxon.IdentityTransformer.transform(IdentityTransformer.java:30)
    at com.jcabi.xml.XMLDocument.toString(XMLDocument.java:287)

Test XSLDocumentTest.stripsXml doesn't complete in AppVeyor

Possibly caused by the \n character in a Windows environment.

java.lang.AssertionError: 
Expected: a string containing "<a>\n<b/>\n</a>"
     but: was "<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<a>
<b/>
</a>
"
    at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
    at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8)
    at com.jcabi.xml.XSLDocumentTest.stripsXml(XSLDocumentTest.java:142)

See log.

RelaxNG class

Let's add a class that validates an XML document against its compliance to the provided RelaxNG schema:

XML xml = new XMLDocument("<hey/>");
boolean valid = new RelaxNG(new FileInputStream(new File("my.xsd"))).isValid(xml);

XSD class

Let's add a class that validates an XML document against its compliance to the provided XSD schema, e.g:

XML xml = new XMLDocument("<hey/>");
boolean valid = new XSD(new FileInputStream(new File("my.xsd"))).isValid(xml);

StrictXML

Let's add class StrictXML, which will implement XML, but do a schema validation on reading

XSLDocumentTest.stripsXml fails under Windows

Failed tests: XSLDocumentTest.stripsXml:142 Expected: a string containing "<a>\n<b/>\n</a>" but: was "<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<a>
<b/>
</a>
"

Test implementation does not account for the system-dependant line separator. Please use line.separator system property, String#format or other system-agnostic implementation.

pom.xml:84-90: Get rid of reb4j dependency...

Puzzle 2-5feea170 in pom.xml:84-90 has to be resolved: Get rid of reb4j dependency and build that regular expression on compile phase, somehow. Maybe we can use some sort of Groovy plugin, which will create the expression and store it in a plain text file, which will be used later in runtime. It's important to leave expression building mechanims maintainable. If we just hard-code the expression, we'll loose an ability to maintain/change it later.

Build fails because of quilice issues

On my Mac and Windows 7 I unable to build the sources because of quilice issues:

Unable to get XClass for java/lang/StringBuilder 
java.lang.ArrayIndexOutOfBoundsException: 5747
<stacktrace> 

output generated using mvn clean install -Pqulice -l maven.debug.log -X

Illegal character in path at index 19: file://C:/Documents and Settings/JustUser/.m2/repository/com/ jcabi/jcabi-aspects/0.20.1/jcabi-aspects-0.20.1.jar on Windows / Java7

[INFO] -------------------------------------------------
[INFO] Build Summary:
[INFO]   Passed: 2, Failed: 0, Errors: 0, Skipped: 0
[INFO] -------------------------------------------------
[INFO]
[INFO] --- qulice-maven-plugin:0.10:check (jcabi-check) @ jcabi-xml ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 03:50 min
[INFO] Finished at: 2015-01-21T18:22:29+03:00
[INFO] Final Memory: 29M/247M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal com.qulice:qulice-maven-plugin:0.10:check (jcabi-check) on project jcabi-xml: Execution jcabi-check of goal c
om.qulice:qulice-maven-plugin:0.10:check failed: Illegal character in path at index 19: file://C:/Documents and Settings/JustUser/.m2/reposi
tory/com/jcabi/jcabi-aspects/0.20.1/jcabi-aspects-0.20.1.jar -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal com.qulice:qulice-maven-plugin:0.10:check (jcabi-check) on pr
oject jcabi-xml: Execution jcabi-check of goal com.qulice:qulice-maven-plugin:0.10:check failed: Illegal character in path at index 19: file
://C:/Documents and Settings/JustUser/.m2/repository/com/jcabi/jcabi-aspects/0.20.1/jcabi-aspects-0.20.1.jar
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:224)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
        at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:120)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:355)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:155)
        at org.apache.maven.cli.MavenCli.execute(MavenCli.java:584)
        at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:216)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:160)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
        at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
        at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.apache.maven.plugin.PluginExecutionException: Execution jcabi-check of goal com.qulice:qulice-maven-plugin:0.10:check failed:
 Illegal character in path at index 19: file://C:/Documents and Settings/JustUser/.m2/repository/com/jcabi/jcabi-aspects/0.20.1/jcabi-aspect
s-0.20.1.jar
        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:143)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
        ... 19 more
Caused by: java.lang.IllegalArgumentException: Illegal character in path at index 19: file://C:/Documents and Settings/JustUser/.m2/reposito
ry/com/jcabi/jcabi-aspects/0.20.1/jcabi-aspects-0.20.1.jar
        at java.net.URI.create(URI.java:859)
        at com.qulice.maven.DefaultMavenEnvironment.classloader(DefaultMavenEnvironment.java:152)
        at com.qulice.checkstyle.CheckstyleValidator.validate(CheckstyleValidator.java:81)
        at com.qulice.maven.CheckMojo.run(CheckMojo.java:83)
        at com.qulice.maven.CheckMojo.doExecute(CheckMojo.java:59)
        at com.qulice.maven.AbstractQuliceMojo.execute(AbstractQuliceMojo.java:175)
        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:132)
        ... 20 more
Caused by: java.net.URISyntaxException: Illegal character in path at index 19: file://C:/Documents and Settings/JustUser/.m2/repository/com/
jcabi/jcabi-aspects/0.20.1/jcabi-aspects-0.20.1.jar
        at java.net.URI$Parser.fail(URI.java:2829)
        at java.net.URI$Parser.checkChars(URI.java:3002)
        at java.net.URI$Parser.parseHierarchical(URI.java:3086)
        at java.net.URI$Parser.parse(URI.java:3034)
        at java.net.URI.(URI.java:595)
        at java.net.URI.create(URI.java:857)
        ... 26 more
[ERROR]
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException
Z:\jcabi-xml>

XPath "//" doesn't work on cloned nodes

This test doesn't work:

final XML xml = new XMLDocument("<t6><z9 a='433'/></t6>");
final XML root = xml.nodes("/t6").get(0);
MatcherAssert.assertThat(
  root.xpath("//z9/@a").get(0),
  Matchers.equalTo("433")
);

`mvn clean install -Pqulice` fails under Windows

When I try to build a freshly checkout out jcabi-xml, I get following error (mvn clean install -Pqulice):

Failed tests:
  XSLDocumentTest.stripsXml:142
Expected: a string containing "<a>\n<b/>\n</a>"
     but: was "<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<a>
<b/>
</a>
"

Tests run: 47, Failures: 1, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 04:47 min
[INFO] Finished at: 2015-01-25T21:05:56+04:00
[INFO] Final Memory: 26M/71M
[INFO] ------------------------------------------------------------------------

XML.xslt()

Let's add a method for XSL transformation of XML

StrictXMLTest.java:171-176: This test does nothing useful...

Puzzle 32-8743edc8 in src/test/java/com/jcabi/xml/StrictXMLTest.java:171-176 has to be resolved: This test does nothing useful for the moment, to be a real test it needs to mock com.jcabi.xml.StrictXML.newValidator() and new mocked validator should throw few SocketException's while executing javax.xml.validation.Validator.validate(Source) in order to simulate the situation with network problems. For more details, see com.jcabi.xml.StrictXML.validate(XML) as well.

If you have any technical questions, don't ask me, submit new tickets instead

Repetitive parsing results should be cached

Reusing objects is very ineffective because a lot of parsing is done on each request. Most important points for optimization:

  • XMLDocument.node()
  • Schema parsing in XSDDocument.validate() - result is thread-safe
  • Transformer creation in XSLDocument.transformInto() - result is not thread-safe and object synchronization is required

Qulice configuration - no license specified.

  1. Checkout master branch
  2. run mvn qulice:check -Pqulice
  3. There are multiple Line does not match expected header line of ' * BSD License'. (HeaderCheck) messages.

This is a known bug of qulice configuration in parent-0.31 (jcabi/jcabi-parent#19), accessed via jcabi-0.16. Suggested action is waiting until jcabi/jcabi#297 fix is released and upgrading jcabi version.

XML#text()

Let's add a new method XML#text(String) as a shortcut for XML#xpath(String).get(0), and XML#text(String, String) (with a default value)

pom.xml:107-109: Get rid of commons-io dependency,...

Puzzle 2-b54ace9f in pom.xml:107-109 has to be resolved: Get rid of commons-io dependency, or move it to "test" scope. It's not efficient for such a small and lightweight library to have a big dependency of this kind.

XPathContext should be immutable

The Javadoc for XPathContext helpfully tells us the following: "The class is immutable and thread-safe." This is a blatant lie. :)

  1. It's not annotated with @Immutable. And even if it were, it wouldn't work because...
  2. The fields map and contexts are instantiated with mutable types.
  3. The merge method creates a new instance and manipulates its map field before returning it.

We need to fix that, or change the Javadoc to suit. If we do need to fix it, you may assign it to me - I have a fix ready. :)

Can't run IndexesRequestsTest.retrievesIndexFromSibling in IntelliJ on Windows

When I try to run the test com.rultor.agents.github.IndexesRequestsTest.retrievesIndexFromSibling in IntelliJ Idea 13 on Windows 7, the test fails with the following exception:

java.lang.ExceptionInInitializerError
    at com.rultor.spi.Talks$InDir.create(Talks.java:187)
    at com.rultor.agents.github.IndexesRequestsTest.retrievesIndexFromSibling(IndexesRequestsTest.java:106)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:202)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:65)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.lang.RuntimeException: XPathFactory#newInstance() failed to create an XPathFactory for the default object model: http://java.sun.com/jaxp/xpath/dom with the XPathFactoryConfigurationException: javax.xml.xpath.XPathFactoryConfigurationException: java.util.ServiceConfigurationError: javax.xml.xpath.XPathFactory: jar:file:/C:/Users/DP118M/.m2/repository/net/sourceforge/saxon/saxon/9.1.0.8/saxon-9.1.0.8-xpath.jar!/META-INF/services/javax.xml.xpath.XPathFactory:2: Illegal configuration-file syntax
    at javax.xml.xpath.XPathFactory.newInstance(XPathFactory.java:102)
    at com.jcabi.xml.XMLDocument.<clinit>(XMLDocument.java:84)
    ... 28 more

Please make sure that all tests in com.rultor.agents.github.IndexesRequestsTest can be run both via Maven and from the IDE.

Error in XPathFactory#newInstance

While running some of the tests of rultor (IndexesRequestsTest, for example), ran into the incorrect config of Saxon 9.1.0.8, used in jcabi-xml code.

java.lang.RuntimeException: XPathFactory#newInstance() failed to create an XPathFactory for the default object model: http://java.sun.com/jaxp/xpath/dom with the XPathFactoryConfigurationException: javax.xml.xpath.XPathFactoryConfigurationException: java.util.ServiceConfigurationError: javax.xml.xpath.XPathFactory: jar:file:%path%/.m2/repository/net/sourceforge/saxon/saxon/9.1.0.8/saxon-9.1.0.8-xpath.jar!/META-INF/services/javax.xml.xpath.XPathFactory:2: Illegal configuration-file syntax
at javax.xml.xpath.XPathFactory.newInstance(XPathFactory.java:102)
at com.jcabi.xml.XMLDocument.(XMLDocument.java:84)

This is a known bug.

Please upgrade dependency version to the one in which the issue is fixed and release a new version of jcabi-xml.

0.16

Please, publish a new release 0.16 (or higher). Don't forget to update us here regularly (at least once a week), as explained in this article.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.