User: olegnitz
Date: 02/01/16 17:23:24
Added: jetty/src/main/org/mortbay/xml XmlConfiguration.java
XmlParser.java configure_1_0.dtd configure_1_1.dtd
Log:
Jetty source files are in JBoss CVS now.
Run "merge-mortbay-cvs" target to update them from Jetty CVS.
Modified Jetty files (if such need will appear) must be explicitely listed in
build.xml
Revision Changes Path
1.1 contrib/jetty/src/main/org/mortbay/xml/XmlConfiguration.java
Index: XmlConfiguration.java
===================================================================
// ========================================================================
// Copyright (c) 1999 Mort Bay Consulting (Australia) Pty. Ltd.
// $Id: XmlConfiguration.java,v 1.1 2002/01/17 01:23:23 olegnitz Exp $
// ========================================================================
package org.mortbay.xml;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.Map;
import org.mortbay.util.Code;
import org.mortbay.util.InetAddrPort;
import org.mortbay.util.Primitive;
import org.mortbay.util.Resource;
import org.xml.sax.SAXException;
/* ------------------------------------------------------------ */
/** Configure Objects from XML.
* This class reads an XML file conforming to the configure.dtd DTD
* and uses it to configure and object by calling set, put or other
* methods on the object.
*
* @see configure.dtd
* @version $Id: XmlConfiguration.java,v 1.1 2002/01/17 01:23:23 olegnitz Exp $
* @author Greg Wilkins (gregw)
*/
public class XmlConfiguration
{
private static Class[] __primitives =
{
Boolean.TYPE, Character.TYPE, Byte.TYPE, Short.TYPE, Integer.TYPE,
Long.TYPE, Float.TYPE, Double.TYPE, Void.TYPE
};
private static Class[] __primitiveHolders =
{
Boolean.class, Character.class, Byte.class, Short.class, Integer.class,
Long.class, Float.class, Double.class, Void.class
};
/* ------------------------------------------------------------ */
private static XmlParser __parser;
XmlParser.Node _config;
/* ------------------------------------------------------------ */
/** Constructor.
* Reads the XML configuration file.
* @param configuration
*/
public XmlConfiguration(URL configuration)
throws SAXException, IOException
{
synchronized(this.getClass())
{
if (__parser==null)
{
__parser = new XmlParser();
Resource config11Resource=Resource.newSystemResource
("org/mortbay/xml/configure_1_1.dtd");
__parser.redirectEntity
("configure.dtd",config11Resource);
__parser.redirectEntity
("configure_1_1.dtd",
config11Resource);
__parser.redirectEntity
("http://jetty.mortbay.org/configure_1_1.dtd",
config11Resource);
__parser.redirectEntity
("-//Mort Bay Consulting//DTD Configure 1.1//EN",
config11Resource);
Resource config10Resource=Resource.newSystemResource
("org/mortbay/xml/configure_1_1.dtd");
__parser.redirectEntity
("configure_1_0.dtd",
config10Resource);
__parser.redirectEntity
("http://jetty.mortbay.org/configure_1_0.dtd",
config10Resource);
__parser.redirectEntity
("-//Mort Bay Consulting//DTD Configure 1.0//EN",
config10Resource);
}
}
_config = __parser.parse(configuration.toString());
}
/* ------------------------------------------------------------ */
/** Configure an object.
* If the object is of the approprate class, the XML configuration
* script is applied to the object.
* @param obj The object to be configured.
* @exception ClassNotFoundException
* @exception NoSuchMethodException
* @exception InvocationTargetException
*/
public void configure(Object obj)
throws ClassNotFoundException,
NoSuchMethodException,
InvocationTargetException,
IllegalAccessException
{
//Check the class of the object
Class oClass = nodeClass(_config);
if (!oClass.isInstance(obj))
throw new IllegalArgumentException("Object is not of type "+oClass);
configure(obj,_config,0);
}
/* ------------------------------------------------------------ */
/** Create a new object and configure it.
* A new object is created and configured.
* @return The newly created configured object.
* @exception ClassNotFoundException
* @exception NoSuchMethodException
* @exception InvocationTargetException
* @exception InstantiationException
* @exception IllegalAccessException
*/
public Object newInstance()
throws ClassNotFoundException,
NoSuchMethodException,
InvocationTargetException,
InstantiationException,
IllegalAccessException
{
Class oClass = nodeClass(_config);
Object obj = oClass.newInstance();
configure(obj,_config,0);
return obj;
}
/* ------------------------------------------------------------ */
private Class nodeClass(XmlParser.Node node)
throws ClassNotFoundException
{
String className=node.getAttribute("class");
if (className==null)
return null;
return Class.forName(className);
}
/* ------------------------------------------------------------ */
/* Recursive configuration step.
* This method applies the remaining Set, Put and Call elements
* to the current object.
* @param obj
* @param cfg
* @param i
* @exception ClassNotFoundException
* @exception NoSuchMethodException
* @exception InvocationTargetException
*/
private void configure(Object obj,XmlParser.Node cfg,int i)
throws ClassNotFoundException,
NoSuchMethodException,
InvocationTargetException,
IllegalAccessException
{
for(;i<cfg.size();i++)
{
Object o = cfg.get(i);
if (o instanceof String)
continue;
XmlParser.Node node = (XmlParser.Node)o;
String tag=node.getTag();
if("Set".equals(tag))
set(obj,node);
else if("Put".equals(tag))
put(obj,node);
else if("Call".equals(tag))
call(obj,node);
else if("Get".equals(tag))
get(obj,node);
else
throw new IllegalStateException("Unknown tag: "+tag);
}
}
/* ------------------------------------------------------------ */
/* Call a set method.
* This method makes a best effort to find a matching set method.
* The type of the value is used to find a suitable set method by
* 1. Trying for a trivial type match.
* 2. Looking for a native type match.
* 3. Trying all correctly named methods for an auto conversion.
* 4. Attempting to construct a suitable value from original value.
* @param obj
* @param node
*/
private void set(Object obj,XmlParser.Node node)
throws ClassNotFoundException,
NoSuchMethodException,
InvocationTargetException,
IllegalAccessException
{
String attr=node.getAttribute("name");
attr=attr.substring(0,1).toUpperCase()+attr.substring(1);
String name = "set"+attr;
Object value= value(obj,node);
Object[] arg={value};
Class oClass = nodeClass(node);
if (oClass!=null)
obj=null;
else
oClass = obj.getClass();
Class[] vClass = {Object.class};
if (value!=null)
vClass[0]=value.getClass();
Code.debug(obj,".",name,"(",vClass[0]," ",value,")");
// Try for trivial match
try{
Method set = oClass.getMethod(name,vClass);
set.invoke(obj,arg);
return;
}
catch(IllegalArgumentException e)
{if(Code.verbose(Integer.MAX_VALUE))Code.ignore(e);}
catch(IllegalAccessException e)
{if(Code.verbose(Integer.MAX_VALUE))Code.ignore(e);}
catch(NoSuchMethodException e)
{if(Code.verbose(Integer.MAX_VALUE))Code.ignore(e);}
// Try for native match
try{
Field type = vClass[0].getField("TYPE");
vClass[0]=(Class)type.get(null);
Method set = oClass.getMethod(name,vClass);
set.invoke(obj,arg);
return;
}
catch(NoSuchFieldException e)
{if(Code.verbose(Integer.MAX_VALUE))Code.ignore(e);}
catch(IllegalArgumentException e)
{if(Code.verbose(Integer.MAX_VALUE))Code.ignore(e);}
catch(IllegalAccessException e)
{if(Code.verbose(Integer.MAX_VALUE))Code.ignore(e);}
catch(NoSuchMethodException e)
{if(Code.verbose(Integer.MAX_VALUE))Code.ignore(e);}
// Search for a match by trying all the set methods
Method[] sets = oClass.getMethods();
Method set=null;
for (int s=0;sets!=null && s<sets.length;s++)
{
if (name.equals(sets[s].getName()) &&
sets[s].getParameterTypes().length==1)
{
// lets try it
try
{
set=sets[s];
sets[s].invoke(obj,arg);
return;
}
catch(IllegalArgumentException
e){if(Code.verbose(Integer.MAX_VALUE))Code.ignore(e);}
catch(IllegalAccessException
e){if(Code.verbose(Integer.MAX_VALUE))Code.ignore(e);}
}
}
// Try converting the arg to the last set found.
if (set!=null)
{
try
{
Class sClass=set.getParameterTypes()[0];
if (sClass.isPrimitive())
{
for (int t=0;t<__primitives.length;t++)
{
if (sClass.equals(__primitives[t]))
{
sClass=__primitiveHolders[t];
break;
}
}
}
Constructor cons = sClass.getConstructor(vClass);
arg[0]=cons.newInstance(arg);
set.invoke(obj,arg);
return;
}
catch(NoSuchMethodException e)
{if(Code.verbose(Integer.MAX_VALUE))Code.ignore(e);}
catch(IllegalAccessException e)
{if(Code.verbose(Integer.MAX_VALUE))Code.ignore(e);}
catch(InstantiationException e)
{if(Code.verbose(Integer.MAX_VALUE))Code.ignore(e);}
}
// No Joy
throw new NoSuchMethodException(oClass+"."+name+"("+vClass[0]+")");
}
/* ------------------------------------------------------------ */
/* Call a put method.
*
* @param obj
* @param node
*/
private void put(Object obj,XmlParser.Node node)
throws NoSuchMethodException,
ClassNotFoundException,
InvocationTargetException,
IllegalAccessException
{
if (!(obj instanceof Map))
throw new IllegalArgumentException("Object for put is not a Map: "+
obj);
Map map = (Map) obj;
String name = node.getAttribute("name");
Object value= value(obj,node);
map.put(name,value);
Code.debug(obj+".put("+name+","+value+")");
}
/* ------------------------------------------------------------ */
/* Call a get method.
* Any object returned from the call is passed to the configure
* method to consume the remaining elements.
* @param obj
* @param node
* @return
* @exception NoSuchMethodException
* @exception ClassNotFoundException
* @exception InvocationTargetException
*/
private Object get(Object obj,XmlParser.Node node)
throws NoSuchMethodException,
ClassNotFoundException,
InvocationTargetException,
IllegalAccessException
{
Class oClass = nodeClass(node);
if (oClass!=null)
obj=null;
else
oClass = obj.getClass();
String name=node.getAttribute("name");
name="get"+name.substring(0,1).toUpperCase()+name.substring(1);
Code.debug("call ",name);
Method method = oClass.getMethod(name,null);
Object n=null;
n=method.invoke(obj,null);
configure(n,node,0);
return n;
}
/* ------------------------------------------------------------ */
/* Call a method.
* A method is selected by trying all methods with matching
* names and number of arguments.
* Any object returned from the call is passed to the configure
* method to consume the remaining elements.
* @param obj
* @param node
* @return
* @exception NoSuchMethodException
* @exception ClassNotFoundException
* @exception InvocationTargetException
*/
private Object call(Object obj,XmlParser.Node node)
throws NoSuchMethodException,
ClassNotFoundException,
InvocationTargetException,
IllegalAccessException
{
Class oClass = nodeClass(node);
if (oClass!=null)
obj=null;
else
oClass = obj.getClass();
int size=0;
int argi=node.size();
for (int i=0;i<node.size();i++)
{
Object o = node.get(i);
if (o instanceof String)
continue;
if(!((XmlParser.Node)o).getTag().equals("Arg"))
{
argi=i;
break;
}
size++;
}
Object[] arg = new Object[size];
for (int i=0,j=0;j<size;i++)
{
Object o = node.get(i);
if (o instanceof String)
continue;
arg[j++]=value(obj,(XmlParser.Node)o);
}
String method=node.getAttribute("name");
Code.debug("call ",method);
// Lets just try all methods for now
Method[] methods = oClass.getMethods();
for (int c=0;methods!=null && c<methods.length;c++)
{
if (!methods[c].getName().equals(method))
continue;
if (methods[c].getParameterTypes().length!=size)
continue;
if (Modifier.isStatic(methods[c].getModifiers())!=(obj==null))
continue;
Object n=null;
boolean called=false;
try{n=methods[c].invoke(obj,arg);called=true;}
catch(IllegalAccessException e)
{if(Code.verbose(Integer.MAX_VALUE))Code.ignore(e);}
catch(IllegalArgumentException e)
{if(Code.verbose(Integer.MAX_VALUE))Code.ignore(e);}
if (called)
{
configure(n,node,argi);
return n;
}
}
throw new IllegalStateException("No Method: "+node+" on "+oClass);
}
/* ------------------------------------------------------------ */
/* Create a new value object.
*
* @param obj
* @param node
* @return
* @exception NoSuchMethodException
* @exception ClassNotFoundException
* @exception InvocationTargetException
*/
private Object newObj(Object obj,XmlParser.Node node)
throws NoSuchMethodException,
ClassNotFoundException,
InvocationTargetException,
IllegalAccessException
{
Class oClass = nodeClass(node);
int size=0;
int argi=node.size();
for (int i=0;i<node.size();i++)
{
Object o = node.get(i);
if (o instanceof String)
continue;
if(!((XmlParser.Node)o).getTag().equals("Arg"))
{
argi=i;
break;
}
size++;
}
Object[] arg = new Object[size];
for (int i=0,j=0;j<size;i++)
{
Object o = node.get(i);
if (o instanceof String)
continue;
arg[j++]=value(obj,(XmlParser.Node)o);
}
Code.debug("new ",oClass);
// Lets just try all constructors for now
Constructor[] constructors = oClass.getConstructors();
for (int c=0;constructors!=null && c<constructors.length;c++)
{
if (constructors[c].getParameterTypes().length!=size)
continue;
Object n=null;
boolean called=false;
try{
n=constructors[c].newInstance(arg);
called=true;
}
catch(IllegalAccessException e)
{if(Code.verbose(Integer.MAX_VALUE))Code.ignore(e);}
catch(InstantiationException e)
{if(Code.verbose(Integer.MAX_VALUE))Code.ignore(e);}
catch(IllegalArgumentException e)
{if(Code.verbose(Integer.MAX_VALUE))Code.ignore(e);}
if(called)
{
configure(n,node,argi);
return n;
}
}
throw new IllegalStateException("No Constructor: "+node+" on "+obj);
}
/* ------------------------------------------------------------ */
/* Get the value of an element.
* If no value type is specified, then white space is trimmed out of the
* value. If it contains multiple value elements they are added as
* strings before being converted to any specified type.
* @param node
*/
private Object value(Object obj,XmlParser.Node node)
throws NoSuchMethodException,
ClassNotFoundException,
InvocationTargetException,
IllegalAccessException
{
// Get the type
String type = node.getAttribute("type");
// handle trivial case
if (node.size()==0)
{
if ("String".equals(type))
return "";
return null;
}
// Trim values
int first=0;
int last=node.size()-1;
// Handle default trim type
if (type==null || !"String".equals(type))
{
// Skip leading white
Object item=null;
while(first<=last )
{
item=node.get(first);
if (!(item instanceof String))
break;
item=((String)item).trim();
if (((String)item).length()>0)
break;
first++;
}
// Skip trailing white
while(first<last)
{
item=node.get(last);
if (!(item instanceof String))
break;
item=((String)item).trim();
if (((String)item).length()>0)
break;
last--;
}
// All white, so return null
if (first>last)
return null;
}
Object value=null;
if (first==last)
// Single Item value
value=itemValue(obj,node.get(first));
else
{
// Get the multiple items as a single string
StringBuffer buf = new StringBuffer();
synchronized(buf)
{
for (int i=first;i<=last;i++)
{
Object item = node.get(i);
buf.append(itemValue(obj,item));
}
value=buf.toString();
}
}
// Untyped or unknown
if (value==null )
{
if ("String".equals(type))
return "";
return null;
}
// Try to type the object
if (type==null)
{
if (value !=null && value instanceof String)
return ((String)value).trim();
return value;
}
if ("String".equals(type) || "java.lang.String".equals(type))
return value.toString();
Class pClass = Primitive.fromName(type);
if (pClass!=null)
return Primitive.valueOf(pClass,value.toString());
if ("URL".equals(type) || "java.net.URL".equals(type))
{
if (value instanceof URL)
return value;
try{return new URL(value.toString());}
catch(MalformedURLException e)
{throw new InvocationTargetException(e);}
}
if ("InetAddress".equals(type)|| "java.net.InetAddress".equals(type))
{
if (value instanceof InetAddress)
return value;
try {return InetAddress.getByName(value.toString());}
catch(UnknownHostException e)
{throw new InvocationTargetException(e);}
}
if ("InetAddrPort".equals(type) ||
"org.mortbay.util.InetAddrPort".equals(type))
{
if (value instanceof InetAddrPort)
return value;
try{return new InetAddrPort(value.toString());}
catch(UnknownHostException e)
{throw new InvocationTargetException(e);}
}
throw new IllegalStateException("Unknown type "+type);
}
/* ------------------------------------------------------------ */
/* Get the value of a single element.
* @param obj
* @param item
* @return
* @exception ClassNotFoundException
*/
private Object itemValue(Object obj, Object item)
throws NoSuchMethodException,
ClassNotFoundException,
InvocationTargetException,
IllegalAccessException
{
// String value
if (item instanceof String)
return item;
XmlParser.Node node=(XmlParser.Node)item;
String tag=node.getTag();
if ("Call".equals(tag))
return call(obj,node);
if ("Get".equals(tag))
return get(obj,node);
if ("New".equals(tag))
return newObj(obj,node);
if ("SystemProperty".equals(tag))
{
String name=node.getAttribute("name");
String defaultValue=node.getAttribute("default");
return System.getProperty(name, defaultValue);
}
Code.warning("Unknown value tag: "+node,new Throwable());
return null;
}
/* ------------------------------------------------------------ */
/* ------------------------------------------------------------ */
/* ------------------------------------------------------------ */
public static void main(String[] arg)
{
try
{
for (int i=0;i<arg.length;i++)
new XmlConfiguration
(Resource.newResource(arg[i]).getURL()).newInstance();
}
catch (Exception e)
{
Code.warning(e);
}
}
}
1.1 contrib/jetty/src/main/org/mortbay/xml/XmlParser.java
Index: XmlParser.java
===================================================================
// ========================================================================
// Copyright (c) 1999 Mort Bay Consulting (Australia) Pty. Ltd.
// $Id: XmlParser.java,v 1.1 2002/01/17 01:23:23 olegnitz Exp $
// ========================================================================
package org.mortbay.xml;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Stack;
import java.util.Vector;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.mortbay.util.Code;
import org.mortbay.util.Resource;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
/*--------------------------------------------------------------*/
/** XML Parser wrapper.
* This class wraps any standard JAXP1.1 parser with convieniant error and
* entity handlers and a mini dom-like document tree.
*
* @version $Id: XmlParser.java,v 1.1 2002/01/17 01:23:23 olegnitz Exp $
* @author Greg Wilkins (gregw)
*/
public class XmlParser
{
private Map _redirectMap = new HashMap();
private SAXParser _parser;
private Map _observerMap;
private Stack _observers = new Stack();
/* ------------------------------------------------------------ */
/** Constructor.
* @exception ParserConfigurationException
*/
public XmlParser()
{
try
{
SAXParserFactory factory = SAXParserFactory.newInstance();
_parser = factory.newSAXParser();
}
catch(Exception e)
{
Code.warning(e);
throw new Error(e.toString());
}
}
/* ------------------------------------------------------------ */
/**
* @param name
* @param local
*/
public synchronized void redirectEntity(String name,Resource entity)
{
if (entity!=null)
_redirectMap.put(name,entity);
}
/* ------------------------------------------------------------ */
/** Add a ContentHandler.
* Add an additional content handler that is triggered on a tag
* name. SAX events are passed to the ContentHandler provided from
* a matching start element to the corresponding end element.
* Only a single content handler can be registered against each tag.
* @param trigger Tag local or q name.
* @param observer SAX ContentHandler
*/
public synchronized void addContentHandler(String trigger,
ContentHandler observer)
{
if (_observerMap==null)
_observerMap=new HashMap();
_observerMap.put(trigger,observer);
}
/* ------------------------------------------------------------ */
/** Parse URL.
* @param url
* @return
* @exception IOException
* @exception SAXException
*/
public synchronized Node parse(String url)
throws IOException,SAXException
{
Handler handler= new Handler();
XMLReader reader = _parser.getXMLReader();
reader.setContentHandler(handler);
reader.setErrorHandler(handler);
reader.setEntityResolver(handler);
_parser.parse(url, handler);
if (handler._error!=null)
throw handler._error;
Node doc=(Node)handler._top.get(0);
handler.clear();
return doc;
}
/* ------------------------------------------------------------ */
/** Parse File.
* @param file
* @return
* @exception IOException
* @exception SAXException
*/
public synchronized Node parse(File file)
throws IOException,SAXException
{
return parse(file.toURL().toString());
}
/* ------------------------------------------------------------ */
/** Parse InputStream.
* @param url
* @return
* @exception IOException
* @exception SAXException
*/
public synchronized Node parse(InputStream in)
throws IOException,SAXException
{
Handler handler= new Handler();
XMLReader reader = _parser.getXMLReader();
reader.setContentHandler(handler);
reader.setErrorHandler(handler);
reader.setEntityResolver(handler);
_parser.parse(new InputSource(in), handler);
if (handler._error!=null)
throw handler._error;
Node doc=(Node)handler._top.get(0);
handler.clear();
return doc;
}
/* ------------------------------------------------------------ */
/* ------------------------------------------------------------ */
private class Handler extends DefaultHandler
{
Node _top = new Node(null,null,null);
SAXParseException _error;
private Node _context = _top;
/* ------------------------------------------------------------ */
void clear()
{
_top=null;
_error=null;
_context=null;
}
/* ------------------------------------------------------------ */
public void startElement (String uri, String localName, String qName,
Attributes attrs)
throws SAXException
{
String name=(uri==null || uri.equals(""))?qName:localName;
Node node= new Node(_context,name,attrs);
_context.add(node);
_context=node;
ContentHandler observer=null;
if (_observerMap!=null)
observer=(ContentHandler) _observerMap.get(name);
_observers.push(observer);
for(int i=0;i<_observers.size();i++)
if (_observers.get(i)!=null)
((ContentHandler)_observers.get(i))
.startElement(uri,localName,qName,attrs);
}
/* ------------------------------------------------------------ */
public void endElement (String uri, String localName, String qName)
throws SAXException
{
_context=_context._parent;
for(int i=0;i<_observers.size();i++)
if (_observers.get(i)!=null)
((ContentHandler)_observers.get(i))
.endElement(uri,localName,qName);
_observers.pop();
}
/* ------------------------------------------------------------ */
public void ignorableWhitespace (char buf [], int offset, int len)
throws SAXException
{
// XXX - for testing
// characters(buf,offset,len);
for(int i=0;i<_observers.size();i++)
if (_observers.get(i)!=null)
((ContentHandler)_observers.get(i))
.ignorableWhitespace (buf,offset,len);
}
/* ------------------------------------------------------------ */
public void characters (char buf [], int offset, int len)
throws SAXException
{
_context.add(new String(buf,offset,len));
for(int i=0;i<_observers.size();i++)
if (_observers.get(i)!=null)
((ContentHandler)_observers.get(i))
. characters(buf,offset,len);
}
/* ------------------------------------------------------------ */
public void warning(SAXParseException ex)
{
Code.debug(ex);
Code.warning("WARNING@"+getLocationString(ex)+
" : "+ex.toString());
}
/* ------------------------------------------------------------ */
public void error(SAXParseException ex)
throws SAXException
{
// Save error and continue to report other errors
if(_error==null)
_error=ex;
Code.debug(ex);
Code.warning("ERROR@"+getLocationString(ex)+
" : "+ex.toString());
}
/* ------------------------------------------------------------ */
public void fatalError(SAXParseException ex)
throws SAXException
{
_error=ex;
Code.debug(ex);
Code.warning("FATAL@"+getLocationString(ex)+
" : "+ex.toString());
throw ex;
}
/* ------------------------------------------------------------ */
private String getLocationString(SAXParseException ex)
{
return ex.getSystemId()+
" line:"+ex.getLineNumber()+
" col:"+ex.getColumnNumber();
}
/* ------------------------------------------------------------ */
public InputSource resolveEntity
(String pid, String sid)
{
Resource resource=null;
if(pid!=null)
resource = (Resource)_redirectMap.get(pid);
if(resource==null)
resource = (Resource)_redirectMap.get(sid);
if (resource==null)
{
String dtd = sid;
if (dtd.lastIndexOf('/')>=0)
dtd=dtd.substring(dtd.lastIndexOf('/')+1);
resource = (Resource)_redirectMap.get(dtd);
}
if (resource!=null && resource.exists())
{
try
{
InputStream in= resource.getInputStream();
Code.debug("Redirected entity ",
sid," --> ",resource);
return new InputSource(in);
}
catch(IOException e){Code.ignore(e);}
}
return null;
}
}
/* ------------------------------------------------------------ */
/* ------------------------------------------------------------ */
/** XML Attribute.
*/
public static class Attribute
{
private String _name;
private String _value;
Attribute(String n,String v)
{
_name=n;
_value=v;
}
public String getName() {return _name;}
public String getValue() {return _value;}
}
/* ------------------------------------------------------------ */
/* ------------------------------------------------------------ */
/** XML Node.
* Represents an XML element with optional attributes and
* ordered content.
*/
public static class Node extends AbstractList
{
Node _parent;
private ArrayList _list;
private String _tag;
private Attribute[] _attrs;
private boolean _lastString=false;
/* ------------------------------------------------------------ */
Node(Node parent,String tag, Attributes attrs)
{
_parent=parent;
_tag=tag;
if (attrs!=null)
{
_attrs=new Attribute[attrs.getLength()];
for (int i = 0; i <attrs.getLength(); i++)
{
String name = attrs.getLocalName(i);
if ( name==null || name.equals("") )
name = attrs.getQName(i);
_attrs[i]=new Attribute(name,
attrs.getValue(i));
}
}
}
/* ------------------------------------------------------------ */
public Node getParent()
{
return _parent;
}
/* ------------------------------------------------------------ */
public String getTag()
{
return _tag;
}
/* ------------------------------------------------------------ */
/** Get an array of element attributes.
*/
public Attribute[] getAttributes()
{
return _attrs;
}
/* ------------------------------------------------------------ */
/** Get an element attribute.
* @return attribute or null.
*/
public String getAttribute(String name)
{
return getAttribute(name,null);
}
/* ------------------------------------------------------------ */
/** Get an element attribute.
* @return attribute or null.
*/
public String getAttribute(String name, String dft)
{
if (_attrs==null || name==null)
return dft;
for (int i=0;i<_attrs.length;i++)
if (name.equals(_attrs[i].getName()))
return _attrs[i].getValue();
return dft;
}
/* ------------------------------------------------------------ */
/** Get the number of children nodes.
*/
public int size()
{
if (_list!=null)
return _list.size();
return 0;
}
/* ------------------------------------------------------------ */
/** Get the ith child node or content.
* @return Node or String.
*/
public Object get(int i)
{
if (_list!=null)
return _list.get(i);
return null;
}
/* ------------------------------------------------------------ */
/** Get the first child node with the tag.
* @param tag
* @return Node or null.
*/
public Node get(String tag)
{
if (_list!=null)
{
for (int i=0;i<_list.size();i++)
{
Object o=_list.get(i);
if (o instanceof Node)
{
Node n=(Node)o;
if (tag.equals(n._tag))
return n;
}
}
}
return null;
}
/* ------------------------------------------------------------ */
public void add(int i, Object o)
{
if (_list==null)
_list=new ArrayList();
if (o instanceof String)
{
if (_lastString)
{
int last=_list.size()-1;
_list.set(last,(String)_list.get(last)+o);
}
else
_list.add(i,o);
_lastString=true;
}
else
{
_lastString=false;
_list.add(i,o);
}
}
/* ------------------------------------------------------------ */
public void clear()
{
if (_list!=null)
_list.clear();
_list=null;
}
/* ------------------------------------------------------------ */
/** Combined get(tag).toString(tags).
* @param tag
* @param tags
* @return
*/
public String getString(String tag, boolean tags, boolean trim)
{
Node node=(Node)get(tag);
if (node==null)
return null;
String s =node.toString(tags);
if (s!=null && trim)
s=s.trim();
return s;
}
/* ------------------------------------------------------------ */
public synchronized String toString()
{
return toString(true);
}
/* ------------------------------------------------------------ */
/** Convert to a string.
* @param tag If false, only content is shown.
*/
public synchronized String toString(boolean tag)
{
StringBuffer buf = new StringBuffer();
synchronized(buf)
{
toString(buf,tag);
return buf.toString();
}
}
/* ------------------------------------------------------------ */
/** Convert to a string.
* @param tag If false, only content is shown.
*/
public synchronized String toString(boolean tag,boolean trim)
{
String s=toString(tag);
if (s!=null && trim)
s=s.trim();
return s;
}
/* ------------------------------------------------------------ */
private synchronized void toString(StringBuffer buf,boolean tag)
{
if(tag)
{
buf.append("<");
buf.append(_tag);
if (_attrs!=null)
{
for (int i=0;i<_attrs.length;i++)
{
buf.append(' ');
buf.append(_attrs[i].getName());
buf.append("=\"");
buf.append(_attrs[i].getValue());
buf.append("\"");
}
}
}
if (_list!=null)
{
if(tag)
buf.append(">");
for (int i=0;i<_list.size();i++)
{
Object o=_list.get(i);
if (o==null)
continue;
if (o instanceof Node)
((Node)o).toString(buf,tag);
else
buf.append(o.toString());
}
if(tag)
{
buf.append("</");
buf.append(_tag);
buf.append(">");
}
}
else if (tag)
buf.append("/>");
}
/* ------------------------------------------------------------ */
/** Iterator over named child nodes.
* @param tag The tag of the nodes.
* @return Iterator over all child nodes with the specified tag.
*/
public Iterator iterator(final String tag)
{
return new Iterator()
{
int c=0;
Node _node;
/* -------------------------------------------------- */
public boolean hasNext()
{
if (_node!=null)
return true;
while (_list!=null && c<_list.size())
{
Object o=_list.get(c);
if (o instanceof Node)
{
Node n=(Node)o;
if (tag.equals(n._tag))
{
_node=n;
return true;
}
}
c++;
}
return false;
}
/* -------------------------------------------------- */
public Object next()
{
try
{
if (hasNext())
return _node;
throw new NoSuchElementException();
}
finally
{
_node=null;
c++;
}
}
/* -------------------------------------------------- */
public void remove()
{
throw new UnsupportedOperationException("Not supported");
}
};
}
}
}
1.1 contrib/jetty/src/main/org/mortbay/xml/configure_1_0.dtd
Index: configure_1_0.dtd
===================================================================
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
This is the document type descriptor for the
org.mortbay.util.XmlConfiguration class. It allows a java object to be
configured by with a sequence of Set, Put and Call elements. These tags are
mapped to methods on the object to be configured as follows:
<Set name="Test">value</Set> == obj.setTest("value");
<Put name="Test">value</Put> == obj.put("Test","value");
<Call name="test"><Arg>value</Arg></Call> == obj.test("value");
Values themselves may be configured objects that are created with the
<New> tag or returned from a <Call> tag.
Values are matched to arguments on a best effort approach, but types
my be specified if a match is not achieved.
$Id: configure_1_0.dtd,v 1.1 2002/01/17 01:23:24 olegnitz Exp $
-->
<!ENTITY % CONFIG "Set|Put|Call">
<!ENTITY % TYPE
"String|char|short|byte|int|long|boolean|float|double|URL|InetAddress|InetAddrPort">
<!ENTITY % VALUE "#PCDATA|Call|New|SystemProperty">
<!ENTITY % TYPEATTR "type (%TYPE;) #IMPLIED " >
<!ENTITY % CLASSATTR "class NMTOKEN #REQUIRED" >
<!ENTITY % NAMEATTR "name NMTOKEN #REQUIRED" >
<!--
Configure Element.
This is the root element that specifies the class of object that
can be configured:
<Configure class="com.acme.MyClass"> ... </Configure>
A Configure element can contain Set, Put or Call elements.
-->
<!ELEMENT Configure (%CONFIG;)* >
<!ATTLIST Configure %CLASSATTR; >
<!--
Set Element.
This element maps to a call to a set method on the current object.
The name and optional type attributes are used to select the set
method.
A Set element can contain value text and/or the value elements Call,
New and SystemProperty. If no value type is specified, then white
space is trimmed out of the value. If it contains multiple value
elements they are added as strings before being converted to any
specified type.
-->
<!ELEMENT Set ( %VALUE; )* >
<!ATTLIST Set %NAMEATTR; %TYPEATTR; >
<!--
Put Element.
This element maps to a call to a put method on the current object,
which must implement the Map interface. The name attribute is used
as the put key and the optional type attribute can force the type
of the value.
A Put element can contain value text and/or the value elements Call,
New and SystemProperty. If no value type is specified, then white
space is trimmed out of the value. If it contains multiple value
elements they are added as strings before being converted to any
specified type.
-->
<!ELEMENT Put ( %VALUE; )* >
<!ATTLIST Put %NAMEATTR; %TYPEATTR;>
<!--
Call Element.
This element maps to an arbitrary call to amethod on the current object,
The name attribute and Arg elements are used to select the method.
A Call element can contain a sequence of Arg elements followed by
a sequence of Set, Put and/or Call elements which act on any object
returned by the original call:
<Call name="test"><Arg>value1</Arg><Set name="Test">Value2</Set></Call>
This is equivalent to:
Object o2 = o1.test("value1");
o2.setTest("value2");
-->
<!ELEMENT Call (Arg*,(%CONFIG;)*)>
<!ATTLIST Call %NAMEATTR;>
<!--
Arg Element.
This element defines a positional argument for the Call element.
The optional type attribute can force the type of the value.
An Arg element can contain value text and/or the value elements Call,
New and SystemProperty. If no value type is specified, then white
space is trimmed out of the value. If it contains multiple value
elements they are added as strings before being converted to any
specified type.
-->
<!ELEMENT Arg ( %VALUE; )* >
<!ATTLIST Arg %TYPEATTR; >
<!--
New Element.
This element allows the creation of a new object as part of a
value of a Set, Put or Arg element. The class attribute determines
the type of the new object and the contained Arg elements
are used to select the constructor for the new object.
A New element can contain a sequence of Arg elements followed by
a sequence of Set, Put and/or Call elements which act on the new object:
<New class="com.acme.MyClass">
<Arg>value1</Arg><Set name="Test">Value2</Set>
</New>
This is equivalent to:
Object o = new com.acme.MyClass("value1");
o.setTest("value2");
-->
<!ELEMENT New (Arg*,(%CONFIG;)*)>
<!ATTLIST New %CLASSATTR; >
<!--
System Property Element.
This element allows JVM System properties to be retrieved as
part of the value of a Set, Put or Arg element.
The name attribute specifies the property name and the optional
default argument provides a default value.
<SystemProperty name="Test" default="value"/>
This is equivalent to:
System.getProperty("Test","value");
-->
<!ELEMENT SystemProperty EMPTY>
<!ATTLIST SystemProperty %NAMEATTR; default NMTOKEN #IMPLIED>
1.1 contrib/jetty/src/main/org/mortbay/xml/configure_1_1.dtd
Index: configure_1_1.dtd
===================================================================
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
This is the document type descriptor for the
org.mortbay.util.XmlConfiguration class. It allows a java object to be
configured by with a sequence of Set, Put and Call elements. These tags are
mapped to methods on the object to be configured as follows:
<Set name="Test">value</Set> == obj.setTest("value");
<Put name="Test">value</Put> == obj.put("Test","value");
<Call name="test"><Arg>value</Arg></Call> == obj.test("value");
Values themselves may be configured objects that are created with the
<New> tag or returned from a <Call> tag.
Values are matched to arguments on a best effort approach, but types
my be specified if a match is not achieved.
$Id: configure_1_1.dtd,v 1.1 2002/01/17 01:23:24 olegnitz Exp $
-->
<!ENTITY % CONFIG "Set|Put|Call|New">
<!ENTITY % TYPE
"String|Character|Short|Byte|Integer|Long|Boolean|Float|Double|char|short|byte|int|long|boolean|float|double|URL|InetAddress|InetAddrPort">
<!ENTITY % VALUE "#PCDATA|Call|New|SystemProperty">
<!ENTITY % TYPEATTR "type (%TYPE;) #IMPLIED " >
<!ENTITY % CLASSATTR "class NMTOKEN #REQUIRED" >
<!ENTITY % NAMEATTR "name NMTOKEN #REQUIRED" >
<!--
Configure Element.
This is the root element that specifies the class of object that
can be configured:
<Configure class="com.acme.MyClass"> ... </Configure>
A Configure element can contain Set, Put or Call elements.
-->
<!ELEMENT Configure (%CONFIG;)* >
<!ATTLIST Configure %CLASSATTR; >
<!--
Set Element.
This element maps to a call to a set method on the current object.
The name and optional type attributes are used to select the set
method.
A Set element can contain value text and/or the value elements Call,
New and SystemProperty. If no value type is specified, then white
space is trimmed out of the value. If it contains multiple value
elements they are added as strings before being converted to any
specified type.
-->
<!ELEMENT Set ( %VALUE; )* >
<!ATTLIST Set %NAMEATTR; %TYPEATTR; >
<!--
Put Element.
This element maps to a call to a put method on the current object,
which must implement the Map interface. The name attribute is used
as the put key and the optional type attribute can force the type
of the value.
A Put element can contain value text and/or the value elements Call,
New and SystemProperty. If no value type is specified, then white
space is trimmed out of the value. If it contains multiple value
elements they are added as strings before being converted to any
specified type.
-->
<!ELEMENT Put ( %VALUE; )* >
<!ATTLIST Put %NAMEATTR; %TYPEATTR;>
<!--
Call Element.
This element maps to an arbitrary call to a method on the current object,
The name attribute and Arg elements are used to select the method.
A Call element can contain a sequence of Arg elements followed by
a sequence of Set, Put and/or Call elements which act on any object
returned by the original call:
<Call name="test"><Arg>value1</Arg><Set name="Test">Value2</Set></Call>
This is equivalent to:
Object o2 = o1.test("value1");
o2.setTest("value2");
-->
<!ELEMENT Call (Arg*,(%CONFIG;)*)>
<!ATTLIST Call %NAMEATTR;>
<!--
Arg Element.
This element defines a positional argument for the Call element.
The optional type attribute can force the type of the value.
An Arg element can contain value text and/or the value elements Call,
New and SystemProperty. If no value type is specified, then white
space is trimmed out of the value. If it contains multiple value
elements they are added as strings before being converted to any
specified type.
-->
<!ELEMENT Arg ( %VALUE; )* >
<!ATTLIST Arg %TYPEATTR; >
<!--
New Element.
This element allows the creation of a new object as part of a
value of a Set, Put or Arg element. The class attribute determines
the type of the new object and the contained Arg elements
are used to select the constructor for the new object.
A New element can contain a sequence of Arg elements followed by
a sequence of Set, Put and/or Call elements which act on the new object:
<New class="com.acme.MyClass">
<Arg>value1</Arg><Set name="Test">Value2</Set>
</New>
This is equivalent to:
Object o = new com.acme.MyClass("value1");
o.setTest("value2");
-->
<!ELEMENT New (Arg*,(%CONFIG;)*)>
<!ATTLIST New %CLASSATTR; >
<!--
System Property Element.
This element allows JVM System properties to be retrieved as
part of the value of a Set, Put or Arg element.
The name attribute specifies the property name and the optional
default argument provides a default value.
<SystemProperty name="Test" default="value"/>
This is equivalent to:
System.getProperty("Test","value");
-->
<!ELEMENT SystemProperty EMPTY>
<!ATTLIST SystemProperty %NAMEATTR; default NMTOKEN #IMPLIED>
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development