jcabi / jcabi-xml Goto Github PK
View Code? Open in Web Editor NEWJava XML Parsing, Transforming, Printing, and Validating
Home Page: https://xml.jcabi.com
License: Other
Java XML Parsing, Transforming, Printing, and Validating
Home Page: https://xml.jcabi.com
License: Other
At the moment, if a network failure is experienced when StrictXML attempts to find the XSDs, it'll fail validation. Instead, it should retry a few times since network problems are likely to be intermittent.
See yegor256/qulice#280.
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);
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);
Let's enable XSL params:
new XSLDocument().withParameter("alpha", "foo")
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.
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.
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)
There is no estimate in the project yet. We have to create one as explained here
The Javadoc for XPathContext
helpfully tells us the following: "The class is immutable and thread-safe." This is a blatant lie. :)
@Immutable
. And even if it were, it wouldn't work because...map
and contexts
are instantiated with mutable types.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. :)
Let's use com.jcabi:jcabi:1.4
as parent pom
for the project
It is impossible to add new classes to the project codebase due to the following qulice check:
[ERROR] /src/main/java/com/jcabi/xml/ClasspathInput.java[2]: Line does not match expected header line of ' * Copyright (c) 2012-2014, jcabi.com'. (HeaderCheck)
XML.node()
should return Document
Reusing objects is very ineffective because a lot of parsing is done on each request. Most important points for optimization:
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
mvn qulice:check -Pqulice
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.
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] ------------------------------------------------------------------------
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.
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);
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)
This doesn't work, but it should:
XML xml = new XMLDocument("<x><a/><a/><a/></x>");
assert xml.xpath("count(/x/a)").get(0).equals("3");
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.
@dmarkov Looks like we need to add .gitattributes to force EOL of the repo.
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)
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.
Let's create a chain of XSLs
These global synchronizations around resource-intensive code should be avoided:
https://github.com/jcabi/jcabi-xml/blob/master/src/main/java/com/jcabi/xml/XSLDocument.java#L275
https://github.com/jcabi/jcabi-xml/blob/master/src/main/java/com/jcabi/xml/XSDDocument.java#L168
https://github.com/jcabi/jcabi-xml/blob/master/src/main/java/com/jcabi/xml/XSDDocument.java#L184
Let's add a method for XSL transformation of XML
Let's make XML
interface and XMLDocument
class immutable (annotated with com.jcabi.aspects.Immutable
)
XMLDocument
should print with and without XML declaration
[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>
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.
Right now StrictXML
validates only using external XSD provided in constructor, it should have also an ability to validate against schema provided in xsi:schemaLocation
inside the XML.
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.
Copyright sections still feature 2014 as a year.
In XMLDocument
and XSLDocument
we have the following field:
/**
* Encoding.
*/
private static final String ENCODING = "UTF-8";
They're not used internally, being superseded by migration to TextResource
(see #16). We should be able to remove them.
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.
Let's get rid of AspectJ in this library, it's redundant
jcabi-xml build fails on travis on master https://travis-ci.org/jcabi/jcabi-xml/jobs/52097246 because System.lineSeparator()
was added in Java 7
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
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")
);
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.
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.
Let's add class StrictXML
, which will implement XML
, but do a schema validation on reading
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
Both XMLDocument
and StrictXML
should have a method to return XML in more compact way (without unnecessary white spaces)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.