alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.schema2beansdev;

import java.util.*;
import java.io.*;

import org.netbeans.modules.schema2beans.*;

/**
 *
 *	This class implement the Document Definition handler in order to build
 *	the internal tree representation of the DD DTD.
 *
 */
public class DocDefParser extends GeneralParser implements SchemaParser {

    static class MissingEndOfEltException extends RuntimeException {
	String propName;
	
	public MissingEndOfEltException(String propName) {
	    this.propName = propName;
	}
    }
    
    static private final int WORD_NO_CONTEXT	= 0;
    static private final int WORD_CHECK		= 1;
    static private final int WORD_COMMENT	= 2;
    static private final int WORD_ELEMENT1	= 3;
    static private final int WORD_ELEMENT	= 4;
    static private final int WORD_ATTLIST1	= 5;
    static private final int WORD_ATTLIST	= 6;
    static private final int WORD_PI		= 7;
    
    static String errHeader = "DTD parsing failed: ";	// NOI18N
    
    //	Buffer used to read the file by chunks
    private char 		buffer[] = new char[BUFFER_SIZE];
    
    //	Current size of the buffer
    private int			bufSize;
    
    //	Reading offset in the buffer while parsing
    private int			bufScan;

    protected static int BUFFER_SIZE	=	4096;
    
    //	Handler to callback with the tokens found in the DTD.
    private DocDefHandler	handler;
    
    private GenBeans.Config config = null;
    
    public DocDefParser() {
    }
    
    public DocDefParser(GenBeans.Config config, DocDefHandler handler) {
        this.config = config;
        this.filename = config.getFilename();
        this.schemaIn = config.getFileIn();
        this.handler = handler;
    }
    
    public void setFilename(File filename) {
        this.filename = filename;
    }
    
    public File getFilename() {
        return filename;
    }
    
    public void setHandler(DocDefHandler handler) {
	this.handler = handler;
    }
    
    public DocDefHandler getHandler() {
	return this.handler;
    }
    
    protected boolean checkBuffer() throws IOException {
	if (this.bufScan >= this.bufSize) {
	    //	Buffer either empty or already parsed - get more from the file
		this.bufSize = reader.read(this.buffer);
		if (this.bufSize == -1)
		    return false;
		this.bufScan = 0;
	}
	return true;
    }
    
    /**
     *	Returns the next character of the parsed file.
     */
    protected char getNext() throws IOException {
	if (this.checkBuffer())
	    return this.buffer[this.bufScan++];
	else
	    return '\0';
    }
    
    /**
     *	Get the next character without moving the parser offset.
     */
    protected char peekNext() throws IOException {
	if (this.checkBuffer())
	    return this.buffer[this.bufScan];
	else
	    return '\0';
    }
    
    /**
     *	Return the instance value associated with an element
     */
    private static int getInstanceValue(char c) {
	switch(c) {
	    case '*':
		return Common.TYPE_0_N;
	    case '+':
		return Common.TYPE_1_N;
	    case '?':
		return Common.TYPE_0_1;
	    default:
		//  We assume this default value if nothing is specified
		return Common.TYPE_1;
	}
    }
    
    /**
     *  Find out the type of the current word
     */
    private int processWord(StringBuffer curWord, int wordContext) throws SchemaParseException{
	String 	word = curWord.toString();
	int	len = word.length();
	
	if (len >0) {
	    //	We have some word to play with
	    switch (wordContext) {
		case WORD_CHECK:
            if (word.startsWith("--")) {	// NOI18N
                if (len > 2)
                    word = curWord.substring(2);
                else
                    word = "";	// NOI18N
                
                this.handler.startElement(word, Common.COMMENT);
                wordContext = WORD_COMMENT;
            } else if (word.equals("ELEMENT"))	// NOI18N
                wordContext = WORD_ELEMENT1;
            else if (word.equals("ATTLIST"))	// NOI18N
                wordContext = WORD_ATTLIST1;
            else {
                //System.err.println("Error: found an unknown '':
		    //	Might be the end of a comment or ' without '' without '
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.