001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.tools.common.dom;
019:
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.util.logging.Level;
023: import java.util.logging.Logger;
024:
025: import javax.xml.XMLConstants;
026: import javax.xml.parsers.DocumentBuilder;
027: import javax.xml.parsers.DocumentBuilderFactory;
028: import javax.xml.transform.stream.StreamSource;
029: import javax.xml.validation.Schema;
030: import javax.xml.validation.SchemaFactory;
031:
032: import org.w3c.dom.Document;
033: import org.xml.sax.SAXException;
034:
035: import org.apache.cxf.common.logging.LogUtils;
036:
037: /**
038: * (not thread safe)
039: *
040: */
041: public class ExtendedDocumentBuilder {
042:
043: private static final Logger LOG = LogUtils
044: .getL7dLogger(ExtendedDocumentBuilder.class);
045:
046: private final DocumentBuilderFactory parserFactory;
047: private DocumentBuilder parser;
048:
049: private SchemaFactory schemaFactory;
050: private Schema schema;
051:
052: public ExtendedDocumentBuilder() {
053: parserFactory = DocumentBuilderFactory.newInstance();
054: parserFactory.setNamespaceAware(true);
055: }
056:
057: private InputStream getSchemaLocation() {
058: String toolspec = "/org/apache/cxf/tools/common/toolspec/tool-specification.xsd";
059: return getClass().getResourceAsStream(toolspec);
060: }
061:
062: public void setValidating(boolean validate) {
063: if (validate) {
064: this .schemaFactory = SchemaFactory
065: .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
066: try {
067: this .schema = schemaFactory.newSchema(new StreamSource(
068: getSchemaLocation()));
069: } catch (org.xml.sax.SAXException e) {
070: LOG.log(Level.SEVERE, "SCHEMA_FACTORY_EXCEPTION_MSG");
071: }
072: try {
073: this .parserFactory.setSchema(this .schema);
074: } catch (UnsupportedOperationException e) {
075: LOG.log(Level.WARNING, "DOC_PARSER_NOT_SUPPORTED", e);
076: }
077: }
078: }
079:
080: private DocumentBuilder getParser() {
081: if (parser == null) {
082: try {
083: parser = parserFactory.newDocumentBuilder();
084: } catch (javax.xml.parsers.ParserConfigurationException e) {
085: LOG.log(Level.SEVERE,
086: "NEW_DOCUMENT_BUILDER_EXCEPTION_MSG");
087: }
088: }
089: return parser;
090: }
091:
092: public Document parse(InputStream in) throws SAXException,
093: IOException {
094: if (in == null && LOG.isLoggable(Level.FINE)) {
095: LOG
096: .fine("ExtendedDocumentBuilder trying to parse a null inputstream");
097: }
098: return getParser().parse(in);
099: }
100:
101: }
|