Skip to Content.
Sympa Menu

xom-interest - [XOM-interest] static Builder.create methods instead of constructors?

xom-interest AT lists.ibiblio.org

Subject: XOM API for Processing XML with Java

List archive

Chronological Thread  
  • From: Wolfgang Hoschek <whoschek AT lbl.gov>
  • To: xom-interest AT lists.ibiblio.org
  • Subject: [XOM-interest] static Builder.create methods instead of constructors?
  • Date: Wed, 11 Aug 2004 23:19:58 -0700

Question: Would it perhaps be better to have static Builder.create methods instead of constructors?

That way one would gain a little flexibility in how/what kind of builder is returned. For example,
one could (in some later XOM release) conceivably return a builder from a thread local pool or similar (recognizing that constructing the underlying XMLReader of a Builder is an extremely expensive operation; takes 2 milliseconds or more).

Background: currently i'm using something like this to reuse builders for lots of smallish XML files in a thread-safe way (which is not quite general enough for everyones needs, but should outline the overall idea):

import nu.xom.Builder;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

/**
* A pool holding one XOM Builder per thread.
*/
public class ThreadLocalBuilder extends ThreadLocal {

private boolean isValidating;
private String namespace = null;
private String schema = null;

public ThreadLocalBuilder() {
isValidating = false;
}

public ThreadLocalBuilder(String namespace, String schema) {
isValidating = true;
this.namespace = namespace;
this.schema = schema;
}

public Builder getBuilder() {
return (Builder) get();
}

protected Object initialValue() {
if (! isValidating) return new Builder();

try {
// make xerces cache the parsed schema grammar for
better performance
// System.setProperty("org.apache.xerces.xni.parser.XMLParserConfiguration" ,
//
"org.apache.xerces.parsers.XMLGrammarCachingConfiguration");

XMLReader parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");


//parser.setFeature("http://xml.org/sax/features/validation";, true);
parser.setFeature("http://apache.org/xml/features/validation/ schema", true);

if (namespace != null && schema != null) {
parser.setProperty(

"http://apache.org/xml/properties/schema/external-schemaLocation";,
namespace + " " + schema);
}
else if (schema != null) {
parser.setProperty(
"http://apache.org/xml/properties/schema/external- noNamespaceSchemaLocation",
schema);
}

return new Builder(parser, true);
} catch (SAXException e) {
throw new RuntimeException(e);
}
}

}


-----------------------------------------------------------------------
Wolfgang Hoschek | email: whoschek AT lbl.gov
Distributed Systems Department | phone: (415)-533-7610
Berkeley Laboratory | http://dsd.lbl.gov/~hoschek/
-----------------------------------------------------------------------





Archive powered by MHonArc 2.6.24.

Top of Page