AbstractTag.java :  » Web-Server » jo » com » tagtraum » framework » markup » Java Open Source

Java Open Source » Web Server » jo 
jo » com » tagtraum » framework » markup » AbstractTag.java
/*
License $Id: AbstractTag.java,v 1.7 2003/10/14 00:27:22 hendriks73 Exp $

Copyright (c) 2001-2005 tagtraum industries.

LGPL
====

jo! is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

jo! is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

For LGPL see <http://www.fsf.org/copyleft/lesser.txt>


Sun license
===========

This release contains software by Sun Microsystems. Therefore
the following conditions have to be met, too. They apply to the
files

 - lib/mail.jar
 - lib/activation.jar
 - lib/jsse.jar
 - lib/jcert.jar
 - lib/jaxp.jar
 - lib/crimson.jar
 - lib/servlet.jar
 - lib/jnet.jar
 - lib/jaas.jar
 - lib/jaasmod.jar

contained in this release.

a. Licensee may not modify the Java Platform
   Interface (JPI, identified as classes contained within the javax
   package or any subpackages of the javax package), by creating additional
   classes within the JPI or otherwise causing the addition to or modification
   of the classes in the JPI.  In the event that Licensee creates any
   Java-related API and distribute such API to others for applet or
   application development, you must promptly publish broadly, an accurate
   specification for such API for free use by all developers of Java-based
   software.

b. Software is confidential copyrighted information of Sun and
   title to all copies is retained by Sun and/or its licensors.  Licensee
   shall not modify, decompile, disassemble, decrypt, extract, or otherwise
   reverse engineer Software.  Software may not be leased, assigned, or
   sublicensed, in whole or in part.  Software is not designed or intended
   for use in on-line control of aircraft, air traffic, aircraft navigation
   or aircraft communications; or in the design, construction, operation or
   maintenance of any nuclear facility.  Licensee warrants that it will not
   use or redistribute the Software for such purposes.

c. Software is provided "AS IS," without a warranty
   of any kind. ALL EXPRESS OR IMPLIED REPRESENTATIONS AND WARRANTIES,
   INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
   PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.

d. This License is effective until terminated.  Licensee may
   terminate this License at any time by destroying all copies of Software.
   This License will terminate immediately without notice from Sun if Licensee
   fails to comply with any provision of this License.  Upon such termination,
   Licensee must destroy all copies of Software.

e. Software, including technical data, is subject to U.S.
   export control laws, including the U.S.  Export Administration Act and its
   associated regulations, and may be subject to export or import regulations
   in other countries.  Licensee agrees to comply strictly with all such
   regulations and acknowledges that it has the responsibility to obtain
   licenses to export, re-export, or import Software.  Software may not be
   downloaded, or otherwise exported or re-exported (i) into, or to a national
   or resident of, Cuba, Iraq, Iran, North Korea, Libya, Sudan, Syria or any
   country to which the U.S. has embargoed goods; or (ii) to anyone on the
   U.S. Treasury Department's list of Specially Designated Nations or the U.S.
   Commerce Department's Table of Denial Orders.


Feedback
========

We encourage your feedback and suggestions and want to use your feedback to
improve the Software. Send all such feedback to:
<feedback@tagtraum.com>

For more information on tagtraum industries and jo!
please see <http://www.tagtraum.com/>.


*/
package com.tagtraum.framework.markup;

import java.io.IOException;
import java.io.PushbackReader;

/**
 * Abstract tag suitable for being extended. Features parsing of quoted ('',"")
 * and commented (java style) sections.
 *
 * @author   <a href="mailto:hs@tagtraum.com">Hendrik Schreiber</a>
 * @version 1.1beta1 $Id: AbstractTag.java,v 1.7 2003/10/14 00:27:22 hendriks73 Exp $
 */
public class AbstractTag extends Tag {

    /**
     * Source-Version
     */
    public static String vcid = "$Id: AbstractTag.java,v 1.7 2003/10/14 00:27:22 hendriks73 Exp $";

    public static final int NO_COMMENT = 0;
    public static final int C_COMMENT = 1;
    public static final int CPP_COMMENT = 2;
    private boolean enableCComments = true;
    private boolean enableCPPComments = true;
    private boolean enableSingleQuotes = true;
    private boolean enableDoubleQuotes = true;

    /**
     * Constructor.
     */
    public AbstractTag(String aTagStart) {
        super(aTagStart);
    }

    public boolean isEnableSingleQuotes() {
        return enableSingleQuotes;
    }

    public void setEnableSingleQuotes(boolean enableSingleQuotes) {
        this.enableSingleQuotes = enableSingleQuotes;
    }

    public boolean isEnableDoubleQuotes() {
        return enableDoubleQuotes;
    }

    public void setEnableDoubleQuotes(boolean enableDoubleQuotes) {
        this.enableDoubleQuotes = enableDoubleQuotes;
    }

    public boolean isEnableCComments() {
        return enableCComments;
    }

    public void setEnableCComments(boolean enableCComments) {
        this.enableCComments = enableCComments;
    }

    public boolean isEnableCPPComments() {
        return enableCPPComments;
    }

    public void setEnableCPPComments(boolean enableCPPComments) {
        this.enableCPPComments = enableCPPComments;
    }

    /**
     * Parses this element.
     */
    public void parse(PushbackReader aReader) throws IOException {
        int c = 0;
        boolean endOfTag = false;
        int ringHead = 0;
        int len = 0;
        int bufpos;
        int tagpos;
        String theTagEnd = getTagEnd() + ">";
        int theTagEndLength = theTagEnd.length();
        char buf[] = new char[theTagEndLength];
        int quoteChar = 0;
        int lastChar = 0;
        int comment = NO_COMMENT;
        boolean escaped = false;

        // WORKAROUND: PushbackReader doesn't support skip properly
        // for (int i=0; i<getTagStart().length(); i++) aReader.read();

        while (!endOfTag) {
            c = aReader.read();
            if (c == -1) {
                if (quoteChar != 0) {
                    throw new ParserException("Missing closing quotation " + (char)quoteChar + " in <" + sb.toString());
                }
                if (comment == CPP_COMMENT) {
                    throw new ParserException("Missing closing comment '*/' in <" + sb.toString());
                }
                // EOF
                throw new ParserException("Tag wasn't properly closed before end of file: <" + sb.toString());
                //return; // EOF
            }
            append((char)c);
            len++;
            if (quoteChar == 0) {
                if (lastChar == '/' && c == '/' && enableCComments) {
                    comment = C_COMMENT;
                }
                if (lastChar == '/' && c == '*' && enableCPPComments) {
                    comment = CPP_COMMENT;
                }
            }
            if (enableDoubleQuotes && c == '\"' && !escaped && comment == NO_COMMENT) {
                quoteChar = setQuoteChar(quoteChar, c);
            }
            else if (enableSingleQuotes && c == '\'' && !escaped && comment == NO_COMMENT) {
                quoteChar = setQuoteChar(quoteChar, c);
            }

            buf[ringHead] = (char)c;    // ringbuffer
            if (len >= theTagEndLength && quoteChar == 0 && comment == NO_COMMENT) {
                // compare, beginning from the last position backward
                for (bufpos = ringHead + buf.length, tagpos = theTagEndLength - 1; tagpos >= 0; --bufpos, --tagpos) {
                    if (theTagEnd.charAt(tagpos) != buf[bufpos % buf.length]) {
                        break;
                    }
                }

                endOfTag = (tagpos == -1);
            }

            if (quoteChar == 0) {
                if ((lastChar == '*' && c == '/' && comment == CPP_COMMENT) || (c == '\n' && comment == C_COMMENT)) {
                    comment = NO_COMMENT;
                }
            }

            ringHead = (++ringHead) % buf.length;
            escaped = c == '\\' && !escaped;
            lastChar = c;
        }
        finish();
    }

    private int setQuoteChar(int quoteChar, int c) {
        if (quoteChar == 0) {
            quoteChar = c;
        } else if (quoteChar == c) {
            quoteChar = 0;
        }
        return quoteChar;
    }

    protected void finish() {
        myData = sb.substring(0, sb.length() - getTagEnd().length() - 1);
    }

    public String toString() {
        return "<" + myData + getTagEnd() + ">";
    }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.