Discussion:
[jdom-interest] malformed URL exception exception in saxbuilder.build due to unreachable URL
cliff palmer
2012-02-09 20:54:17 UTC
Permalink
I'm reading through several hundred thousand existing XML documents
building counts of XML tags and have encountered a
Java.net.MalformedURL Exception raised by saxBuilder.build because the
xmlns points to a URL that can not be reached.
I am using JDOM 1.1.2.
Is there a call or parameter setting that will cause saxBuilder to
ignore namespaces when parsing?
Thanks!
Cliff
Paul Libbrecht
2012-02-09 21:18:41 UTC
Permalink
Cliff,

this doesn't sound reasonable to me. I have never seen code doing such and would not expect it (I also run jdom offline quite often).

Could it be the DTD is hitting you?
Can you copy a full stacktrace?

thanks in advance

Paul
Post by cliff palmer
I'm reading through several hundred thousand existing XML documents
building counts of XML tags and have encountered a
Java.net.MalformedURL Exception raised by saxBuilder.build because the
xmlns points to a URL that can not be reached.
I am using JDOM 1.1.2.
Is there a call or parameter setting that will cause saxBuilder to
ignore namespaces when parsing?
Thanks!
Cliff
_______________________________________________
cliff palmer
2012-02-09 21:31:16 UTC
Permalink
I don't believe this is a DTD problem - no DTD is referenced.
This is the stack trace:
at java.net.URL.<init>(URL.java:567)
at java.net.URL.<init>(URL.java:464)
at java.net.URL.<init>(URL.java:413)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:650)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:186)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:772)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:518)
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:986)
at com.dt.XMLUtility.ProfileXMLAttributes.process(ProfileXMLAttributes.java:162)
at com.dt.XMLUtility.main.main(main.java:22)
java.io.FileNotFoundException:
/home/cpalmer/workspace/ProfileXMLAttributes/< (No such file or
directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at java.io.FileInputStream.<init>(FileInputStream.java:79)
at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:70)
at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:161)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:653)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:186)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:772)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:518)
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:986)
at com.dt.XMLUtility.ProfileXMLAttributes.process(ProfileXMLAttributes.java:162)
at com.dt.XMLUtility.main.main(main.java:22)
java.net.MalformedURLException: no protocol: <?xml version="1.0"?>

Thanks
Cliff
Post by Paul Libbrecht
Cliff,
this doesn't sound reasonable to me. I have never seen code doing such and
would not expect it (I also run jdom offline quite often).
Could it be the DTD is hitting you?
Can you copy a full stacktrace?
thanks in advance
Paul
Post by cliff palmer
I'm reading through several hundred thousand existing XML documents
building counts of XML tags and have encountered a
Java.net.MalformedURL Exception raised by saxBuilder.build because the
xmlns points to a URL that can not be reached.
I am using JDOM 1.1.2.
Is there a call or parameter setting that will cause saxBuilder to
ignore namespaces when parsing?
Thanks!
Cliff
_______________________________________________
Oliver Ruebenacker
2012-02-09 22:08:24 UTC
Permalink
Hello,

I don't know anything about SAX, but the error says the problem is
not an unreachable URL, but a malformed one. Somehow, it seems to try
to use "<?xml version="1.0"?>" as an URL. Apparently, the content of
an XML file (or the first line of it) is used where a URL should be
used.

Take care
Oliver
Post by cliff palmer
I don't believe this is a DTD problem - no DTD is referenced.
at java.net.URL.<init>(URL.java:567)
       at java.net.URL.<init>(URL.java:464)
       at java.net.URL.<init>(URL.java:413)
       at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:650)
       at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:186)
       at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:772)
       at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
       at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
       at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
       at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
       at org.jdom.input.SAXBuilder.build(SAXBuilder.java:518)
       at org.jdom.input.SAXBuilder.build(SAXBuilder.java:986)
       at com.dt.XMLUtility.ProfileXMLAttributes.process(ProfileXMLAttributes.java:162)
       at com.dt.XMLUtility.main.main(main.java:22)
/home/cpalmer/workspace/ProfileXMLAttributes/< (No such file or
directory)
       at java.io.FileInputStream.open(Native Method)
       at java.io.FileInputStream.<init>(FileInputStream.java:120)
       at java.io.FileInputStream.<init>(FileInputStream.java:79)
       at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:70)
       at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:161)
       at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:653)
       at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:186)
       at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:772)
       at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
       at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
       at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
       at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
       at org.jdom.input.SAXBuilder.build(SAXBuilder.java:518)
       at org.jdom.input.SAXBuilder.build(SAXBuilder.java:986)
       at com.dt.XMLUtility.ProfileXMLAttributes.process(ProfileXMLAttributes.java:162)
       at com.dt.XMLUtility.main.main(main.java:22)
java.net.MalformedURLException: no protocol: <?xml version="1.0"?>
Thanks
Cliff
Post by Paul Libbrecht
Cliff,
this doesn't sound reasonable to me. I have never seen code doing such and
would not expect it (I also run jdom offline quite often).
Could it be the DTD is hitting you?
Can you copy a full stacktrace?
thanks in advance
Paul
Post by cliff palmer
I'm reading through several hundred thousand existing XML documents
building counts of XML tags and have encountered a
Java.net.MalformedURL Exception raised by saxBuilder.build because the
xmlns points to a URL that can not be reached.
I am using JDOM 1.1.2.
Is there a call or parameter setting that will cause saxBuilder to
ignore namespaces when parsing?
Thanks!
Cliff
_______________________________________________
_______________________________________________
--
Oliver Ruebenacker, Computational Cell Biologist
Virtual Cell (http://vcell.org)
SBPAX: Turning Bio Knowledge into Math Models (http://www.sbpax.org)
http://www.oliver.curiousworld.org
cliff palmer
2012-02-09 22:19:16 UTC
Permalink
Thanks Oliver - I don't need access to the unreachable/malformed URL,
I just need to instantiate the XML that is there so I can step through
it and determine what tags are there.
Is there a way to tell JDOM to ignore external references and not be
concerned about URLs ?
Thanks
Cliff
    Hello,
 I don't know anything about SAX, but the error says the problem is
not an unreachable URL, but a malformed one. Somehow, it seems to try
to use "<?xml version="1.0"?>" as an URL. Apparently, the content of
an XML file (or the first line of it) is used where a URL should be
used.
    Take care
    Oliver
Post by cliff palmer
I don't believe this is a DTD problem - no DTD is referenced.
at java.net.URL.<init>(URL.java:567)
       at java.net.URL.<init>(URL.java:464)
       at java.net.URL.<init>(URL.java:413)
       at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:650)
       at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:186)
       at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:772)
       at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
       at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
       at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
       at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
       at org.jdom.input.SAXBuilder.build(SAXBuilder.java:518)
       at org.jdom.input.SAXBuilder.build(SAXBuilder.java:986)
       at com.dt.XMLUtility.ProfileXMLAttributes.process(ProfileXMLAttributes.java:162)
       at com.dt.XMLUtility.main.main(main.java:22)
/home/cpalmer/workspace/ProfileXMLAttributes/< (No such file or
directory)
       at java.io.FileInputStream.open(Native Method)
       at java.io.FileInputStream.<init>(FileInputStream.java:120)
       at java.io.FileInputStream.<init>(FileInputStream.java:79)
       at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:70)
       at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:161)
       at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:653)
       at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:186)
       at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:772)
       at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
       at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
       at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
       at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
       at org.jdom.input.SAXBuilder.build(SAXBuilder.java:518)
       at org.jdom.input.SAXBuilder.build(SAXBuilder.java:986)
       at com.dt.XMLUtility.ProfileXMLAttributes.process(ProfileXMLAttributes.java:162)
       at com.dt.XMLUtility.main.main(main.java:22)
java.net.MalformedURLException: no protocol: <?xml version="1.0"?>
Thanks
Cliff
Post by Paul Libbrecht
Cliff,
this doesn't sound reasonable to me. I have never seen code doing such and
would not expect it (I also run jdom offline quite often).
Could it be the DTD is hitting you?
Can you copy a full stacktrace?
thanks in advance
Paul
Post by cliff palmer
I'm reading through several hundred thousand existing XML documents
building counts of XML tags and have encountered a
Java.net.MalformedURL Exception raised by saxBuilder.build because the
xmlns points to a URL that can not be reached.
I am using JDOM 1.1.2.
Is there a call or parameter setting that will cause saxBuilder to
ignore namespaces when parsing?
Thanks!
Cliff
_______________________________________________
_______________________________________________
--
Oliver Ruebenacker, Computational Cell Biologist
Virtual Cell (http://vcell.org)
SBPAX: Turning Bio Knowledge into Math Models (http://www.sbpax.org)
http://www.oliver.curiousworld.org
Oliver Ruebenacker
2012-02-09 22:32:52 UTC
Permalink
Hello,

I would guess the fact that a piece of XML content appears where a
URL is expected points to some kind of coding error. Can you post a
few lines of your code?

Take care
Oliver
Post by cliff palmer
Thanks Oliver - I don't need access to the unreachable/malformed URL,
I just need to instantiate the XML that is there so I can step through
it and determine what tags are there.
Is there a way to tell JDOM to ignore external references and not be
concerned about URLs ?
Thanks
Cliff
    Hello,
 I don't know anything about SAX, but the error says the problem is
not an unreachable URL, but a malformed one. Somehow, it seems to try
to use "<?xml version="1.0"?>" as an URL. Apparently, the content of
an XML file (or the first line of it) is used where a URL should be
used.
    Take care
    Oliver
Post by cliff palmer
I don't believe this is a DTD problem - no DTD is referenced.
at java.net.URL.<init>(URL.java:567)
       at java.net.URL.<init>(URL.java:464)
       at java.net.URL.<init>(URL.java:413)
       at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:650)
       at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:186)
       at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:772)
       at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
       at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
       at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
       at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
       at org.jdom.input.SAXBuilder.build(SAXBuilder.java:518)
       at org.jdom.input.SAXBuilder.build(SAXBuilder.java:986)
       at com.dt.XMLUtility.ProfileXMLAttributes.process(ProfileXMLAttributes.java:162)
       at com.dt.XMLUtility.main.main(main.java:22)
/home/cpalmer/workspace/ProfileXMLAttributes/< (No such file or
directory)
       at java.io.FileInputStream.open(Native Method)
       at java.io.FileInputStream.<init>(FileInputStream.java:120)
       at java.io.FileInputStream.<init>(FileInputStream.java:79)
       at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:70)
       at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:161)
       at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:653)
       at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:186)
       at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:772)
       at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
       at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
       at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
       at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
       at org.jdom.input.SAXBuilder.build(SAXBuilder.java:518)
       at org.jdom.input.SAXBuilder.build(SAXBuilder.java:986)
       at com.dt.XMLUtility.ProfileXMLAttributes.process(ProfileXMLAttributes.java:162)
       at com.dt.XMLUtility.main.main(main.java:22)
java.net.MalformedURLException: no protocol: <?xml version="1.0"?>
Thanks
Cliff
Post by Paul Libbrecht
Cliff,
this doesn't sound reasonable to me. I have never seen code doing such and
would not expect it (I also run jdom offline quite often).
Could it be the DTD is hitting you?
Can you copy a full stacktrace?
thanks in advance
Paul
Post by cliff palmer
I'm reading through several hundred thousand existing XML documents
building counts of XML tags and have encountered a
Java.net.MalformedURL Exception raised by saxBuilder.build because the
xmlns points to a URL that can not be reached.
I am using JDOM 1.1.2.
Is there a call or parameter setting that will cause saxBuilder to
ignore namespaces when parsing?
Thanks!
Cliff
_______________________________________________
_______________________________________________
--
Oliver Ruebenacker, Computational Cell Biologist
Virtual Cell (http://vcell.org)
SBPAX: Turning Bio Knowledge into Math Models (http://www.sbpax.org)
http://www.oliver.curiousworld.org
--
Oliver Ruebenacker, Computational Cell Biologist
Virtual Cell (http://vcell.org)
SBPAX: Turning Bio Knowledge into Math Models (http://www.sbpax.org)
http://www.oliver.curiousworld.org
Paul Libbrecht
2012-02-09 22:25:22 UTC
Permalink
Click,

are you parsing a document that' s in a string?
Mustn't be...

one thing tickles me: the trailing < in
java.io.FileNotFoundException: /home/cpalmer/workspace/ProfileXMLAttributes/< (No such file or

Crawling this stacktrace with my intelliJ idea makes me conclude that the file above is passed to SAXBuilder in ProfileXMLAttributes.java:162.

hope it helps.

paul
Post by Oliver Ruebenacker
Hello,
I don't know anything about SAX, but the error says the problem is
not an unreachable URL, but a malformed one. Somehow, it seems to try
to use "<?xml version="1.0"?>" as an URL. Apparently, the content of
an XML file (or the first line of it) is used where a URL should be
used.
Take care
Oliver
Post by cliff palmer
I don't believe this is a DTD problem - no DTD is referenced.
at java.net.URL.<init>(URL.java:567)
at java.net.URL.<init>(URL.java:464)
at java.net.URL.<init>(URL.java:413)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:650)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:186)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:772)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:518)
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:986)
at com.dt.XMLUtility.ProfileXMLAttributes.process(ProfileXMLAttributes.java:162)
at com.dt.XMLUtility.main.main(main.java:22)
/home/cpalmer/workspace/ProfileXMLAttributes/< (No such file or
directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at java.io.FileInputStream.<init>(FileInputStream.java:79)
at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:70)
at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:161)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:653)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:186)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:772)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:518)
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:986)
at com.dt.XMLUtility.ProfileXMLAttributes.process(ProfileXMLAttributes.java:162)
at com.dt.XMLUtility.main.main(main.java:22)
java.net.MalformedURLException: no protocol: <?xml version="1.0"?>
Thanks
Cliff
Post by Paul Libbrecht
Cliff,
this doesn't sound reasonable to me. I have never seen code doing such and
would not expect it (I also run jdom offline quite often).
Could it be the DTD is hitting you?
Can you copy a full stacktrace?
thanks in advance
Paul
Post by cliff palmer
I'm reading through several hundred thousand existing XML documents
building counts of XML tags and have encountered a
Java.net.MalformedURL Exception raised by saxBuilder.build because the
xmlns points to a URL that can not be reached.
I am using JDOM 1.1.2.
Is there a call or parameter setting that will cause saxBuilder to
ignore namespaces when parsing?
Thanks!
Cliff
_______________________________________________
_______________________________________________
--
Oliver Ruebenacker, Computational Cell Biologist
Virtual Cell (http://vcell.org)
SBPAX: Turning Bio Knowledge into Math Models (http://www.sbpax.org)
http://www.oliver.curiousworld.org
Rolf Lear
2012-02-09 22:40:57 UTC
Permalink
Hi Cliff.

I think there's been some good pointers already, but just to make things
crystal clear... can you perhaps post the relevant code snippet you are
using to parse the document, and perhaps the first few lines of the
actual XML too.

Also, does this problem happen with *all* xml documents (the first one),
or with just some of them?

My guess is that Oliver has the right idea with parsing the wrong
string.... remember that the SaxBuilder.build(String) method expects the
String to be a URL, not the actual XML content..... YTour stack trace
indicates you are calling this method...

See the code here:
https://github.com/hunterhacker/jdom/blob/jdom-1.x/core/src/java/org/jdom/input/SAXBuilder.java#L986

Anyway, seeing your code would help....

Rolf
Post by cliff palmer
I'm reading through several hundred thousand existing XML documents
building counts of XML tags and have encountered a
Java.net.MalformedURL Exception raised by saxBuilder.build because the
xmlns points to a URL that can not be reached.
I am using JDOM 1.1.2.
Is there a call or parameter setting that will cause saxBuilder to
ignore namespaces when parsing?
Thanks!
Cliff
_______________________________________________
cliff palmer
2012-02-09 22:58:23 UTC
Permalink
Hi Rolf
I will post the code later, (sorry late for a meeting) but to answer
your questions:
- this error occurs when there is an "xmlns" declaration. Since this
is the first instance of an "xmlns" declaration I've encountered with
JDOM and all of the URLs in the "xmlns" declaration that I have found
point to the same bad address, I don't know if the problem is related
to lookup of the URL or just the presence of an "xmlns" declaration.
- the problem is predictable and occurs for each xml document that
uses this bad URL in an "xmlns" declaration.
- I've used the code (I will post it, I promise) to parse over 3
million xml documents, passing a string containing the xml document
(not a URL). The value I pass to saxbuilder.build is the returned
string from the JDBC call ResultSet.getString using a column number
parameter. I haven't been altering or converting the string returned
from JDBC.

Thanks Rolf and I will post the code as soon as the suits are done with me.

Cliff
Post by Rolf Lear
Hi Cliff.
I think there's been some good pointers already, but just to make things
crystal clear... can you perhaps post the relevant code snippet you are
using to parse the document, and perhaps the first few lines of the actual
XML too.
Also, does this problem happen with *all* xml documents (the first one), or
with just some of them?
My guess is that Oliver has the right idea with parsing the wrong string....
remember that the SaxBuilder.build(String) method expects the String to be a
URL, not the actual XML content..... YTour stack trace indicates you are
calling this method...
https://github.com/hunterhacker/jdom/blob/jdom-1.x/core/src/java/org/jdom/input/SAXBuilder.java#L986
Anyway, seeing your code would help....
Rolf
Post by cliff palmer
I'm reading through several hundred thousand existing XML documents
building counts of XML tags and have encountered a
Java.net.MalformedURL Exception raised by saxBuilder.build because the
xmlns points to a URL that can not be reached.
I am using JDOM 1.1.2.
Is there a call or parameter setting that will cause saxBuilder to
ignore namespaces when parsing?
Thanks!
Cliff
_______________________________________________
cliff palmer
2012-02-09 23:26:22 UTC
Permalink
code below:

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Collections;
import java.util.Map;
import javax.sql.DataSource;
import oracle.sql.CLOB;
import org.apache.log4j.Appender;
import org.apache.log4j.EnhancedPatternLayout;
import org.apache.log4j.Logger;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Attribute;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class ProfileXMLAttributes implements AppDriver {
public String driverClassName;
static Logger logLogger;
Appender logAppender;
EnhancedPatternLayout logLayout;
DataSource dataSource;
JdbcTemplate jdbcTemplate;
Connection conn;
Statement stmt;
ResultSet rs;
ResultSetMetaData rsmd;
SQLClob sqlClob;
SAXBuilder saxBuilder;
Document xmlDoc;
Format xmlFmt;
XMLOutputter xmlOutputter;
Element xmlElement;
Element xPathElement;
Element rootElement;
String pathString;
static SQLClob theSQLClob;
Map<String, Integer> tagMap;
String tagKey;
Integer tagValue;
int badXML = 0;
int rowsRead = 0;
boolean goodXML;
String msgID;

/* =========================================================
init is called once by the driver program to set up the
run environment for logging, JDBC, JDOM, etc
========================================================= */

@Override
public void init() {
logLogger = Logger.getLogger(ProfileXMLAttributes.class.getName());
dataSource = (DataSource) main.context.getBean("datasource");
jdbcTemplate = new JdbcTemplate(dataSource);
try{
conn = DriverManager.getConnection("jdbc:oracle:thin:@10.1.6.78:1521:kbfcubs",
"kbfcubs", "kbfcubs");
} catch (SQLException e) {
logLogger.debug("Exception initializing connection");
e.printStackTrace();
}
stmt = null;
try{
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
stmt.setFetchSize(10000);
} catch (SQLException e) {
logLogger.debug("Exception initializing statement");
e.printStackTrace();
}
rs = null;
try {
String xmlQuery = "select xml_id, xml_contents from xml_table";
rs = stmt.executeQuery(xmlQuery);
} catch (SQLException e) {
logLogger.debug("Exception executing query");
e.printStackTrace();
}
saxBuilder = new SAXBuilder();
xmlFmt = Format.getPrettyFormat().setEncoding("UTF-8");
xmlOutputter = new XMLOutputter(xmlFmt);
structureString = new ArrayList<String>();
tagMap = new HashMap<String, Integer>();
}

/* =========================================================
process is called once by the driver program to do all
the work. For each row returned by the JDBC query it
instantiates the xml document in JDOM and then calls
doProcess passing the rootElement to step through the
xml tags.
========================================================= */

@Override
public void process() throws SQLException {
while(rs.next()) {
xmlDoc = null;
rowsRead ++;
msgID = rs.getString(1);
try {
xmlDoc = saxBuilder.build(rs.getString(2));
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
goodXML = false;
e.printStackTrace();
}
try {
rootElement = xmlDoc.getRootElement();
doProcess(rootElement);
} catch (Exception e) {
e.printStackTrace();
}
}
}

/* =========================================================
doProcess is called once by process for each row in the
JDBC result set. It is also called recursively as the
xml tags are discovered. Each xml tag is added to a
hash map and counts of the occurances of each tag are
accumulated.
========================================================= */


private void doProcess (Element currentElement) {
tagKey = new String(currentElement.getName());
tagValue = tagMap.get(tagKey);
if(tagValue == null) {
tagValue = 1;
} else {
tagValue += 1;
}
tagMap.put(tagKey, tagValue);
Iterator<Element> itr = currentElement.getChildren().iterator();
boolean hasChildren = false;
while (itr.hasNext()) {
hasChildren = true;
Object childElement = itr.next();
}
if (hasChildren == true) {
itr = currentElement.getChildren().iterator();
hasChildren = false;
}
while (itr.hasNext()) {
Object childElement = itr.next();
doProcess((Element) childElement);
}
}
}
Post by cliff palmer
Hi Rolf
I will post the code later, (sorry late for a meeting) but to answer
- this error occurs when there is an "xmlns" declaration. Since this
is the first instance of an "xmlns" declaration I've encountered with
JDOM and all of the URLs in the "xmlns" declaration that I have found
point to the same bad address, I don't know if the problem is related
to lookup of the URL or just the presence of an "xmlns" declaration.
- the problem is predictable and occurs for each xml document that
uses this bad URL in an "xmlns" declaration.
- I've used the code (I will post it, I promise) to parse over 3
million xml documents, passing a string containing the xml document
(not a URL). The value I pass to saxbuilder.build is the returned
string from the JDBC call ResultSet.getString using a column number
parameter. I haven't been altering or converting the string returned
from JDBC.
Thanks Rolf and I will post the code as soon as the suits are done with me.
Cliff
Post by Rolf Lear
Hi Cliff.
I think there's been some good pointers already, but just to make things
crystal clear... can you perhaps post the relevant code snippet you are
using to parse the document, and perhaps the first few lines of the actual
XML too.
Also, does this problem happen with *all* xml documents (the first one), or
with just some of them?
My guess is that Oliver has the right idea with parsing the wrong string....
remember that the SaxBuilder.build(String) method expects the String to be a
URL, not the actual XML content..... YTour stack trace indicates you are
calling this method...
https://github.com/hunterhacker/jdom/blob/jdom-1.x/core/src/java/org/jdom/input/SAXBuilder.java#L986
Anyway, seeing your code would help....
Rolf
Post by cliff palmer
I'm reading through several hundred thousand existing XML documents
building counts of XML tags and have encountered a
Java.net.MalformedURL Exception raised by saxBuilder.build because the
xmlns points to a URL that can not be reached.
I am using JDOM 1.1.2.
Is there a call or parameter setting that will cause saxBuilder to
ignore namespaces when parsing?
Thanks!
Cliff
_______________________________________________
Rolf Lear
2012-02-09 23:44:10 UTC
Permalink
Hi Cliff.

I think the problem is that the content of the column 'xml_contents' of
the table 'xml_table' is actual XML data.... not a file name/URI.


I think what you want to do is:

String xmlcontent = rs.getString(2);
StringReader reader = new StringReader(xmlcontent);
xmlDoc = saxBuilder.build(reader);

See http://jdom.org/docs/faq.html#a0210

Rolf
.....
Post by cliff palmer
rs = null;
try {
String xmlQuery = "select xml_id, xml_contents from xml_table";
rs = stmt.executeQuery(xmlQuery);
} catch (SQLException e) {
logLogger.debug("Exception executing query");
e.printStackTrace();
}
saxBuilder = new SAXBuilder();
xmlFmt = Format.getPrettyFormat().setEncoding("UTF-8");
xmlOutputter = new XMLOutputter(xmlFmt);
structureString = new ArrayList<String>();
tagMap = new HashMap<String, Integer>();
...
Post by cliff palmer
while(rs.next()) {
xmlDoc = null;
rowsRead ++;
msgID = rs.getString(1);
try {
xmlDoc = saxBuilder.build(rs.getString(2));
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
goodXML = false;
e.printStackTrace();
}
try {
rootElement = xmlDoc.getRootElement();
doProcess(rootElement);
} catch (Exception e) {
e.printStackTrace();
}
}
Post by cliff palmer
Hi Rolf
I will post the code later, (sorry late for a meeting) but to answer
- this error occurs when there is an "xmlns" declaration. Since this
is the first instance of an "xmlns" declaration I've encountered with
JDOM and all of the URLs in the "xmlns" declaration that I have found
point to the same bad address, I don't know if the problem is related
to lookup of the URL or just the presence of an "xmlns" declaration.
- the problem is predictable and occurs for each xml document that
uses this bad URL in an "xmlns" declaration.
- I've used the code (I will post it, I promise) to parse over 3
million xml documents, passing a string containing the xml document
(not a URL). The value I pass to saxbuilder.build is the returned
string from the JDBC call ResultSet.getString using a column number
parameter. I haven't been altering or converting the string returned
from JDBC.
Thanks Rolf and I will post the code as soon as the suits are done with me.
Cliff
Post by Rolf Lear
Hi Cliff.
I think there's been some good pointers already, but just to make things
crystal clear... can you perhaps post the relevant code snippet you are
using to parse the document, and perhaps the first few lines of the actual
XML too.
Also, does this problem happen with *all* xml documents (the first one), or
with just some of them?
My guess is that Oliver has the right idea with parsing the wrong string....
remember that the SaxBuilder.build(String) method expects the String to be a
URL, not the actual XML content..... YTour stack trace indicates you are
calling this method...
https://github.com/hunterhacker/jdom/blob/jdom-1.x/core/src/java/org/jdom/input/SAXBuilder.java#L986
Anyway, seeing your code would help....
Rolf
Post by cliff palmer
I'm reading through several hundred thousand existing XML documents
building counts of XML tags and have encountered a
Java.net.MalformedURL Exception raised by saxBuilder.build because the
xmlns points to a URL that can not be reached.
I am using JDOM 1.1.2.
Is there a call or parameter setting that will cause saxBuilder to
ignore namespaces when parsing?
Thanks!
Cliff
_______________________________________________
Rolf Lear
2012-02-09 23:59:20 UTC
Permalink
Hi Cliff.

I just ran a quick test ... :

public class TestParse {
public static void main(String[] args) throws Exception {
String xml = "<?xml version=\"1.0\" ?>\n<xmlchars />";
SAXBuilder sb = new SAXBuilder();
sb.build(xml);
}
}

and got:

Exception in thread "main" java.net.MalformedURLException: no protocol:
<?xml version="1.0" ?>
<xmlchars />
at java.net.URL.<init>(URL.java:567)
at java.net.URL.<init>(URL.java:464)
at java.net.URL.<init>(URL.java:413)
at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown
Source)
at
org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown
Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:217)
at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:327)
at org.jdom2.input.SAXBuilder.build(SAXBuilder.java:1286)
at net.tuis.debug.TestParse.main(TestParse.java:12)
Post by Rolf Lear
Hi Cliff.
I think the problem is that the content of the column 'xml_contents' of
the table 'xml_table' is actual XML data.... not a file name/URI.
String xmlcontent = rs.getString(2);
StringReader reader = new StringReader(xmlcontent);
xmlDoc = saxBuilder.build(reader);
See http://jdom.org/docs/faq.html#a0210
Rolf
.....
Post by cliff palmer
rs = null;
try {
String xmlQuery = "select xml_id, xml_contents from xml_table";
rs = stmt.executeQuery(xmlQuery);
} catch (SQLException e) {
logLogger.debug("Exception executing query");
e.printStackTrace();
}
saxBuilder = new SAXBuilder();
xmlFmt = Format.getPrettyFormat().setEncoding("UTF-8");
xmlOutputter = new XMLOutputter(xmlFmt);
structureString = new ArrayList<String>();
tagMap = new HashMap<String, Integer>();
...
Post by cliff palmer
while(rs.next()) {
xmlDoc = null;
rowsRead ++;
msgID = rs.getString(1);
try {
xmlDoc = saxBuilder.build(rs.getString(2));
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
goodXML = false;
e.printStackTrace();
}
try {
rootElement = xmlDoc.getRootElement();
doProcess(rootElement);
} catch (Exception e) {
e.printStackTrace();
}
}
Post by cliff palmer
Hi Rolf
I will post the code later, (sorry late for a meeting) but to answer
- this error occurs when there is an "xmlns" declaration. Since this
is the first instance of an "xmlns" declaration I've encountered with
JDOM and all of the URLs in the "xmlns" declaration that I have found
point to the same bad address, I don't know if the problem is related
to lookup of the URL or just the presence of an "xmlns" declaration.
- the problem is predictable and occurs for each xml document that
uses this bad URL in an "xmlns" declaration.
- I've used the code (I will post it, I promise) to parse over 3
million xml documents, passing a string containing the xml document
(not a URL). The value I pass to saxbuilder.build is the returned
string from the JDBC call ResultSet.getString using a column number
parameter. I haven't been altering or converting the string returned
from JDBC.
Thanks Rolf and I will post the code as soon as the suits are done with me.
Cliff
Post by Rolf Lear
Hi Cliff.
I think there's been some good pointers already, but just to make things
crystal clear... can you perhaps post the relevant code snippet you are
using to parse the document, and perhaps the first few lines of the actual
XML too.
Also, does this problem happen with *all* xml documents (the first one),
or
with just some of them?
My guess is that Oliver has the right idea with parsing the wrong string....
remember that the SaxBuilder.build(String) method expects the String
to be
a
URL, not the actual XML content..... YTour stack trace indicates you are
calling this method...
https://github.com/hunterhacker/jdom/blob/jdom-1.x/core/src/java/org/jdom/input/SAXBuilder.java#L986
Anyway, seeing your code would help....
Rolf
Post by cliff palmer
I'm reading through several hundred thousand existing XML documents
building counts of XML tags and have encountered a
Java.net.MalformedURL Exception raised by saxBuilder.build because the
xmlns points to a URL that can not be reached.
I am using JDOM 1.1.2.
Is there a call or parameter setting that will cause saxBuilder to
ignore namespaces when parsing?
Thanks!
Cliff
_______________________________________________
_______________________________________________
cliff palmer
2012-02-10 12:35:43 UTC
Permalink
Rolf, that worked - it's mysterious that the version in my code
(passing the string) worked until there was a URL, but I can live with
mysteries in light of working code :).
Thanks!
Cliff
Post by Rolf Lear
Hi Cliff.
I think the problem is that the content of the column 'xml_contents' of the
table 'xml_table' is actual XML data.... not a file name/URI.
   String xmlcontent = rs.getString(2);
   StringReader reader = new StringReader(xmlcontent);
   xmlDoc = saxBuilder.build(reader);
See http://jdom.org/docs/faq.html#a0210
Rolf
.....
               rs = null;
               try {
                       String xmlQuery = "select xml_id, xml_contents from
xml_table";
                       rs = stmt.executeQuery(xmlQuery);
               } catch (SQLException e) {
                       logLogger.debug("Exception executing query");
                       e.printStackTrace();
               }
               saxBuilder = new SAXBuilder();
               xmlFmt =  Format.getPrettyFormat().setEncoding("UTF-8");
               xmlOutputter = new XMLOutputter(xmlFmt);
               structureString = new ArrayList<String>();
               tagMap = new HashMap<String, Integer>();
...
               while(rs.next()) {
                       xmlDoc = null;
                       rowsRead ++;
                       msgID = rs.getString(1);
                       try {
                               xmlDoc = saxBuilder.build(rs.getString(2));
                       } catch (JDOMException e) {
                               e.printStackTrace();
                       } catch (IOException e) {
                               goodXML = false;
                               e.printStackTrace();
                       }
                       try {
                               rootElement = xmlDoc.getRootElement();
                               doProcess(rootElement);
                       } catch (Exception e) {
                               e.printStackTrace();
                       }
               }
Post by cliff palmer
Hi Rolf
I will post the code later, (sorry late for a meeting) but to answer
- this error occurs when there is an "xmlns" declaration.  Since this
is the first instance of an "xmlns" declaration I've encountered with
JDOM and all of the URLs in the "xmlns" declaration that I have found
point to the same bad address, I don't know if the problem is related
to lookup of the URL or just the presence of an "xmlns" declaration.
- the problem is predictable and occurs for each xml document that
uses this bad URL in an "xmlns" declaration.
- I've used the code (I will post it, I promise) to parse over 3
million xml documents, passing a string containing the xml document
(not a URL).  The value I pass to saxbuilder.build is the returned
string from the JDBC call ResultSet.getString using a column number
parameter.  I haven't been altering or converting the string returned
from JDBC.
Thanks Rolf and I will post the code as soon as the suits are done with me.
Cliff
Post by Rolf Lear
Hi Cliff.
I think there's been some good pointers already, but just to make things
crystal clear... can you perhaps post the relevant code snippet you are
using to parse the document, and perhaps the first few lines of the actual
XML too.
Also, does this problem happen with *all* xml documents (the first one), or
with just some of them?
My guess is that Oliver has the right idea with parsing the wrong string....
remember that the SaxBuilder.build(String) method expects the String to
be
a
URL, not the actual XML content..... YTour stack trace indicates you are
calling this method...
https://github.com/hunterhacker/jdom/blob/jdom-1.x/core/src/java/org/jdom/input/SAXBuilder.java#L986
Anyway, seeing your code would help....
Rolf
Post by cliff palmer
I'm reading through several hundred thousand existing XML documents
building counts of XML tags and have encountered a
Java.net.MalformedURL Exception raised by saxBuilder.build because the
xmlns points to a URL that can not be reached.
I am using JDOM 1.1.2.
Is there a call or parameter setting that will cause saxBuilder to
ignore namespaces when parsing?
Thanks!
Cliff
_______________________________________________
Rolf Lear
2012-02-10 13:22:30 UTC
Permalink
Hi Cliff.

That would be a mystery, but my suspicion is that it has not worked at
all... if it *did* work then there are other problems ... ;-)

I suspect that everything has been failing - maybe the first record from
the database had an xmlns, and it has been a red-herring for you.

I did run some test code through, and it causes similar errors regardless
of the actual XML content.

I wonder if it would help if we put a validation process in the
SAXBuilder.build(String) method that inspected the string, and if the first
non-white character is '<' it throws an exception.... that should catch all
instances where XML content is supplied to the method, since '<' is never
valid in a URI..... and it is always the first character in any valid XML
document.....

getting an exception: MalformedURLException: "SAXBuilder.build(String)
expects a String URI not XML Content" would be a whole lot easier to manage
than the exception you have....

Rolf
Post by cliff palmer
Rolf, that worked - it's mysterious that the version in my code
(passing the string) worked until there was a URL, but I can live with
mysteries in light of working code :).
Thanks!
Cliff
Post by Rolf Lear
Hi Cliff.
I think the problem is that the content of the column 'xml_contents' of the
table 'xml_table' is actual XML data.... not a file name/URI.
   String xmlcontent = rs.getString(2);
   StringReader reader = new StringReader(xmlcontent);
   xmlDoc = saxBuilder.build(reader);
See http://jdom.org/docs/faq.html#a0210
Rolf
cliff palmer
2012-02-10 18:32:16 UTC
Permalink
Rolf, that sounds like a great idea.
Another validation process that would help when doing XML exploration
(i.e you don't know what is in the data and are trying to find out)
would be to throw an exception of the string is null and not use the
"null pointer" exception.
Thanks again for your help.
Cliff
Post by Rolf Lear
Hi Cliff.
That would be a mystery, but my suspicion is that it has not worked at
all... if it *did* work then there are other problems ... ;-)
I suspect that everything has been failing - maybe the first record from
the database had an xmlns, and it has been a red-herring for you.
I did run some test code through, and it causes similar errors regardless
of the actual XML content.
I wonder if it would help if we put a validation process in the
SAXBuilder.build(String) method that inspected the string, and if the first
non-white character is '<' it throws an exception.... that should catch all
instances where XML content is supplied to the method, since '<' is never
valid in a URI..... and it is always the first character in any valid XML
document.....
getting an exception:  MalformedURLException: "SAXBuilder.build(String)
expects a String URI not XML Content" would be a whole lot easier to manage
than the exception you have....
Rolf
Post by cliff palmer
Rolf, that worked - it's mysterious that the version in my code
(passing the string) worked until there was a URL, but I can live with
mysteries in light of working code :).
Thanks!
Cliff
Post by Rolf Lear
Hi Cliff.
I think the problem is that the content of the column 'xml_contents' of the
table 'xml_table' is actual XML data.... not a file name/URI.
   String xmlcontent = rs.getString(2);
   StringReader reader = new StringReader(xmlcontent);
   xmlDoc = saxBuilder.build(reader);
See http://jdom.org/docs/faq.html#a0210
Rolf
Grzegorz
2012-02-11 21:15:24 UTC
Permalink
Post by cliff palmer
Another validation process that would help when doing XML exploration
(i.e you don't know what is in the data and are trying to find out)
would be to throw an exception of the string is null and not use the
"null pointer" exception.
Why not? NullPointerException is designed to handle just that.

Regards,
Grzegorz
Paul Libbrecht
2012-02-11 21:44:33 UTC
Permalink
I would vote for a NullPointerException with a message that says "Null URI".
But I note that I generally interpret any NullPointerException as a place where a "." is.
And in the case discussed in this thread, this is far far deep inside.

paul
Post by cliff palmer
Another validation process that would help when doing XML exploration
(i.e you don't know what is in the data and are trying to find out)
would be to throw an exception of the string is null and not use the
"null pointer" exception.
Why not? NullPointerException is designed to handle just that.
Regards,
Grzegorz
_______________________________________________
Rolf Lear
2012-02-11 22:29:47 UTC
Permalink
I prefer NullPointer for null values too. In some limited places it
makes sense to throw IllegalArgumentException.

My 'habit' in the past (and it is recently changing...) has been to:
- if I actually check for null I throw IllegalArgumentException.
- do not check, and then let Java throw the NPE on de-reference.

Having read some things recently in Effective Java, and also more
carefully implementing core API's like the Collections API, I am now
more inclined to do an explicit null check, and throw NullPointerException.

I think in cases like JDOM where the code is open-source, it is less
critical to validate-for-null in methods because the stack trace on NPE
is easy to follow through on.... If JDOM was closed source, and people
got a NPE, they would be (justifiably) peeved, especially because the
stack trace would be no help for identifying the null reference.

Still the question is whether every parameter should be validated before
use.... in this case, since the actual null de-reference happens deep in
the URI code it sort of makes sense to check... especially if we are
adding a check for an invalid URI...

So, in this case, I think I will do an explicit check-for-null, and
throw NullPointerException... but in a general case the issue is more
'grey'.

I also think, in this case, that the issue of having XML content instead
of a SystemID in build(String) is common enough to do the
pre-validation.... with a better error message. In general though, I
think that people misusing the documented API should not expect
'hand-holding' error messages. GIGO

Rolf
Post by Paul Libbrecht
I would vote for a NullPointerException with a message that says "Null URI".
But I note that I generally interpret any NullPointerException as a place where a "." is.
And in the case discussed in this thread, this is far far deep inside.
paul
Post by cliff palmer
Another validation process that would help when doing XML exploration
(i.e you don't know what is in the data and are trying to find out)
would be to throw an exception of the string is null and not use the
"null pointer" exception.
Why not? NullPointerException is designed to handle just that.
Regards,
Grzegorz
_______________________________________________
_______________________________________________
Michael Kay
2012-02-11 23:33:14 UTC
Permalink
Post by Rolf Lear
Having read some things recently in Effective Java, and also more
carefully implementing core API's like the Collections API, I am now
more inclined to do an explicit null check, and throw
NullPointerException.
I started putting @Nullable and @NotNull assertions into my code, and
this got very confusing. I don't know about other tools, but IntelliJ
gives you warnings if you say @NotNull and then have an "if null" test,
while if you say @Nullable then the wrong information is conveyed to the
user of the API.

Michael Kay
Saxonica
Paul Libbrecht
2012-02-12 13:00:02 UTC
Permalink
Michael,

can you display a javadoc that displays this use of @Nullable or @NotNull?
A screenshot with IntelliJ would also be useful but well...
I would think IntelliJ is uselessly zealous to complain about the "if null"; they're rather easy to speak to about such... I am sure it's a matter of maturity for such annotations.

thanks

paul
Post by Michael Kay
Having read some things recently in Effective Java, and also more carefully implementing core API's like the Collections API, I am now more inclined to do an explicit null check, and throw NullPointerException.
Michael Kay
Saxonica
_______________________________________________
Michael Kay
2012-02-12 15:53:54 UTC
Permalink
Post by Paul Libbrecht
Michael,
Not readily because I commented out all the annotations when I realised
they stopped the code being compiled under JDK 1.5, and I would have to
reconfigure things quite a bit to get back to where I was.

Michael Kay
Saxonica

Loading...