jakarta-taglibs-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dmand...@apache.org
Subject cvs commit: jakarta-taglibs/tools/UltraDev/4.0/ctlx/src/org/apache/taglibs/tools/ultradev/ctlx TLDParser.java
Date Wed, 24 Apr 2002 10:11:37 GMT
dmandell    02/04/24 03:11:36

  Modified:    tools/UltraDev/4.0/ctlx/src/org/apache/taglibs/tools/ultradev/ctlx
                        TLDParser.java
  Log:
  Fixed NoClassDefFoundError: org/apache/xerces/parsers/AbstractDOMParser
  Removed dependency on xerces, now use only JAXP API
  Included patch from David Miller to make TLDParser compatible with Orion and
  OC4J containers. Thanks David!
  
  Revision  Changes    Path
  1.6       +518 -497  jakarta-taglibs/tools/UltraDev/4.0/ctlx/src/org/apache/taglibs/tools/ultradev/ctlx/TLDParser.java
  
  Index: TLDParser.java
  ===================================================================
  RCS file: /home/cvs/jakarta-taglibs/tools/UltraDev/4.0/ctlx/src/org/apache/taglibs/tools/ultradev/ctlx/TLDParser.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- TLDParser.java	28 Mar 2002 01:42:44 -0000	1.5
  +++ TLDParser.java	24 Apr 2002 10:11:36 -0000	1.6
  @@ -1,497 +1,518 @@
  -/* ====================================================================
  - * 
  - * The Apache Software License, Version 1.1
  - *
  - * Copyright (c) 1999 The Apache Software Foundation.  All rights 
  - * reserved.
  - *
  - * Redistribution and use in source and binary forms, with or without
  - * modification, are permitted provided that the following conditions
  - * are met:
  - *
  - * 1. Redistributions of source code must retain the above copyright
  - *    notice, this list of conditions and the following disclaimer. 
  - *
  - * 2. 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.
  - *
  - * 3. The end-user documentation included with the redistribution, if
  - *    any, must include the following acknowlegement:  
  - *       "This product includes software developed by the 
  - *        Apache Software Foundation (http://www.apache.org/)."
  - *    Alternately, this acknowlegement may appear in the software itself,
  - *    if and wherever such third-party acknowlegements normally appear.
  - *
  - * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
  - *    Foundation" must not be used to endorse or promote products derived
  - *    from this software without prior written permission. For written 
  - *    permission, please contact apache@apache.org.
  - *
  - * 5. Products derived from this software may not be called "Apache"
  - *    nor may "Apache" appear in their names without prior written
  - *    permission of the Apache Group.
  - *
  - * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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 APACHE SOFTWARE FOUNDATION OR
  - * ITS 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.
  - * ====================================================================
  - *
  - * This software consists of voluntary contributions made by many
  - * individuals on behalf of the Apache Software Foundation.  For more
  - * information on the Apache Software Foundation, please see
  - * <http://www.apache.org/>.
  - */
  -
  -package org.apache.taglibs.tools.ultradev.ctlx;
  -
  - 
  -import java.io.*; import java.util.*;
  -import org.apache.xerces.parsers.*;
  -import org.xml.sax.helpers.*;
  -import org.w3c.dom.*;
  -import org.xml.sax.*;
  -import javax.servlet.http.*;
  -import javax.servlet.*;
  -
  -
  -/* file: TLDParser.java
  - * author: Dan Mandell [dmandell@stanford.edu]
  - * contributors : Julien Carnelos [juliencarnelos@netcourrier.com]
  - * -------------------------------------------
  - * Takes the URL of a TLD file as an argument and produces a text
  - * file containing JavaScript declarations of associative
  - * arrays representing all tags and their attributes in the supplied
  - * TLD. Requires the xerces XML parser available from xml.apache.org
  - *
  - */
  -
  -
  -public class TLDParser extends HttpServlet {
  -
  -	/* Output Modes */
  -	public final int ULTRADEV = 1;
  -	public final int OTHER = 2;
  -	public final int TLD_LIST = 3;
  -	
  -	/* Result Flags */
  -	public final int FAILURE = 0;
  -	public final int SUCCESS = 1;
  -	
  -	/* Private Constants/Defaults */
  -	private final String PATH_TO_TLDS = "tlds";
  -	private final String TLD_SUFFIX = ".tld";
  -	private final String UNDEFINED = "UNDEFINED";
  -	private final String DEFAULT_OUTPUT_FILE = "tagLibData.js";
  -	private final int DEFAULT_OUTPUT_MODE = ULTRADEV;
  -	
  -	/* Private Variables */
  -	private int result = FAILURE; // internal flag. Set to SUCCESS if parsing is successful
  -	private int mode; // defines format of the output
  -	private String outputFilePrefix; // for naming output files (ie: prefix.js, prefix.html)
  -	private PrintWriter out;
  -	private Hashtable tags;
  -
  -	public void init(ServletConfig config) throws ServletException
  -	{
  -		super.init(config);
  -	}
  -
  -	public void doGet(HttpServletRequest req, HttpServletResponse res)
  -					throws ServletException, IOException
  -	{
  -		tags = new Hashtable();
  -		out = new PrintWriter(res.getOutputStream());
  -		String userMode;
  -		File tld;
  -
  -		userMode = req.getParameter("mode").toLowerCase();
  -		outputFilePrefix = req.getParameter("prefix");
  -		String tldpath = getServletConfig().getServletContext().getRealPath(PATH_TO_TLDS);
  -
  -		/* updated the separator to make it compatible with Windows based servers */
  -		tld = new File(tldpath + File.separator + outputFilePrefix + TLD_SUFFIX);
  -
  -               	System.out.println( "tld = " + tld );
  -				
  -		if (userMode.equals("ultradev")) mode = ULTRADEV;
  -		else if (userMode.equals("other")) mode = OTHER;
  -		else if (userMode.equals("tldlist")) mode = TLD_LIST;
  -		
  -		if (mode == TLD_LIST) outputTLDList(req);
  -		else parseTLD(tld);
  -
  -		out.close();
  -	}
  -	
  -
  -
  -/* funtion: parseTLD()
  - * -------------------
  - * Use the Xerces XML parser to create a DOM of the supplied TLD.
  - * Passes the root element of the DOM to the createAttTable method
  - * to create the attribute table.
  - */
  - 
  -	private void parseTLD(File TLD) {
  -		System.out.println("You entered " + TLD);
  -		
  -		try {
  -			DOMParser domParser = (DOMParser) new DOMParser();
  -			domParser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd",false);
  -			domParser.parse(new InputSource((Reader) new FileReader(TLD)));
  -			Document doc = domParser.getDocument();
  -			Element root = doc.getDocumentElement();
  -			System.out.println("Creating attributes table");
  -			createAttTable(root);
  -			writeOutput();
  -			result = SUCCESS;
  -		}
  -		
  -		catch (org.xml.sax.SAXException e) {
  -			e.printStackTrace();
  -		}
  -
  -		catch (java.io.IOException e)
  -		{
  -			e.printStackTrace();
  -		}
  -		
  -	}
  -	
  -	
  -/* method: createAttTable()
  - * ------------------------
  - * Takes the root element in a DOM and enters the tags
  - * into the tags table as an arraylist 
  - *
  - */
  -	private void createAttTable (Element root) {
  -		Node curTag = root.getFirstChild();
  -				
  -		curTag = getNextTag(curTag, "tag");//start with first <tag> node
  -		Tag thetag;
  -		
  -		while (curTag != null) {
  -			System.out.println("Current tag name is: " + getTagName(curTag));
  -			if (curTag.getNodeName().toLowerCase().equals("tag")) {
  -				thetag = analyzeAtts(curTag);
  -				tags.put(getTagName(curTag), thetag); 
  -			}	
  -			curTag = getNextTag(curTag, "tag");
  -		}
  -	}
  -	
  - 	
  - /* method: analyzeAtts()
  -  * ---------------------
  -  * Analyzes each child of the supplied tag. If the child is an attribute,
  -  * the child Node is passed to checkFields to check if the attribute
  -  * should be written.
  -  */
  - 	
  - 	private Tag analyzeAtts(Node tagNode) {
  -  		Node bodyContentNode;
  - 		Node curAtt = getNextTag(tagNode.getFirstChild(), "attribute"); // go to first attribute
  - 		Node curField; // the current attribute field, ie. name,required, etc...
  -   		Node curFieldContent; // whatever content is inside the current field
  -  		String curFieldText; // the text data of the curFieldContent
  -   		String curFieldName; // the name of the current field Node
  -   		String attName = UNDEFINED; // the value of the attribute's "name" field
  - 		String attReq = UNDEFINED; // the value of the attribute's "required" field
  - 		Tag thetag = new Tag();
  -
  -		bodyContentNode = getNextTag(tagNode.getFirstChild(), "bodycontent"); // node containing
value of bodycontent
  -		if (bodyContentNode == null) 
  -			 bodyContentNode = getNextTag(tagNode.getFirstChild(), "body-content"); // support for
JSP 1.2 naming		
  - 		if (bodyContentNode != null) { //set bodyContent based on text contained in body content
node
  - 			String bodyContentText = ((Text)bodyContentNode.getFirstChild()).getData(); 
  - 			thetag.setBodyContent(bodyContentText.toLowerCase().equals("empty") ? false : true);
  - 		}
  -
  - 		System.out.println("Analyzing attributes...");
  -
  - 		while (curAtt != null) {
  - 			curField = curAtt.getFirstChild();
  - 			
  - 			while (curField != null) {	// store relevent attribute fields
  - 				if (curField.hasChildNodes()) {
  - 					curFieldContent = curField.getFirstChild();
  - 					curFieldName = curField.getNodeName().toLowerCase();
  - 					curFieldText = ((Text)curFieldContent).getData();
  - 				
  - 					if (curFieldName != null && curFieldName.equals("name")) {
  - 						attName = curFieldText;
  - 					}
  - 				
  - 					else if (curFieldName != null && curFieldName.equals("required")) {
  - 						attReq = curFieldText;
  - 					}
  - 				}
  - 				curField = curField.getNextSibling();
  -			}
  -			if (attName == null | attReq == null) { 
  -				//tag is malformed; either <name> or <required> does not exist
  -			
  -				System.err.println("Warning - Malformed tag: " + getTagName(tagNode));
  -				System.err.println("<name> and <required> are required fields in TLD");
  -				attName = attReq = UNDEFINED;
  -			}
  -			else if (attName != UNDEFINED && attReq != UNDEFINED){					
  -				// add attName to the tag's atts list
  -				thetag.addAtt(attName);
  -				// if att is required, also add it to the reqAtts list
  -				if (attReq.toLowerCase().equals("true")) {
  -					thetag.reqAtts.add(attName);
  -				}
  -				System.out.println("Attribute = " + attName);
  -				System.out.println("Required = " + attReq);
  -				attName = attReq = UNDEFINED;
  -			}
  -			curAtt = curAtt.getNextSibling();
  -		}
  -		return thetag;
  -	}
  -
  -
  -/* method: getNextTag()
  - * ----------------------
  - * Takes a Node object and returns the subsequent node that the getNodeName()
  - * method returns a string equal (case insensitive) to the supplied tagName.
  - *
  - */
  - 
  - 	private Node getNextTag(Node curField, String tagName) {
  - 		Node nextTag = curField.getNextSibling();
  - 		
  - 		//continue through DOM until next tag matching tagName is found
  - 		while (nextTag != null && 
  - 			!(nextTag.getNodeName().toLowerCase().equals(tagName.toLowerCase()))) 
  - 		{
  - 			nextTag = nextTag.getNextSibling();
  - 		}
  -
  - 		return nextTag;
  - 	}
  -
  -
  -/* method: getTagName()
  - * ----------------------	
  - * Takes a tag in a TLD DOM and returns a string version of the text data
  - * in the tag's <name> field.
  - *
  - */
  -
  -	private String getTagName(Node tag) {
  -		Node nameTag = getNextTag(tag.getFirstChild(), "name").getFirstChild();
  -		return ((Text)nameTag).getData();
  -	}
  - 	
  -
  -/* method: writeOutput()
  - * ---------------------
  - * Writes the out file(s) based on the file output mode.
  - * Currently a placeholder for future output formats.
  - */
  - 
  -	private void writeOutput() {
  -		switch(mode) {
  -			case ULTRADEV:
  -				outputForUltraDev();
  -				break;
  -			default:
  -				outputForUltraDev();
  -				break;
  -		}
  -	}
  -
  -/* method: outputForUltraDev()
  - * ---------------------------
  - * Writes the JavaScript (.js) and HTML (.html) files necessary to
  - * represent the floater to the current directory.
  - */
  - 
  - 	private void outputForUltraDev() {		
  -  		outputTaglibData();
  - 	}
  -
  -	
  -/* method: outputTaglibData()
  - * --------------------------
  - * Outputs the taglib's TLD data, as a series of JavaScript  
  - * associative array declarations, to the HTTP response object.
  - *
  - */
  -
  -	private void outputTaglibData() {
  -		Object[] tagNames = (tags.keySet().toArray());
  -		Arrays.sort(tagNames);
  -		String curName;
  -		Object[] attributes, reqAttributes;
  -		Tag curTag;
  -		
  -		out.println("taglibs[\"" + outputFilePrefix + "\"]=[");	
  -		for (int i = 0; i < tagNames.length; i++) {
  -			curName = (String)tagNames[i];
  -			curTag = ((Tag)tags.get(curName));
  -			out.print("\t[\"" + curName + "\", " + curTag.hasBodyContent() + ", [");
  -
  -			reqAttributes = ((ArrayList)(curTag.getReqAtts())).toArray();
  -			for (int j = 0; j < reqAttributes.length; j++) {
  -				out.print("\"" + reqAttributes[j] + "\"");
  -				if (j < reqAttributes.length - 1) out.print(", ");
  -			}
  -			out.print("], [");
  -			
  -			attributes = ((ArrayList)(curTag.getAtts())).toArray();
  -			for (int k = 0; k < attributes.length; k++) {
  -				out.print("\"" + attributes[k] + "\"");
  -				if (k < attributes.length - 1) out.print(", ");
  -			}
  -
  -			out.print("]");
  -
  -			/* easy to add any new information... */
  -
  -			if ( i >= tagNames.length - 1 ) out.println( "]];");
  -			else
  -				out.println("],");
  -		}		
  -	}
  -	
  -	
  -/* method: outputTLDList()
  - * -----------------------
  - * Sends a tab-delimited list of TLDs in the servlet's TLDs directory 
  - * to the http response out stream.
  - *
  - */
  - 
  -	private void outputTLDList(HttpServletRequest req) {
  -                String tldpath = getServletConfig().getServletContext().getRealPath(PATH_TO_TLDS);
  -		File dir = new File(tldpath);
  -		String[] files = dir.list();
  -		
  -		for (int i = 0; i < files.length; i++) {
  -			out.print(files[i].substring(0,files[i].indexOf(".")));
  -			if (i < files.length - 1) out.print("\t");
  -		}
  -	}
  -	
  -	
  - 
  -/** Public Accessors **/
  -
  -
  -
  -/* method: setOutputPrefix()
  - * -----------------------
  - * Sets the name of the file to be written.
  - *
  - */
  -
  -	public void setOutputPrefix(String prefix) {
  -		outputFilePrefix = prefix.toLowerCase();
  - 	}
  -  	
  -
  -/* method: setOutputMode()
  - * -------------------------
  - * Sets the output mode of the parser. Available options listed at top
  - * under "Output Modes". Modify this method as output modes are added.
  - *
  - */
  -
  -	public void setOutputMode(int outputMode) {
  -		if (outputMode != ULTRADEV &&
  -		    outputMode != OTHER) {
  -		    	System.err.println("Invalid output mode.");
  -		}
  -		
  -		else {
  -			mode = outputMode;
  -		}				
  -	}
  -
  -
  -/* method: getOutputMode()
  - * -----------------------
  - * Returns the parser's output mode.
  - *
  - */
  -
  -	public int getOutputMode() {
  -		return mode;
  -	}
  -	
  -
  -/* method: getResult()
  - * -------------------
  - * Returns the value of the result flag. True if all operations completed
  - * successfully.
  - */
  - 
  -	public int getResult() {
  -		return result;
  -	}
  -	
  -
  -
  -/* Private classes */	
  -	
  -
  -
  -/* class: Tag
  - * ----------
  - * Private wrapper class. Stores an ArrayList of attributes, an ArrayList
  - * of required attributes (kept seperate for performance purposes) and a 
  - * bodyContent boolean, set to true if the tag allows any body content.
  - */
  -
  -	private class Tag {
  -		private ArrayList atts = new ArrayList();
  -		private ArrayList reqAtts = new ArrayList();
  -		private boolean bodyContent;
  -		
  -		public void addAtt (String att) {
  -			if (att != null) {
  -				System.out.println("Adding attribute: " + att);
  -				atts.add(att);
  -			}
  -		}
  -		
  -		public void addReqAtt (String att) {
  -			if (att != null ) {
  -				reqAtts.add(att);
  -			}
  -		}
  -		
  -		public void setBodyContent(boolean bc) {
  -			if (bc == true || bc == false) {
  -				bodyContent = bc;
  -			}
  -		}
  -
  -		public ArrayList getAtts () {
  -			return atts;
  -		}
  -		
  -		public ArrayList getReqAtts () {
  -			return reqAtts;
  -		}
  -		
  -		public boolean hasBodyContent() {
  -			return bodyContent;
  -		}
  -	}
  -}
  +/* ====================================================================
  + * 
  + * The Apache Software License, Version 1.1
  + *
  + * Copyright (c) 1999 The Apache Software Foundation.  All rights 
  + * reserved.
  + *
  + * Redistribution and use in source and binary forms, with or without
  + * modification, are permitted provided that the following conditions
  + * are met:
  + *
  + * 1. Redistributions of source code must retain the above copyright
  + *    notice, this list of conditions and the following disclaimer. 
  + *
  + * 2. 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.
  + *
  + * 3. The end-user documentation included with the redistribution, if
  + *    any, must include the following acknowlegement:  
  + *       "This product includes software developed by the 
  + *        Apache Software Foundation (http://www.apache.org/)."
  + *    Alternately, this acknowlegement may appear in the software itself,
  + *    if and wherever such third-party acknowlegements normally appear.
  + *
  + * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
  + *    Foundation" must not be used to endorse or promote products derived
  + *    from this software without prior written permission. For written 
  + *    permission, please contact apache@apache.org.
  + *
  + * 5. Products derived from this software may not be called "Apache"
  + *    nor may "Apache" appear in their names without prior written
  + *    permission of the Apache Group.
  + *
  + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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 APACHE SOFTWARE FOUNDATION OR
  + * ITS 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.
  + * ====================================================================
  + *
  + * This software consists of voluntary contributions made by many
  + * individuals on behalf of the Apache Software Foundation.  For more
  + * information on the Apache Software Foundation, please see
  + * <http://www.apache.org/>.
  + */
  +
  +package org.apache.taglibs.tools.ultradev.ctlx;
  +
  +import javax.xml.parsers.DocumentBuilder;
  +import javax.xml.parsers.DocumentBuilderFactory;
  +import javax.xml.parsers.FactoryConfigurationError;
  +import javax.xml.parsers.ParserConfigurationException;
  +import javax.servlet.http.HttpServletRequest;
  +import javax.servlet.http.HttpServletResponse;
  +import javax.servlet.http.HttpServlet;
  +import javax.servlet.ServletConfig;
  +import javax.servlet.ServletException;
  +import org.xml.sax.SAXException;
  +import org.xml.sax.SAXParseException;
  +import org.xml.sax.InputSource;
  +import org.w3c.dom.Document;
  +import org.w3c.dom.Element;
  +import org.w3c.dom.Node;
  +import org.w3c.dom.Text;
  +import org.w3c.dom.DOMException;
  +import java.io.File;
  +import java.io.PrintWriter;
  +import java.io.IOException;
  +import java.util.Arrays;
  +import java.util.ArrayList;
  +import java.util.Hashtable;
  +import java.net.URL;
  +
  +
  +/* file: TLDParser.java
  + * author : Dan Mandell [dmandell@stanford.edu]
  + * contributors : Julien Carnelos [juliencarnelos@netcourrier.com]
  + *              : David Miller [dmiller_va@hotmail.com]
  + * -------------------------------------------
  + * Takes the URL of a TLD file as an argument and produces a text
  + * file containing JavaScript declarations of associative
  + * arrays representing all tags and their attributes in the supplied
  + * TLD. Requires the xerces XML parser available from xml.apache.org
  + *
  + */
  +
  +
  +public class TLDParser extends HttpServlet {
  +
  +	/* Output Modes */
  +	public final int ULTRADEV = 1;
  +	public final int OTHER = 2;
  +	public final int TLD_LIST = 3;
  +	
  +	/* Result Flags */
  +	public final int FAILURE = 0;
  +	public final int SUCCESS = 1;
  +	
  +	/* Private Constants/Defaults */
  +	private final String URL_SEP = "/";
  +	private final String PATH_TO_TLDS = URL_SEP + "tlds";
  +	private final String TLD_SUFFIX = ".tld";
  +	private final String UNDEFINED = "UNDEFINED";
  +	private final String DEFAULT_OUTPUT_FILE = "tagLibData.js";
  +	private final int DEFAULT_OUTPUT_MODE = ULTRADEV;
  +	
  +	/* Private Variables */
  +	private int result = FAILURE; // internal flag. Set to SUCCESS if parsing is successful
  +	private int mode; // defines format of the output
  +	private String outputFilePrefix; // for naming output files (ie: prefix.js, prefix.html)
  +	private PrintWriter out;
  +	private Hashtable tags;
  +
  +	public void init(ServletConfig config) throws ServletException
  +	{
  +		super.init(config);
  +	}
  +
  +	public void doGet(HttpServletRequest req, HttpServletResponse res)
  +					throws ServletException, IOException
  +	{
  +		tags = new Hashtable();
  +		out = new PrintWriter(res.getOutputStream());
  +		String userMode;
  +		URL tld = null;
  +		
  +		userMode = req.getParameter("mode").toLowerCase();
  +		outputFilePrefix = req.getParameter("prefix");
  +		String tldPath = PATH_TO_TLDS + URL_SEP + outputFilePrefix + TLD_SUFFIX;
  +
  +		/* updated the separator to make it compatible with Windows based servers */
  +		tld = getServletConfig().getServletContext().getResource(tldPath);
  +
  +            System.out.println( "tld = " + tld );
  +				
  +		if (userMode.equals("ultradev")) mode = ULTRADEV;
  +		else if (userMode.equals("other")) mode = OTHER;
  +		else if (userMode.equals("tldlist")) mode = TLD_LIST;
  +		
  +		if (mode == TLD_LIST) outputTLDList(req);
  +		else parseTLD(tld);
  +
  +		out.close();
  +	}
  +		
  +
  +
  +/* funtion: parseTLD()
  + * -------------------
  + * Use the Xerces XML parser to create a DOM of the supplied TLD.
  + * Passes the root element of the DOM to the createAttTable method
  + * to create the attribute table.
  + */
  + 
  +	private void parseTLD(URL TLD) {
  +		System.out.println("You entered " + TLD);
  +		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  +		factory.setValidating(true);
  +		factory.setNamespaceAware(true);
  +			
  +		try {
  +			DocumentBuilder builder = factory.newDocumentBuilder();
  +			Document doc = builder.parse(new InputSource(TLD.openStream()));
  +			Element root = doc.getDocumentElement();
  +			System.out.println("Creating attributes table");
  +			createAttTable(root);
  +			writeOutput();
  +			result = SUCCESS;
  +		}
  +		
  +		catch (org.xml.sax.SAXException e) {
  +			e.printStackTrace();
  +		}
  +
  +		catch (javax.xml.parsers.ParserConfigurationException e) {
  +			e.printStackTrace();
  +		}
  +
  +		catch (java.io.IOException e)	{
  +			e.printStackTrace();
  +		}		
  +	}
  +	
  +	
  +/* method: createAttTable()
  + * ------------------------
  + * Takes the root element in a DOM and enters the tags
  + * into the tags table as an arraylist 
  + *
  + */
  +	private void createAttTable (Element root) {
  +		Node curTag = root.getFirstChild();
  +				
  +		curTag = getNextTag(curTag, "tag");//start with first <tag> node
  +		Tag thetag;
  +		
  +		while (curTag != null) {
  +			System.out.println("Current tag name is: " + getTagName(curTag));
  +			if (curTag.getNodeName().toLowerCase().equals("tag")) {
  +				thetag = analyzeAtts(curTag);
  +				tags.put(getTagName(curTag), thetag); 
  +			}	
  +			curTag = getNextTag(curTag, "tag");
  +		}
  +	}
  +	
  + 	
  + /* method: analyzeAtts()
  +  * ---------------------
  +  * Analyzes each child of the supplied tag. If the child is an attribute,
  +  * the child Node is passed to checkFields to check if the attribute
  +  * should be written.
  +  */
  + 	
  + 	private Tag analyzeAtts(Node tagNode) {
  +  		Node bodyContentNode;
  + 		Node curAtt = getNextTag(tagNode.getFirstChild(), "attribute"); // go to first attribute
  + 		Node curField; // the current attribute field, ie. name,required, etc...
  +   		Node curFieldContent; // whatever content is inside the current field
  +  		String curFieldText; // the text data of the curFieldContent
  +   		String curFieldName; // the name of the current field Node
  +   		String attName = UNDEFINED; // the value of the attribute's "name" field
  + 		String attReq = UNDEFINED; // the value of the attribute's "required" field
  + 		Tag thetag = new Tag();
  +
  +		bodyContentNode = getNextTag(tagNode.getFirstChild(), "bodycontent"); // node containing
value of bodycontent
  +		if (bodyContentNode == null) 
  +			 bodyContentNode = getNextTag(tagNode.getFirstChild(), "body-content"); // support for
JSP 1.2 naming		
  + 		if (bodyContentNode != null) { //set bodyContent based on text contained in body content
node
  + 			String bodyContentText = ((Text)bodyContentNode.getFirstChild()).getData(); 
  + 			thetag.setBodyContent(bodyContentText.toLowerCase().equals("empty") ? false : true);
  + 		}
  +
  + 		System.out.println("Analyzing attributes...");
  +
  + 		while (curAtt != null) {
  + 			curField = curAtt.getFirstChild();
  + 			
  + 			while (curField != null) {	// store relevent attribute fields
  + 				if (curField.hasChildNodes()) {
  + 					curFieldContent = curField.getFirstChild();
  + 					curFieldName = curField.getNodeName().toLowerCase();
  + 					curFieldText = ((Text)curFieldContent).getData();
  + 				
  + 					if (curFieldName != null && curFieldName.equals("name")) {
  + 						attName = curFieldText;
  + 					}
  + 				
  + 					else if (curFieldName != null && curFieldName.equals("required")) {
  + 						attReq = curFieldText;
  + 					}
  + 				}
  + 				curField = curField.getNextSibling();
  +			}
  +			if (attName == null | attReq == null) { 
  +				//tag is malformed; either <name> or <required> does not exist
  +			
  +				System.err.println("Warning - Malformed tag: " + getTagName(tagNode));
  +				System.err.println("<name> and <required> are required fields in TLD");
  +				attName = attReq = UNDEFINED;
  +			}
  +			else if (attName != UNDEFINED && attReq != UNDEFINED){					
  +				// add attName to the tag's atts list
  +				thetag.addAtt(attName);
  +				// if att is required, also add it to the reqAtts list
  +				if (attReq.toLowerCase().equals("true")) {
  +					thetag.reqAtts.add(attName);
  +				}
  +				System.out.println("Attribute = " + attName);
  +				System.out.println("Required = " + attReq);
  +				attName = attReq = UNDEFINED;
  +			}
  +			curAtt = curAtt.getNextSibling();
  +		}
  +		return thetag;
  +	}
  +
  +
  +/* method: getNextTag()
  + * ----------------------
  + * Takes a Node object and returns the subsequent node that the getNodeName()
  + * method returns a string equal (case insensitive) to the supplied tagName.
  + *
  + */
  + 
  + 	private Node getNextTag(Node curField, String tagName) {
  + 		Node nextTag = curField.getNextSibling();
  + 		
  + 		//continue through DOM until next tag matching tagName is found
  + 		while (nextTag != null && 
  + 			!(nextTag.getNodeName().toLowerCase().equals(tagName.toLowerCase()))) 
  + 		{
  + 			nextTag = nextTag.getNextSibling();
  + 		}
  +
  + 		return nextTag;
  + 	}
  +
  +
  +/* method: getTagName()
  + * ----------------------	
  + * Takes a tag in a TLD DOM and returns a string version of the text data
  + * in the tag's <name> field.
  + *
  + */
  +
  +	private String getTagName(Node tag) {
  +		Node nameTag = getNextTag(tag.getFirstChild(), "name").getFirstChild();
  +		return ((Text)nameTag).getData();
  +	}
  + 	
  +
  +/* method: writeOutput()
  + * ---------------------
  + * Writes the out file(s) based on the file output mode.
  + * Currently a placeholder for future output formats.
  + */
  + 
  +	private void writeOutput() {
  +		switch(mode) {
  +			case ULTRADEV:
  +				outputForUltraDev();
  +				break;
  +			default:
  +				outputForUltraDev();
  +				break;
  +		}
  +	}
  +
  +/* method: outputForUltraDev()
  + * ---------------------------
  + * Writes the JavaScript (.js) and HTML (.html) files necessary to
  + * represent the floater to the current directory.
  + */
  + 
  + 	private void outputForUltraDev() {		
  +  		outputTaglibData();
  + 	}
  +
  +	
  +/* method: outputTaglibData()
  + * --------------------------
  + * Outputs the taglib's TLD data, as a series of JavaScript  
  + * associative array declarations, to the HTTP response object.
  + *
  + */
  +
  +	private void outputTaglibData() {
  +		Object[] tagNames = (tags.keySet().toArray());
  +		Arrays.sort(tagNames);
  +		String curName;
  +		Object[] attributes, reqAttributes;
  +		Tag curTag;
  +		
  +		out.println("taglibs[\"" + outputFilePrefix + "\"]=[");	
  +		for (int i = 0; i < tagNames.length; i++) {
  +			curName = (String)tagNames[i];
  +			curTag = ((Tag)tags.get(curName));
  +			out.print("\t[\"" + curName + "\", " + curTag.hasBodyContent() + ", [");
  +
  +			reqAttributes = ((ArrayList)(curTag.getReqAtts())).toArray();
  +			for (int j = 0; j < reqAttributes.length; j++) {
  +				out.print("\"" + reqAttributes[j] + "\"");
  +				if (j < reqAttributes.length - 1) out.print(", ");
  +			}
  +			out.print("], [");
  +			
  +			attributes = ((ArrayList)(curTag.getAtts())).toArray();
  +			for (int k = 0; k < attributes.length; k++) {
  +				out.print("\"" + attributes[k] + "\"");
  +				if (k < attributes.length - 1) out.print(", ");
  +			}
  +
  +			out.print("]");
  +
  +			/* easy to add any new information... */
  +
  +			if ( i >= tagNames.length - 1 ) out.println( "]];");
  +			else
  +				out.println("],");
  +		}		
  +	}
  +	
  +	
  +/* method: outputTLDList()
  + * -----------------------
  + * Sends a tab-delimited list of TLDs in the servlet's TLDs directory 
  + * to the http response out stream.
  + *
  + */
  + 
  +	private void outputTLDList(HttpServletRequest req) {
  +                String tldpath = getServletConfig().getServletContext().getRealPath(PATH_TO_TLDS);
  +		File dir = new File(tldpath);
  +		String[] files = dir.list();
  +		
  +		for (int i = 0; i < files.length; i++) {
  +			out.print(files[i].substring(0,files[i].indexOf(".")));
  +			if (i < files.length - 1) out.print("\t");
  +		}
  +	}
  +	
  +	
  + 
  +/** Public Accessors **/
  +
  +
  +
  +/* method: setOutputPrefix()
  + * -----------------------
  + * Sets the name of the file to be written.
  + *
  + */
  +
  +	public void setOutputPrefix(String prefix) {
  +		outputFilePrefix = prefix.toLowerCase();
  + 	}
  +  	
  +
  +/* method: setOutputMode()
  + * -------------------------
  + * Sets the output mode of the parser. Available options listed at top
  + * under "Output Modes". Modify this method as output modes are added.
  + *
  + */
  +
  +	public void setOutputMode(int outputMode) {
  +		if (outputMode != ULTRADEV &&
  +		    outputMode != OTHER) {
  +		    	System.err.println("Invalid output mode.");
  +		}
  +		
  +		else {
  +			mode = outputMode;
  +		}				
  +	}
  +
  +
  +/* method: getOutputMode()
  + * -----------------------
  + * Returns the parser's output mode.
  + *
  + */
  +
  +	public int getOutputMode() {
  +		return mode;
  +	}
  +	
  +
  +/* method: getResult()
  + * -------------------
  + * Returns the value of the result flag. True if all operations completed
  + * successfully.
  + */
  + 
  +	public int getResult() {
  +		return result;
  +	}
  +	
  +
  +
  +/* Private classes */	
  +	
  +
  +
  +/* class: Tag
  + * ----------
  + * Private wrapper class. Stores an ArrayList of attributes, an ArrayList
  + * of required attributes (kept seperate for performance purposes) and a 
  + * bodyContent boolean, set to true if the tag allows any body content.
  + */
  +
  +	private class Tag {
  +		private ArrayList atts = new ArrayList();
  +		private ArrayList reqAtts = new ArrayList();
  +		private boolean bodyContent;
  +		
  +		public void addAtt (String att) {
  +			if (att != null) {
  +				System.out.println("Adding attribute: " + att);
  +				atts.add(att);
  +			}
  +		}
  +		
  +		public void addReqAtt (String att) {
  +			if (att != null ) {
  +				reqAtts.add(att);
  +			}
  +		}
  +		
  +		public void setBodyContent(boolean bc) {
  +			if (bc == true || bc == false) {
  +				bodyContent = bc;
  +			}
  +		}
  +
  +		public ArrayList getAtts () {
  +			return atts;
  +		}
  +		
  +		public ArrayList getReqAtts () {
  +			return reqAtts;
  +		}
  +		
  +		public boolean hasBodyContent() {
  +			return bodyContent;
  +		}
  +	}
  +}
  
  
  

--
To unsubscribe, e-mail:   <mailto:taglibs-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:taglibs-dev-help@jakarta.apache.org>


Mime
View raw message