📄 nafnavigator.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.xml;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.jaxen.DefaultNavigator;
import org.jaxen.XPath;
import org.jaxen.saxpath.SAXPathException;
import org.jaxen.util.SingleObjectIterator;
import com.novocode.naf.resource.NGComponent;
/**
* A Navigator implementation which allows Jaxen to traverse
* NAF widget hierarchies.
*
* @author Stefan Zeiger (szeiger@novocode.com)
* @since Nov 17, 2004
* @version $Id: NAFNavigator.java,v 1.2 2004/11/19 22:02:47 szeiger Exp $
*/
public class NAFNavigator extends DefaultNavigator
{
private static final long serialVersionUID = 3690194356430714930L;
private static final NAFNavigator instance = new NAFNavigator();
public static final class Attr
{
public final NGComponent parent;
public final String name;
public final String value;
Attr(NGComponent parent, String name, String value)
{
this.parent = parent;
this.name = name;
this.value = value;
}
public boolean equals(Object o)
{
if(!(o instanceof Attr)) return false;
Attr a = (Attr)o;
return parent == a.parent && name.equals(a.name);
}
public int hashCode()
{
return parent.hashCode() + name.hashCode();
}
public String toString()
{
return name+"=\""+value+"\"";
}
}
public static final class Document
{
public final NGComponent root;
Document(NGComponent root) { this.root = root; }
public boolean equals(Object o)
{
if(!(o instanceof Document)) return false;
return root == ((Document)o).root;
}
public int hashCode() { return root.hashCode(); }
}
private NAFNavigator() {}
public static NAFNavigator getInstance() { return instance; }
public String getElementNamespaceUri(Object o)
{
return null;
}
public String getElementName(Object o)
{
String s = ((NGComponent)o).getElementName();
if(s == null)
{
s = o.getClass().getName();
int i = s.lastIndexOf('.');
if(i != -1) s = s.substring(i+1);
}
return s;
}
public String getElementQName(Object o)
{
return getElementName(o);
}
public String getAttributeNamespaceUri(Object o)
{
return null;
}
public String getAttributeName(Object o)
{
return ((Attr)o).name;
}
public String getAttributeQName(Object o)
{
return getAttributeName(o);
}
public boolean isDocument(Object o)
{
return o instanceof Document;
}
public boolean isElement(Object o)
{
return o instanceof NGComponent;
}
public boolean isAttribute(Object o)
{
return o instanceof Attr;
}
public boolean isNamespace(Object o)
{
return false;
}
public boolean isComment(Object o)
{
return false;
}
public boolean isText(Object o)
{
return false;
}
public boolean isProcessingInstruction(Object o)
{
return false;
}
public String getCommentStringValue(Object o)
{
return null;
}
public String getElementStringValue(Object o)
{
return "";
}
public String getAttributeStringValue(Object o)
{
return ((Attr)o).value;
}
public String getNamespaceStringValue(Object o)
{
return null;
}
public String getTextStringValue(Object o)
{
return null;
}
public String getNamespacePrefix(Object o)
{
return null;
}
public XPath parseXPath(String s) throws SAXPathException
{
return new NAFXPath(s);
}
public Object getDocumentNode(Object o)
{
if(o instanceof Document) return o;
NGComponent c;
if(o instanceof Attr) c = ((Attr)o).parent;
else c = (NGComponent)o;
if(c == null || c.getParent() == null) return null;
NGComponent root = c;
while(c.getParent() != null) { root = c; c = c.getParent(); }
return new Document(root);
}
public Iterator getAttributeAxisIterator(Object o)
{
if(!(o instanceof NGComponent)) return null;
NGComponent c = (NGComponent)o;
List<Attr> l = new ArrayList<Attr>();
String id = c.getID();
if(id != null) l.add(new Attr(c, "id", id));
// [TODO] Add "role" and other attributes
return l.iterator();
}
public Iterator getChildAxisIterator(Object o)
{
if(o instanceof Document) return new SingleObjectIterator(((Document)o).root);
if(!(o instanceof NGComponent)) return null;
NGComponent c = (NGComponent)o;
List<NGComponent> l = new ArrayList<NGComponent>();
l.addAll(c.getChildren(null));
for(String role : c.getUsedRoles()) l.addAll(c.getChildren(role));
return l.iterator();
}
public Iterator getParentAxisIterator(Object o)
{
if(o instanceof Attr) return new SingleObjectIterator(((Attr)o).parent);
if(o instanceof NGComponent)
{
NGComponent c = (NGComponent)o;
if(c.getParent() != null && c.getParent().getParent() == null)
return new SingleObjectIterator(new Document(c));
return new SingleObjectIterator(((NGComponent)o).getParent());
}
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -