Package XMLDOM2

Source Code of XMLDOM2.DomFactory

/*
Copyright (c) 2003-2009 ITerative Consulting Pty Ltd. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
 
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
   
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder

 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


*/
package XMLDOM2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import Framework.Constants;
import Framework.ErrorMgr;
import Framework.MemoryStream;
import Framework.TextData;
/**
* Factory class to create documents
*/
public class DomFactory {

    private DomFactory() {
        super();
    }
    public static Document newDocument(){
        try {
            DocumentBuilderFactory factory =
                DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.newDocument()// Create from whole cloth

            return doc;
        } catch (DOMException e) {
            XMLDOM2.DOMException errorVar = new XMLDOM2.DOMException("Document creation failed", e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        } catch (ParserConfigurationException e) {
            XMLDOM2.DOMException errorVar = new XMLDOM2.DOMException("Document creation failed", e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        }
    }

    /**
     * Using a MemoryStream (not an InputStream) seems much more reliable.  CraigM:12/01/2009.
     */
    public static Document newDocument(MemoryStream stream) {
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            TextData fullXML = new TextData();
            TextData lineXML = new TextData();
           
            stream.open(Constants.SP_AM_READ);

            while (stream.readLine(lineXML) != -1) {
              fullXML.concat(lineXML);
            }
            stream.close();
            StringReader strReader = new StringReader(fullXML.toString().trim());
            Document doc = builder.parse(new InputSource(strReader));

            return doc;
        } catch (IOException e) {
          XMLDOM2.DOMException errorVar = new XMLDOM2.DOMException("Document creation failed", e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        } catch (ParserConfigurationException e) {
          XMLDOM2.DOMException errorVar = new XMLDOM2.DOMException("Document creation failed", e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        } catch (SAXException e) {
          XMLDOM2.DOMException errorVar = new XMLDOM2.DOMException("Document creation failed", e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        }
    }

    public static Document newDocument(InputStream is) {
        try {
            DocumentBuilderFactory factory =
                DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
           
            // AD:26/8/2008 Resolved GMA-29. Removes EOL character which caused parsing errors.
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            String str;
            StringBuilder strBuilder = new StringBuilder();
            while ((str = reader.readLine()) != null) {
                strBuilder.append(str);
            }
            reader.close();
            StringReader strReader = new StringReader(strBuilder.toString().trim());
            Document doc = builder.parse(new InputSource(strReader));
           
            return doc;
        } catch (IOException e) {
            XMLDOM2.DOMException errorVar = new XMLDOM2.DOMException("Document creation failed", e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        } catch (ParserConfigurationException e) {
            XMLDOM2.DOMException errorVar = new XMLDOM2.DOMException("Document creation failed", e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        } catch (SAXException e) {
            XMLDOM2.DOMException errorVar = new XMLDOM2.DOMException("Document creation failed", e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        }
    }
}
TOP

Related Classes of XMLDOM2.DomFactory

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.