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

Java Open Source » Web Server » jo 
jo » com » tagtraum » framework » markup » Tag.java
/*
License $Id: Tag.java,v 1.6 2003/09/13 04:59:56 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 com.tagtraum.framework.util.UnSyncStringBuffer;

import java.io.IOException;
import java.io.PushbackReader;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.Vector;

/**
 * Parses a Tag.
 *
 * @author   <a href="mailto:hs@tagtraum.com">Hendrik Schreiber</a>
 * @version 1.1beta1 $Id: Tag.java,v 1.6 2003/09/13 04:59:56 hendriks73 Exp $
 */
public class Tag extends RawDataElement implements I_Tag {

    /**
     * Source-Version
     */
    public static String vcid = "$Id: Tag.java,v 1.6 2003/09/13 04:59:56 hendriks73 Exp $";

    protected HashMap myAttributes;
    protected Vector myOrderedAttributeNames;
    protected boolean wellFormed;
    protected boolean attributesRead;
    protected StringTokenizer st;
    protected String myName;


    /**
     * Constructor.
     */
    public Tag(String aTagStart) {
        super(aTagStart);
        wellFormed = true;
        attributesRead = false;
        st = null;
    }

    /**
     * Returns the name of this tag. I.e. P for &lt;p&gt;.
     */
    public String getName() {
        if (st == null) {
            st = new StringTokenizer(myData, "\\\r\n\t\f \"\'=/", true);
        }
        if (myName == null) {
            myName = nextToken();
            // end tags
            if ("/".equals(myName)) {
                String theToken = nextToken();
                if (theToken != null) {
                    myName += theToken;
                }
            }
            // if (myName != null) myName = myName.toUpperCase();
        }
        return myName;
    }

    /**
     * Returns the names of the attributes.
     *
     * @return an enumeration of the attribute names
     */
    public Enumeration getAttributeNames() {
        if (getName() == null) {
            return null;  // read name first.
        }
        readAttributes();       // read attributes
        return myOrderedAttributeNames.elements();
    }

    /**
     * Returns an attribute value of this tag.
     *
     * @param aName key of this value
     */
    public String getAttribute(String aName) {
        if (getName() == null) {
            return null;  // read name first.
        }
        readAttributes();       // read attributes
        return (String) myAttributes.get(aName.toUpperCase());
    }

    public void addAttribute(String key, String value) {
        myAttributes.put(key.toUpperCase(), value);
    }

    /**
     * Indicates whether this tag starts with a '/'
     */
    public boolean isEndTag() {
        return toString().startsWith("</");
    }

    /**
     * Indicates whether this tag ends with a '/'
     */
    public boolean isEmptyTag() {
        return toString().endsWith("/>");
    }

    /**
     * Parses the current element.
     */
    public void parse(PushbackReader aReader) throws IOException {
        int c = 0;
        while (true) {
            c = aReader.read();
            if (c != -1 && c != '>') {
                append((char) c);
            } else {
                break;
            }
        }
        finish();
    }

    /**
     * Check for well-formedness of this tag.
     */
    public boolean isWellFormed() {
        if (getName() == null) {
            return false;
        }
        return readAttributes();
    }

    /**
     * Attempt to read attributes from tag if not already read.
     *
     * @return true if everything was read fine, false otherwise
     */
    protected boolean readAttributes() {
        if (attributesRead) {
            return wellFormed;
        }
        attributesRead = true;
        String key;
        String value;
        String token;
        wellFormed = true;
        myAttributes = new HashMap();
        myOrderedAttributeNames = new Vector();

        ParseLoop:
        while (st.hasMoreTokens()) {
            wellFormed = false;
            key = skipWhiteSpace();
            // isEmptyElement
            if (key == null || "/".equals(key)) {
                wellFormed = true;
                break ParseLoop;
            }
            if (!myOrderedAttributeNames.contains(key)) myOrderedAttributeNames.add(key);
            // ensure '='-sign
            token = skipWhiteSpace();
            if (token == null || "/".equals(key)) {
                break ParseLoop;
            }
            if (!token.equals("=")) {
                break ParseLoop;
            }
            // get value or quotes
            token = skipWhiteSpace();
            if (token == null) {
                break ParseLoop;
            }
            String endToken = null;
            if (token.equals("\"")) {
                endToken = "\"";
                token = nextToken();
            } else if (token.equals("\'")) {
                endToken = "\'";
                token = nextToken();
            }
            if (endToken != null) {
                UnSyncStringBuffer sb = new UnSyncStringBuffer();
                boolean escaped = false;
                while (endToken != null && ((!token.equals(endToken)) || escaped)) {
                    if (token.equals("\\") && !escaped) {
                        escaped = true;
                    } else {
                        sb.append(token);
                        escaped = false;
                    }
                    token = nextToken();
                    if (token == null) {
                        // ends too early
                        break ParseLoop;
                    }
                }
                value = sb.toString();
            } else {
                value = token;
            }
            if (value == null) {
                break ParseLoop;
            }
            addAttribute(key.toUpperCase(), value);
            wellFormed = true;
        }
        return wellFormed;
    }


    /**
     * Skips whitespace.
     */
    protected String skipWhiteSpace() {
        while (st.hasMoreTokens()) {
            String theToken = st.nextToken();
            if (!Character.isWhitespace(theToken.charAt(0))) {
                return theToken;
            }
        }
        return null;
    }

    /**
     * Returns next token.
     */
    protected String nextToken() {
        if (st.hasMoreTokens()) {
            return st.nextToken();
        }
        return null;
    }

    /**
     * String representation.
     */
    public String toString() {
        return "<" + myData + getTagEnd() + ">";
    }

    /**
     * Returns the start characters of this tag.
     */
    public String getTagStart() {
        return myTagStart;
    }

    /**
     * Returns the end characters of this tag.
     */
    public String getTagEnd() {
        return "";
    }


}
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.