All Downloads are FREE. Search and download functionalities are using the official Maven repository.

oracle.toplink.essentials.platform.xml.SAXDocumentBuilder Maven / Gradle / Ivy

There is a newer version: 2.1-60f
Show newest version
/*
 * The contents of this file are subject to the terms 
 * of the Common Development and Distribution License 
 * (the "License").  You may not use this file except 
 * in compliance with the License.
 * 
 * You can obtain a copy of the license at 
 * glassfish/bootstrap/legal/CDDLv1.0.txt or 
 * https://glassfish.dev.java.net/public/CDDLv1.0.html. 
 * See the License for the specific language governing 
 * permissions and limitations under the License.
 * 
 * When distributing Covered Code, include this CDDL 
 * HEADER in each file and include the License file at 
 * glassfish/bootstrap/legal/CDDLv1.0.txt.  If applicable, 
 * add the following below this CDDL HEADER, with the 
 * fields enclosed by brackets "[]" replaced with your 
 * own identifying information: Portions Copyright [yyyy] 
 * [name of copyright owner]
 */
// Copyright (c) 1998, 2007, Oracle. All rights reserved.  
package oracle.toplink.essentials.platform.xml;

import java.util.Stack;
import org.xml.sax.SAXException;
import org.xml.sax.Locator;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.ProcessingInstruction;
import org.w3c.dom.Text;

public class SAXDocumentBuilder implements ContentHandler {
    private Document document;
    private Stack nodes = new Stack();
    private XMLPlatform xmlPlatform;

    public SAXDocumentBuilder() {
        super();
        nodes = new Stack();
        xmlPlatform = XMLPlatformFactory.getInstance().getXMLPlatform();
    }

    public Document getDocument() {
        return document;
    }

    public Document getInitializedDocument() throws SAXException {
        if (document == null) {
            try {
                document = xmlPlatform.createDocument();
                nodes.push(document);
            } catch (Exception e) {
                throw new SAXException(e);
            }
        }
        return document;
    }

    public void setDocumentLocator(Locator locator) {
    }

    public void startDocument() throws SAXException {
        try {
            document = xmlPlatform.createDocument();
            nodes.push(document);
        } catch (Exception e) {
            throw new SAXException(e);
        }
    }

    public void endDocument() throws SAXException {
        nodes.pop();
    }

    public void startPrefixMapping(String prefix, String uri) throws SAXException {
    }

    public void endPrefixMapping(String prefix) throws SAXException {
    }

    public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
        if ((null != namespaceURI) && ("".equals(namespaceURI))) {
            namespaceURI = null;
        }
        Element element = getInitializedDocument().createElementNS(namespaceURI, qName);
        Node parentNode = (Node)nodes.peek();
        parentNode.appendChild(element);
        nodes.push(element);

        int numberOfAttributes = atts.getLength();
        Attr attribute;
        for (int x = 0; x < numberOfAttributes; x++) {
            element.setAttributeNS(atts.getURI(x), atts.getQName(x), atts.getValue(x));
        }
    }

    public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
        nodes.pop();
    }

    public void characters(char[] ch, int start, int length) throws SAXException {
        String characters = new String(ch, start, length);
        if (characters.trim().length() == 0) {
            return;
        }
        Text text = getInitializedDocument().createTextNode(characters);
        Node parentNode = (Node)nodes.peek();
        parentNode.appendChild(text);
    }

    public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
    }

    public void processingInstruction(String target, String data) throws SAXException {
        ProcessingInstruction pi = getInitializedDocument().createProcessingInstruction(target, data);
        Node parentNode = (Node)nodes.peek();
        parentNode.appendChild(pi);
    }

    public void skippedEntity(String name) throws SAXException {
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy