001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.visualweb.designer.markup;
042:
043: import javax.xml.parsers.DocumentBuilder;
044: import javax.xml.parsers.DocumentBuilderFactory;
045: import javax.xml.parsers.FactoryConfigurationError;
046: import javax.xml.parsers.ParserConfigurationException;
047:
048: /**
049: * XXX Copied from former insync/Factories
050: *
051: * <p>Description: </p>
052: * @author Carl Quinn
053: * @version 1.0
054: */
055: final class RaveDocumentBuilderFactory {
056:
057: DocumentBuilderFactory domFactory; // DOM parser factory
058: //SAXParserFactory saxFactory; // SAX parser factory
059:
060: private static RaveDocumentBuilderFactory instance;
061:
062: private static final String PROPERTY_NAME_DOCUMENT_BUILDER_FACTORY = "javax.xml.parsers.DocumentBuilderFactory"; // NOI18N
063: private static final String PROPERTY_VALUE_XERCES_DOCUMENT_BUILDER_FACTORY = "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl"; // NOI18N
064:
065: /**
066: * Make the singleton & instantiate some shared worker objects
067: */
068: private RaveDocumentBuilderFactory() {
069: //tidy = new Tidy();
070: //Configuration conf = tidy.getConfiguration();
071:
072: // XXX CHECK Assuring the xerces is used, which document is expected (implementing EvenTarget) in insync.
073: String oldValue = System
074: .getProperty(PROPERTY_NAME_DOCUMENT_BUILDER_FACTORY);
075: try {
076: System.setProperty(PROPERTY_NAME_DOCUMENT_BUILDER_FACTORY,
077: PROPERTY_VALUE_XERCES_DOCUMENT_BUILDER_FACTORY);
078:
079: // Setup our XML DOM Document Builder (parser) factory
080: domFactory = DocumentBuilderFactory.newInstance();
081: //domFactory.setCoalescing(false);
082: //domFactory.setExpandEntityReferences(false);
083: //domFactory.setIgnoringComments(false);
084: //domFactory.setIgnoringElementContentWhitespace(true);
085: domFactory.setNamespaceAware(true);
086: domFactory.setValidating(false);
087: //domFactory.setFeature("http://apache.org/xml/features/continue-after-fatal-error",
088: // true);
089: /*
090: Trace.trace("insync.markup",
091: "MUF DocumentBuilderFactory" +
092: " c:" + domFactory.isCoalescing() +
093: " eer:" + domFactory.isExpandEntityReferences() +
094: " ic:" + domFactory.isIgnoringComments() +
095: " iecw:" + domFactory.isIgnoringElementContentWhitespace() +
096: " na:" + domFactory.isNamespaceAware() +
097: " v:" + domFactory.isValidating());
098: Trace.out.flush();*/
099:
100: // Setup
101: //saxFactory = SAXParserFactory.newInstance();
102: //saxFactory.setNamespaceAware(true);
103: //saxFactory.setValidating(false);
104: //to set features: saxFactory.setFeature("", true);
105: } catch (FactoryConfigurationError ex) {
106: // Trace.trace("insync.markup", "Exception creating factory");
107: // Trace.trace("insync.markup", e);
108: ex.printStackTrace();
109: } finally {
110: if (oldValue == null) {
111: System
112: .clearProperty(PROPERTY_NAME_DOCUMENT_BUILDER_FACTORY);
113: } else {
114: System.setProperty(
115: PROPERTY_NAME_DOCUMENT_BUILDER_FACTORY,
116: oldValue);
117: }
118: }
119: }
120:
121: //SAXParser newSaxParser() throws ParserConfigurationException, SAXException {
122: // return saxFactory.newSAXParser();
123: //}
124:
125: public static DocumentBuilder newDocumentBuilder(boolean useCss,
126: boolean sourceDocument) throws ParserConfigurationException {
127: return getInstance().createDocumentBuilder(useCss,
128: sourceDocument);
129: }
130:
131: private DocumentBuilder createDocumentBuilder(boolean useCss,
132: boolean sourceDocument) throws ParserConfigurationException {
133: if (useCss) {
134: java.util.Hashtable attrs = null;
135: // TODO - duplicate any attributes?
136: try {
137: return new RaveDocumentBuilder(domFactory, attrs,
138: sourceDocument);
139: } catch (Exception ex) {
140: ex.printStackTrace();
141: // fall through and use document builder below
142: }
143: /* I MIGHT be able to do this instead. But how do I set the property?
144: There's no setProperty. setFeature doesn't seem to be it.
145: domFactory.setProperty("http://apache.org/xml/properties/dom/document-class-name",
146: "org.netbeans.modules.visualweb.insync.markup.RaveDocument");
147: */
148: }
149: return domFactory.newDocumentBuilder();
150: }
151:
152: private static synchronized RaveDocumentBuilderFactory getInstance() {
153: if (instance == null)
154: instance = new RaveDocumentBuilderFactory();
155: return instance;
156: }
157:
158: }
|