📄 ngcomponent.java
字号:
/*******************************************************************************
* Copyright (c) 2004 Stefan Zeiger and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.novocode.com/legal/epl-v10.html
*
* Contributors:
* Stefan Zeiger (szeiger@novocode.com) - initial API and implementation
*******************************************************************************/
package com.novocode.naf.resource;
import java.io.*;
import java.net.URL;
import java.util.*;
import java.util.List;
import org.w3c.dom.*;
import com.novocode.naf.app.*;
import com.novocode.naf.data.XMLPropertyDescriptor;
import com.novocode.naf.data.XMLProperty;
import com.novocode.naf.data.XMLPropertyManager;
/**
* Base class for resource objects which are created from XML files.
*
* @author Stefan Zeiger (szeiger@novocode.com)
* @since Nov 24, 2003
*/
public abstract class NGComponent implements Cloneable
{
protected ResourceManager resourceManager;
protected Element selfElement;
protected XMLPropertyManager propertyManager = createPropertyManager();
private String elementName;
private URL resURL;
private String id;
private Map<String, List<NGComponent>> roleChildren = new HashMap<String, List<NGComponent>>();
private Map<String, List<NGComponent>> expandedRoleChildren = new HashMap<String, List<NGComponent>>();
private List<NGComponent> children = new ArrayList<NGComponent>();
private List<NGComponent> expandedChildren;
private boolean postInitCalled;
private List<Import> imports;
private NGComponent parent;
private String role;
private Class referencedClass;
@XMLProperty
public void setRole(String s) { role = (s == null || s.length() == 0) ? null : s; }
public String getRole() { return role; }
public void expandSelf(List<NGComponent> l)
{
l.add(this);
}
protected XMLPropertyManager createPropertyManager()
{
return XMLPropertyManager.forClass(getClass());
}
public final void init(Class referencedClass, Element e, URL resURL, String id, ResourceManager resourceManager, NGComponent parent) throws NAFException
{
this.selfElement = e;
this.resURL = resURL;
this.id = id;
this.resourceManager = resourceManager;
this.elementName = e.getLocalName();
this.parent = parent;
this.referencedClass = referencedClass;
for(Node n = e.getFirstChild(); n != null; n = n.getNextSibling()) parseChild(n);
NamedNodeMap attrs = e.getAttributes();
for(int i=0; i<attrs.getLength(); i++) parseChild(attrs.item(i));
init();
if(!postInitCalled) throw new NAFException("Class "+getClass().getName()+" (or an ancestor thereof) forgot to call super.postInit()");
}
protected void init() throws NAFException
{
postInitCalled = true;
}
protected void parseChild(Node n) throws NAFException
{
if(n.getNodeType() == Node.ATTRIBUTE_NODE && n.getNamespaceURI() == null)
{
String name = ((Attr)n).getLocalName();
if(!name.startsWith("_"))
{
XMLPropertyDescriptor pd = propertyManager.getXMLPropertyDescriptor(name);
if(pd != null) pd.set(this, ((Attr)n).getValue());
}
}
else if(n.getNodeType() == Node.ELEMENT_NODE && ResourceManager.NAF_NAMESPACE_URI.equals(n.getNamespaceURI()))
parseNAFChildElement((Element)n);
}
protected void parseNAFChildElement(Element e) throws NAFException
{
if("import".equals(e.getLocalName()))
{
Import im = Import.parse(e);
if(imports == null) imports = new ArrayList<Import>();
imports.add(im);
}
else
{
String nafid = e.getAttribute("id");
if(nafid != null && nafid.length() == 0) nafid = null;
NGComponent ch = resourceManager.createComponent(e, nafid, resURL, this);
if(ch.role == null) children.add(ch);
else
{
List<NGComponent> cl = roleChildren.get(ch.role);
if(cl == null)
{
cl = new ArrayList<NGComponent>();
roleChildren.put(ch.role, cl);
}
cl.add(ch);
}
}
}
public final String toString()
{
return toString(false);
}
public final String toString(boolean expand)
{
StringWriter swr = new StringWriter();
PrintWriter pwr = new PrintWriter(swr);
dump(pwr, "", expand);
pwr.flush();
return swr.getBuffer().toString();
}
private void dump(PrintWriter out, String indent, boolean expand)
{
String cln = getClass().getName();
if(cln.startsWith("com.novocode.naf.")) cln = cln.substring(17);
out.print(indent+cln+" <"+elementName+">: "+getAttributeDump());
for(XMLPropertyDescriptor mp : propertyManager.getXMLPropertyDescriptors())
{
if(mp.getReadMethod() == null) continue;
String pval = mp.get(this);
if(pval != null) out.print(", "+mp.getXMLPropertyName()+"="+pval);
}
out.println();
for(NGComponent ch : expand ? getChildren(null) : children)
ch.dump(out, indent + " ", expand);
for(Iterator<String> it = roleChildren.keySet().iterator(); it.hasNext();)
{
String key = it.next();
List<NGComponent> cl = roleChildren.get(key);
out.println(indent + " ["+key+"]");
for(NGComponent ch: cl) ch.dump(out, indent + " ", expand);
}
}
protected StringBuffer getAttributeDump()
{
StringBuffer b = new StringBuffer();
b.append("resURL=").append(resURL);
if(id != null) b.append(", id=").append(id);
return b;
}
public final List<NGComponent> getUnexpandedChildren(String role)
{
if(role == null) return children;
else return roleChildren.get(role);
}
public final <T extends NGComponent> List<T> getChildren(String role)
{
if(role == null)
{
if(expandedChildren == null)
{
expandedChildren = new ArrayList<NGComponent>();
for(NGComponent ch : children) ch.expandSelf(expandedChildren);
}
return (List<T>)expandedChildren;
}
else
{
List<NGComponent> l = expandedRoleChildren.get(role);
if(l == null)
{
List<NGComponent> src = roleChildren.get(role);
if(src != null)
{
l = new ArrayList<NGComponent>();
for(NGComponent ch : src) ch.expandSelf(l);
expandedRoleChildren.put(role, l);
}
}
return (List<T>)l;
}
}
public Set<String> getUsedRoles()
{
return roleChildren.keySet();
}
public final NGComponent getFirstChild(String role)
{
List<NGComponent> l = getChildren(role);
if(l == null || l.size() == 0) return null;
return l.get(0);
}
final List<Import> getImports() { return imports; }
public final NGComponent getParent() { return parent; }
public String getID() { return id; }
public URL getResourceURL() { return resURL; }
public String getElementName() { return elementName; }
public Class<?> getReferencedClass() { return referencedClass; }
public void setReferencedClass(Class referencedClass) { this.referencedClass = referencedClass; }
public Object clone()
{
try
{
return super.clone();
}
catch(CloneNotSupportedException ex)
{
ex.printStackTrace();
System.exit(1);
return null;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -