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

com.articulate.sigma.SimpleDOMParser Maven / Gradle / Ivy

Go to download

Sigma knowledge engineering system is an system for developing, viewing and debugging theories in first order logic. It works with Knowledge Interchange Format (KIF) and is optimized for the Suggested Upper Merged Ontology (SUMO) www.ontologyportal.org.

There is a newer version: 2.10
Show newest version
/*
 * @(#)SimpleDOMParser.java
 * From DevX
 * http://www.devx.com/xml/Article/10114
 * Further modified for Articulate Software by Adam Pease 12/2005
 */
package com.articulate.sigma;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Stack;

/** *****************************************************************   
 * SimpleDOMParser is a highly-simplified XML DOM
 * parser.
 */
public class SimpleDOMParser {

    private static final int[] cdata_start = {'<', '!', '[', 'C', 'D', 'A', 'T', 'A', '['};
    private static final int[] cdata_end = {']', ']', '>'};

    private Reader reader;
    private Stack elements;
    private SimpleElement currentElement;

    /** *****************************************************************
    */
    public SimpleDOMParser() {
        elements = new Stack();
        currentElement = null;
    }

    /** *****************************************************************
     * Read the full path of an XML file and returns the SimpleElement 
     * object that corresponds to its parsed format.
    */
    public static SimpleElement readFile (String filename) {

        SimpleElement result = null;
        File f = new File(filename);
        BufferedReader br = null;

        try {
            br = new BufferedReader(new FileReader(f));
            SimpleDOMParser sdp = new SimpleDOMParser();
            result = sdp.parse(br);
        }
        catch (java.io.IOException e) {
            System.out.println("Error in SimpleDOMParser.readFile(): IO exception parsing file " + 
                               filename + "\n" + e.getMessage());
        }
        finally {
            if (br != null) {
                try {
                    br.close();
                }
                catch (Exception ex) {
                    System.out.println("Error in SimpleDOMParser.readFile(): IO exception parsing file " + 
                                       filename + "\n" + ex.getMessage());
                }
            }
        }
        return result;
    }

    /** *****************************************************************
    */
    public SimpleElement parse(Reader reader) throws IOException {

        this.reader = reader;
        skipPrologs();          // skip xml declaration or DocTypes
        while (true) {
            int index;
            String tagName;
            
            String currentTag = null;
            while (currentTag == null || currentTag.startsWith("