📄 elementadapter.java
字号:
/*
* $Id: ElementAdapter.java,v 1.7 2006/01/29 21:52:12 geoffm74 Exp $
* $Source: /cvsroot/domify/domify/src/java/org/infohazard/domify/ElementAdapter.java,v $
*/
package org.infohazard.domify;
import org.apache.log4j.Category;
import org.w3c.dom.*;
import java.lang.reflect.*;
import java.util.*;
/**
*/
public class ElementAdapter extends NodeAdapter implements Element
{
/** Logging category for log4j. */
protected static Category log = Category.getInstance(ElementAdapter.class.getName());
/**
* <code>Class</code> object representing class extended by all Java 5 Enums. If not running
* in Java 5, this will be null.
*
*/
protected static Class enumClass;
/**
*/
String nodeName;
/**
* Used by the NodeListAdapter
*/
protected static Object[] emptyArgList = new Object[0];
private static final String[] ENUM_EXCLUDE_METHODS = new String[]{"getDeclaringClass"};
static{
try {
enumClass=ElementAdapter.class.getClassLoader().loadClass("java.lang.Enum");
} catch (ClassNotFoundException e) {
enumClass=null;
}
}
/**
*/
protected class MethodNodeListAdapter implements NodeList
{
/**
* Local list of pethods that meet our property definition below.
*/
protected List propertyMethods;
/**
* creates a list of methods for the wrapped (in our parent class)
* object that meet the following criteria:
*
* public, no args req'd, non-void return,
* not defined by Object interface
* and starts with "get" or "is"
*/
public MethodNodeListAdapter()
{
this(null);
}
/**
* creates a list of methods for the wrapped (in our parent class)
* object that meet the following criteria:
*
* public, no args req'd, non-void return,
* not defined by Object interface,
* starts with "get" or "is",
* and not in specified list of names to exclude
*/
public MethodNodeListAdapter(String[] excludeMethods){
List excludeMethodsList = excludeMethods==null ? Collections.EMPTY_LIST : Arrays.asList(excludeMethods);
Method[] methods = getWrapped().getClass().getMethods();
// A good outside bound
this.propertyMethods = new ArrayList(methods.length);
for (int i=0; i<methods.length; i++)
{
Method m = methods[i];
if (Modifier.isPublic(m.getModifiers()) &&
m.getParameterTypes().length == 0 &&
m.getReturnType() != null &&
!m.getDeclaringClass().equals(Object.class) &&
(m.getName().startsWith("get") || m.getName().startsWith("is")) &&
(!excludeMethodsList.contains(m.getName())))
{
// add it!
this.propertyMethods.add(m);
}
}
}
/**
* returns the length of the method exposed as nodes.
*/
public int getLength()
{
log.debug("MethodNodeListAdapter.getLength() is " + this.propertyMethods.size());
return this.propertyMethods.size();
}
/**
* retrieves a single node by index from our adapted object
*/
public Node item(int index)
{
log.debug("MethodNodeListAdapter.item(" + index + ")");
Method m = (Method)this.propertyMethods.get(index);
try
{
String nodeName;
if (m.getName().startsWith("is"))
nodeName = m.getName().substring("is".length());
else if(m.getName().startsWith("get"))
nodeName = m.getName().substring("get".length());
else
nodeName = m.getName();
nodeName = java.beans.Introspector.decapitalize(nodeName);
return new ElementAdapter(getWrapped(), m, nodeName, ElementAdapter.this, index, config);
}
catch (RuntimeException ex)
{
throw ex;
}
catch (Exception ex)
{
// This should never happen because we chose the method for its
// public access and no args.
ex.printStackTrace();
String message = "Exception when invoking " + m + ": " + ex;
throw new RuntimeException(message);
}
}
}
/**
*/
protected class ToStringNodeListAdapter implements NodeList
{
/** Cache the node */
protected Node value;
/** */
public ToStringNodeListAdapter()
{
}
/** */
public int getLength()
{
log.debug("ToStringNodeListAdapter.getLength() is 1");
return 1;
}
/** */
public Node item(int index)
{
log.debug("ToStringNodeListAdapter.item(" + index + ")");
if (value == null)
value = new TextAdapter(getWrapped().toString(), ElementAdapter.this, index);
return value;
}
}
/**
*/
protected class NodeNodeListAdapter implements NodeList
{
/** */
public int getLength()
{
log.debug("NodeNodeListAdapter.getLength() is 1");
return 1;
}
/** */
public Node item(int index)
{
log.debug("NodeNodeListAdapter.item(" + index + ")");
return (Node)getWrapped();
}
}
/**
*/
protected class CollectionNodeListAdapter implements NodeList
{
/** Cache the node */
protected List nodes;
/** */
public CollectionNodeListAdapter(Collection col)
{
//String newNodeName = nodeName + "Item";
String newNodeName = "item";
nodes = new ArrayList(col.size());
int index = 0;
Iterator it = col.iterator();
while (it.hasNext())
{
ElementAdapter child = new TypedElementAdapter(it.next(), newNodeName, ElementAdapter.this, index, config);
nodes.add(child);
index++;
}
}
/** The array parameter MUST BE AN ARRAY! Can contain objects or basic types. */
public CollectionNodeListAdapter(Object array)
{
//String newNodeName = nodeName + "Item";
String newNodeName = "item";
int length = java.lang.reflect.Array.getLength(array);
nodes = new ArrayList(length);
for (int index=0; index<length; index++)
{
Object obj = java.lang.reflect.Array.get(array, index);
ElementAdapter child = new TypedElementAdapter(obj, newNodeName, ElementAdapter.this, index, config);
nodes.add(child);
}
}
/** */
public int getLength()
{
log.debug("CollectionNodeListAdapter.getLength() is " + nodes.size());
return nodes.size();
}
/** */
public Node item(int index)
{
log.debug("CollectionNodeListAdapter.item(" + index + ")");
return (Node)nodes.get(index);
}
}
/**
*/
protected class MapNodeListAdapter implements NodeList
{
/** Cache the node */
protected List nodes;
/** */
public MapNodeListAdapter(Map m)
{
//String newNodeName = nodeName + "Item";
String newNodeName = "item";
nodes = new ArrayList(m.size());
int index = 0;
Iterator it = m.entrySet().iterator();
while (it.hasNext())
{
Map.Entry mapEntry = (Map.Entry)it.next();
MapEntryElementAdapter child = new MapEntryElementAdapter(mapEntry, newNodeName, ElementAdapter.this, index, config);
nodes.add(child);
index++;
}
}
/** */
public int getLength()
{
log.debug("MapNodeListAdapter.getLength() is " + nodes.size());
return nodes.size();
}
/** */
public Node item(int index)
{
log.debug("MapNodeListAdapter.item(" + index + ")");
return (Node)nodes.get(index);
}
}
/**
*/
protected static class EmptyNodeListAdapter implements NodeList
{
/** We only need one of these */
public static final NodeList EMPTY_LIST = new EmptyNodeListAdapter();
/** */
public int getLength() { return 0; }
/** */
public Node item(int index) { throw new IllegalStateException(); }
}
protected class EnumNodeListAdapter extends MethodNodeListAdapter
{
public EnumNodeListAdapter(){
super(ENUM_EXCLUDE_METHODS);
try {
this.propertyMethods.add(getWrapped().getClass().getMethod("ordinal",null));
} catch (NoSuchMethodException e) {
log.debug("Enum doesn't have ordinal method. Huh?",e);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -