Re: Specifying a DTD file when parsing XML files that don't contain !DOCTYPE lines

2007-02-26 Thread Will Holcomb

Are you using the default jdk parser? I don't really know if there is some
commonly accepted method for doing it, but I have this in my ant build.xml:

jar destfile=dist/templ-0.1.jar basedir=build
 zipfileset src=lib/log4j-1.2.13.jar/
 zipfileset src=lib/xercesImpl.jar/
 manifest
   attribute name=Built-By value=${user.name}/
   attribute name=Main-Class value=org.himinbi.templ.HooksProcessor/
 /manifest
/jar

This sticks the xerces classes in the jar with my classes. I think though
that you can just include xercesImpl.jar in the classpath. (I started this a
week ago and am still learning myself.)

On 2/25/07, Mike O'Leary [EMAIL PROTECTED] wrote:


 When I try this, I get an error that says:

javax.xml.parsers.ParserConfigurationException:
jaxp_feature_not_supported: Feature 
http://xml.org/sax/features/use-entity-resolver2; is not supported.

What am I doing wrong?

Mike



Re: Specifying a DTD file when parsing XML files that don't contain !DOCTYPE lines

2007-02-26 Thread Will Holcomb

On 2/26/07, Mike O'Leary [EMAIL PROTECTED] wrote:


 This file is in JDK 1.5 in the package
com.sun.org.apache.xerces.internal.jaxp, but it is defined in a jar file
called rt.jar. I downloaded a copy of Xerces-J-tools.2.9.0 and added
xercesImpl.jar to my classpath. Now when I get to the setFeature function
call, I get an error that says

Exception in thread main java.lang.AbstractMethodError:
javax.xml.parsers.DocumentBuilderFactory.setFeature(Ljava/lang/String;Z)V

It does look like the version of DocumentBuilderFactoryImpl does not
contain a definition for setFeature and DcoumentBuilderFactory is an
abstract class, so I guess this all makes sense. Did you encounter things
like this when you were setting things up?


I didn't. I didn't realize at the time that I was getting lucky. =)

For me, the following code:

final static String DOCUMENT_CLASS_ID = 
http://apache.org/xml/properties/dom/document-class-name;;
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
log.debug(Document Class:  + dbFactory.getAttribute(DOCUMENT_CLASS_ID));

Prints:

Document Class: org.apache.xerces.dom.DocumentImpl

The system property javax.xml.parsers.DocumentBuilderFactory is not set.
What I have that compiles and runs. It's very alpha, but it does compile and
work for me.

http://odin.himinbi.org/jars/src/org/himinbi/templ/
http://odin.himinbi.org/templ/build.xml

If you're really stuck and want to try and run it, the hierarchy should look
like:

/build.xml
/src/org/himinbi/templ/
/lib/

Where /lib/ contains the jars from the most recent releases of log4j and
xerces.

Will


Specifying a DTD file when parsing XML files that don't contain !DOCTYPE lines

2007-02-25 Thread Mike O'Leary
I have a project in which I am supposed to write a parser in Java for a set
of files that don't contain !DOCTYPE ... lines. A DTD file was defined for
these files, and the files contain entity references whose definitions are
in the DTD file. I looked at the JAXP documentation, and it looks like I
have several options: There appear to be a variety of ways I can provide a
system id to a factory object, to a parser object, or as an argument to the
parse function. Or I can specify an entityResolver function for the parser,
or I can tell the parser not to expand entity references. I have tried
several of these, and I haven't gotten any of them to work yet. In each
case, it appears that the DTD file is not read and the parse crashes with an
error saying that an entity was referenced that was not declared. Can anyone
provide me with a sort of recipe for constructing a parser in which the DTD
file is specified without the use of a !DOCTYPE ...  line in the file to
be parsed, and the DTD file is read and is available to resolving entity
references that are encountered during the parse? Thanks.

Mike O'Leary



Re: Specifying a DTD file when parsing XML files that don't contain !DOCTYPE lines

2007-02-25 Thread Will Holcomb

I happened to send a message to this list asking this exact question earlier
this week. I figure I'll pass on the kindness someone showed me and answer
it this time/ (Perhaps this should go in the FAQ because it isn't really
intuitive.)

final static String ENTITY_RESOLVER_2_ID = 
http://xml.org/sax/features/use-entity-resolver2;;
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
dbFactory.setFeature(ENTITY_RESOLVER_2_ID, true);
DocumentBuilder builder = dbFactory.newDocumentBuilder();
builder.setEntityResolver(new DTDEntityResolver(new
FileReader(dtdFilename)));

public class DTDEntityResolver implements EntityResolver2 {
 InputSource input;
 public DTDEntityResolver(Reader dtd) { input = new InputSource(dtd);
}
 public InputSource getExternalSubset(String name, String baseURI) throws
SAXException {
  return input;
 }

   public InputSource resolveEntity(String publicId, String systemId) {
   log.debug(Entity:  + publicId +  :  + systemId);
   return null;
   }

   public InputSource resolveEntity(String name, String publicId, String
baseURI, String systemId)
   throws SAXException {
   log.debug(Entity:  + name + :  + baseURI +  [ + publicId +  :
 + systemId + ]);
   return null;
   }
}


On 2/25/07, Mike O'Leary [EMAIL PROTECTED] wrote:


 I have a project in which I am supposed to write a parser in Java for a
set of files that don't contain !DOCTYPE ... lines. A DTD file was defined
for these files, and the files contain entity references whose definitions
are in the DTD file. I looked at the JAXP documentation, and it looks like I
have several options: There appear to be a variety of ways I can provide a
system id to a factory object, to a parser object, or as an argument to the
parse function. Or I can specify an entityResolver function for the parser,
or I can tell the parser not to expand entity references. I have tried
several of these, and I haven't gotten any of them to work yet. In each
case, it appears that the DTD file is not read and the parse crashes with an
error saying that an entity was referenced that was not declared. Can anyone
provide me with a sort of recipe for constructing a parser in which the DTD
file is specified without the use of a !DOCTYPE ...  line in the file to
be parsed, and the DTD file is read and is available to resolving entity
references that are encountered during the parse? Thanks.

Mike O'Leary



Re: Specifying a DTD file when parsing XML files that don't contain !DOCTYPE lines

2007-02-25 Thread Will Holcomb

Didn't actually mean to send that just yet, the code is right, just ignore
the log statements...

Will Holcomb

On 2/25/07, Will Holcomb [EMAIL PROTECTED] wrote:


I happened to send a message to this list asking this exact question
earlier this week. I figure I'll pass on the kindness someone showed me and
answer it this time/ (Perhaps this should go in the FAQ because it isn't
really intuitive.)

final static String ENTITY_RESOLVER_2_ID = 
http://xml.org/sax/features/use-entity-resolver2;;
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
dbFactory.setFeature(ENTITY_RESOLVER_2_ID, true);
DocumentBuilder builder = dbFactory.newDocumentBuilder();
builder.setEntityResolver(new DTDEntityResolver(new
FileReader(dtdFilename)));

public class DTDEntityResolver implements EntityResolver2 {
  InputSource input;
  public DTDEntityResolver(Reader dtd) { input = new InputSource(dtd); }
  public InputSource getExternalSubset(String name, String baseURI)


   throws SAXException { return input; }

  public InputSource resolveEntity(String publicId, String systemId) {return 
null;}
  public InputSource resolveEntity(String name, String publicId, String
baseURI, String systemId)
throws SAXException { return null; }
}



Re: Specifying a DTD file when parsing XML files that don't contain !DOCTYPE lines

2007-02-25 Thread Will Holcomb

One more note about a bug I found in this code. It only works once. When you
parse a second document, you get an exception because the FileReader for the
DTD has already been closed.

Will Holcomb

On 2/25/07, Will Holcomb [EMAIL PROTECTED] wrote:


I happened to send a message to this list asking this exact question
 earlier this week. I figure I'll pass on the kindness someone showed me and
 answer it this time/ (Perhaps this should go in the FAQ because it isn't
 really intuitive.)

 final static String ENTITY_RESOLVER_2_ID = 
http://xml.org/sax/features/use-entity-resolver2;;

 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
 dbFactory.setFeature(ENTITY_RESOLVER_2_ID, true);
 DocumentBuilder builder = dbFactory.newDocumentBuilder();
 builder.setEntityResolver(new DTDEntityResolver(new
 FileReader(dtdFilename)));

 public class DTDEntityResolver implements EntityResolver2 {
   InputSource input;
   public DTDEntityResolver(Reader dtd) { input = new InputSource(dtd); }
   public InputSource getExternalSubset(String name, String baseURI)

throws SAXException { return input; }
   public InputSource resolveEntity(String publicId, String systemId) {return 
null;}
   public InputSource resolveEntity(String name, String publicId, String
 baseURI, String systemId)
 throws SAXException { return null; }
 }