001: /* Copyright 2001 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.utils;
007:
008: import java.io.IOException;
009: import java.io.InputStream;
010:
011: import javax.xml.parsers.DocumentBuilder;
012: import javax.xml.parsers.DocumentBuilderFactory;
013:
014: import org.apache.commons.logging.Log;
015: import org.apache.commons.logging.LogFactory;
016: import org.w3c.dom.Document;
017: import org.xml.sax.EntityResolver;
018: import org.xml.sax.InputSource;
019: import org.xml.sax.SAXException;
020:
021: /**
022: * Produces an empty Document implementation
023: * @author Bernie Durfee, bdurfee@interactivebusiness.com
024: * @version $Revision: 36684 $
025: */
026: public class DocumentFactory {
027:
028: private static final Log log = LogFactory
029: .getLog(DocumentFactory.class);
030:
031: protected static DocumentFactory _instance;
032: protected static final LocalDocumentBuilder localDocBuilder = new LocalDocumentBuilder();
033: protected DocumentBuilderFactory dbFactory = null;
034:
035: protected static synchronized DocumentFactory instance() {
036: if (_instance == null) {
037: _instance = new DocumentFactory();
038: }
039: return _instance;
040: }
041:
042: protected DocumentFactory() {
043:
044: try {
045: dbFactory = DocumentBuilderFactory.newInstance();
046: dbFactory.setNamespaceAware(true);
047: dbFactory.setValidating(false);
048: } catch (Exception e) {
049: log
050: .error(
051: "DocumentFactory: unable to initialize DocumentBuilderFactory",
052: e);
053: }
054: }
055:
056: /**
057: * Returns a new copy of a Document implementation. This will
058: * return an <code>IPortalDocument</code> implementation.
059: * @return an empty org.w3c.dom.Document implementation
060: */
061: public static Document getNewDocument() {
062: return newDocumentBuilder().newDocument();
063: }
064:
065: /**
066: * Returns a new copy of a Document implementation.
067: * @return an empty org.w3c.dom.Document implementation
068: */
069: static Document __getNewDocument() {
070: Document doc = newDocumentBuilder().newDocument();
071: return doc;
072: }
073:
074: /**
075: * @param stream
076: * @return Document
077: * @throws IOException
078: * @throws SAXException
079: * @deprecated Does not provide document identifier in exceptions.
080: */
081: public static Document getDocumentFromStream(InputStream stream)
082: throws IOException, SAXException {
083: DocumentBuilder builder = newDocumentBuilder();
084: Document doc = builder.parse(stream);
085: return doc;
086: }
087:
088: /**
089: * @param stream
090: * @param er
091: * @return Document
092: * @throws IOException
093: * @throws SAXException
094: * @deprecated Does not provide document identifier in exceptions.
095: */
096: public static Document getDocumentFromStream(InputStream stream,
097: EntityResolver er) throws IOException, SAXException {
098: DocumentBuilder builder = newDocumentBuilder();
099: builder.setEntityResolver(er);
100: Document doc = builder.parse(stream);
101: return doc;
102: }
103:
104: public static Document getDocumentFromStream(InputStream stream,
105: String publicId) throws IOException, SAXException {
106: DocumentBuilder builder = newDocumentBuilder();
107: InputSource source = new InputSource(stream);
108: source.setPublicId(publicId);
109: Document doc = builder.parse(source);
110: return doc;
111: }
112:
113: public static Document getDocumentFromStream(InputStream stream,
114: EntityResolver er, String publicId) throws IOException,
115: SAXException {
116: DocumentBuilder builder = newDocumentBuilder();
117: builder.setEntityResolver(er);
118: InputSource source = new InputSource(stream);
119: source.setPublicId(publicId);
120: Document doc = builder.parse(source);
121: return doc;
122: }
123:
124: public static DocumentBuilder newDocumentBuilder() {
125: DocumentBuilder builder = (DocumentBuilder) localDocBuilder
126: .get();
127: return builder;
128: }
129:
130: protected static class LocalDocumentBuilder extends ThreadLocal {
131: protected Object initialValue() {
132: Object r = null;
133: try {
134: r = instance().dbFactory.newDocumentBuilder();
135: } catch (Exception e) {
136: log.error(e, e);
137: }
138: return r;
139: }
140: }
141: }
|