26 package org.nexusformat.nxvalidate;
28 import java.util.ArrayList;
29 import java.util.Vector;
31 import javax.swing.event.TreeModelListener;
32 import javax.swing.tree.TreeModel;
33 import javax.swing.tree.TreePath;
35 import org.w3c.dom.NamedNodeMap;
36 import org.w3c.dom.Node;
37 import org.w3c.dom.NodeList;
39 class NXSnode
extends AbstractNXSnode implements TreeModel {
41 private static final String PADDING =
" ";
42 private static final String NXROOT =
"NXroot";
43 private static final String TYPE =
"NAPItype";
44 private static final String LINK =
"NAPIlink";
45 private static final String SDS =
"SDS";
46 private static final Logger LOG = Logger.getInstance();
48 private String nxclass;
49 private ArrayList<AbstractNXSnode> children;
50 private ArrayList<SVRLitem> svrl;
55 this.children =
new ArrayList<AbstractNXSnode>();
56 this.svrl =
new ArrayList<SVRLitem>();
59 NXSnode(
final Node node) {
61 this.nxclass = node.getNodeName();
62 this.setAttrs(node.getAttributes());
64 NodeList nodes = node.getChildNodes();
65 int size = nodes.getLength();
66 for (
int i = 0; i < size; i++) {
67 this.addChild(nodes.item(i));
71 ArrayList<SVRLitem> addSVRL(
final Node svrl) {
73 ArrayList<Node> errors =
new ArrayList<Node>();
74 addSVRL(svrl, errors);
77 ArrayList<SVRLitem> items =
new ArrayList<SVRLitem>();
78 for (Node node : errors) {
79 items.add(
new SVRLitem(node));
84 for (SVRLitem item : items) {
85 nxsnode = getNode(
this, item.getLocationArray(), 1);
86 nxsnode.svrl.add(item);
92 private static NXSnode getNode(NXSnode parent, ArrayList<String> location,
95 if (location.size() <= depth) {
98 String partPath = location.get(depth);
99 int left = partPath.indexOf(
"[");
100 int right = partPath.indexOf(
"]", left);
101 String name = partPath.substring(0, left);
102 int index = Integer.parseInt(partPath.substring(left + 1, right)) - 1;
103 LOG.debug(
"Looking for " + name +
"[" + index +
"]");
106 ArrayList<NXSnode> choices =
new ArrayList<NXSnode>();
108 for (AbstractNXSnode child : parent.children) {
109 if (child instanceof NXSnode) {
110 temp = (NXSnode) child;
111 if (equals(temp.getType(), name)) {
113 }
else if (equals(temp.getName(), name)) {
120 int numChoice = choices.size();
121 LOG.debug(
"Found " + numChoice +
" options");
122 if ((numChoice <= 0) || (numChoice < index)) {
125 if (depth >= location.size()) {
126 return choices.get(index);
128 return getNode(choices.get(index), location, depth + 1);
132 private static boolean equals(
final String left,
final String right) {
139 return left.equals(right);
142 private static void addSVRL(
final Node node,
final ArrayList<Node> errors) {
143 if (SVRLitem.hasLocation(node)) {
147 NodeList nodes = node.getChildNodes();
148 int size = nodes.getLength();
149 for (
int i = 0; i < size; i++) {
150 addSVRL(nodes.item(i), errors);
154 private void setAttrs(
final NamedNodeMap attrs) {
158 int size = attrs.getLength();
160 for (
int i = 0; i < size; i++) {
161 attr = attrs.item(i);
162 this.setAttr(attr.getNodeName(), attr.getNodeValue());
166 void setAttr(
final String name,
final String value) {
167 if (name.equals(
"name")) {
170 }
else if (name.equals(TYPE)) {
171 this.setName(this.nxclass);
175 this.children.add(0,
new Attribute(name, value));
178 private void addChild(
final Node node) {
182 int type = node.getNodeType();
183 if (type != Node.ELEMENT_NODE) {
186 this.children.add(
new NXSnode(node));
194 return (this.svrl.size() > 0);
197 private String getAttrValue(
final String name) {
198 int size = this.children.size();
199 AbstractNXSnode node;
200 for (
int i = 0; i < size; i++) {
201 node = this.children.get(i);
202 if (node instanceof NXSnode) {
204 }
else if (this.children.get(i).getName().equals(name)) {
205 return ((Attribute) node).getValue();
211 public String toString() {
213 if (NXROOT.equals(
this.getName()) || NXROOT.equals(
this.nxclass)) {
215 }
else if (LINK.equals(
this.getName())) {
216 result = this.getName() +
":target=" + this.getAttrValue(
"target");
217 }
else if (SDS.equals(
this.nxclass)) {
218 result = this.getName() +
":" + TYPE +
"=" + this.getAttrValue(TYPE);
220 result = this.getName() +
":" + this.nxclass;
222 if (this.hasError()) {
229 public void printTree() {
233 private void printTree(String padding) {
234 System.out.println(padding + this.toString());
236 for (AbstractNXSnode node : this.children) {
237 if (node instanceof NXSnode)
239 ((NXSnode) node).printTree(padding);
244 public boolean equals(
final Object other) {
252 if (!(other instanceof NXSnode)) {
257 NXSnode temp = (NXSnode) other;
258 if (!this.getName().equals(temp.getName())) {
261 if (!this.nxclass.equals(temp.nxclass)) {
264 if (!this.children.equals(temp.children)) {
267 if (!this.svrl.equals(temp.svrl)) {
275 private static NXSnode toNXSnode(
final Object
object) {
276 if (
object == null) {
277 throw new Error(
"Cannot convert null object to NXSnode");
279 if (!(
object instanceof NXSnode)) {
280 throw new Error(
"Cannot convert \"" +
object +
"\" to NXSnode");
282 return (NXSnode) object;
287 public void addTreeModelListener(TreeModelListener l) {
291 public Object getChild(Object parent,
int index) {
292 if (parent instanceof NXSnode) {
293 return ((NXSnode) parent).children.get(index);
299 public int getChildCount(Object parent) {
300 NXSnode temp = toNXSnode(parent);
301 return temp.children.size();
304 public int getIndexOfChild(Object parent, Object child) {
305 if (parent == null) {
311 NXSnode myParent = toNXSnode(parent);
312 if (child instanceof AbstractNXSnode) {
313 return myParent.children.indexOf((AbstractNXSnode) child);
319 public Object getRoot() {
323 public boolean isLeaf(
final Object node) {
324 if (node instanceof NXSnode) {
325 return (((NXSnode) node).children.size() <= 0);
332 public void removeTreeModelListener(TreeModelListener l) {
337 public void valueForPathChanged(TreePath path, Object newValue) {