NXvalidate  1
 All Classes Namespaces Files Functions Variables
NXNodeMapper.java
Go to the documentation of this file.
1 /* NeXus - Neutron & X-ray Common Data Format
2  *
3  * NeXus file validation GUI tool.
4  *
5  * Copyright (C) 2010 Stephen Rankin
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * For further information, see <http://www.nexusformat.org/>
22  *
23  * NXNodeMapper.java
24  *
25  */
26 package org.nexusformat.nxvalidate;
27 
28 import java.io.File;
29 import java.util.ArrayList;
30 import java.util.Enumeration;
31 import java.util.NoSuchElementException;
32 import javax.swing.tree.MutableTreeNode;
33 import javax.swing.tree.TreeNode;
34 import org.w3c.dom.Document;
35 import org.w3c.dom.Element;
36 import org.w3c.dom.NamedNodeMap;
37 import org.w3c.dom.Node;
38 import org.w3c.dom.NodeList;
39 
44 public class NXNodeMapper implements MutableTreeNode {
45 
46  private Node domNode = null;
47  private String nodeName = null;
48  private boolean isRoot = false;
49  private boolean isLeaf = false;
50  private boolean isDocument = false;
51  private boolean allowsChildren = false;
52  private ArrayList<NXNodeMapper> documents = null;
53  static final int ELEMENT_TYPE = Node.ELEMENT_NODE;
54  private int childCount = 0;
55  private ArrayList<Node> children = null;
56  private NXNodeMapper root = null;
57  private File nxsFile = null;
58  private File nxdlFile = null;
59  private File reducedFile = null;
60  private File resultsFile = null;
61  private Document reducedDoc = null;
62  private Document resultsDoc = null;
63  private boolean badNode = false;
64  private boolean warnNode = false;
65  private boolean validatedNode = false;
66  private Object userObject = null;
67 
68  // Construct an Adapter node from a DOM node
69  public NXNodeMapper(Node node, boolean isDocument, File nxsFile) {
70 
71  this.isDocument = isDocument;
72  domNode = node;
73 
74  allowsChildren = true;
75  if (node.getNodeType() != ELEMENT_TYPE) {
76  isLeaf = true;
77  }
78 
79  this.nxsFile = nxsFile;
80  nodeName = nxsFile.getAbsolutePath();
81  children = getElements();
82  }
83 
84  // Construct an Adapter node from a DOM node
85  public NXNodeMapper(Node node, boolean isDocument, String nodeName) {
86 
87  this.isDocument = isDocument;
88  domNode = node;
89 
90  allowsChildren = true;
91  if (node.getNodeType() != ELEMENT_TYPE) {
92  isLeaf = true;
93  }
94 
95  this.nodeName = nodeName;
96  children = getElements();
97  }
98 
99  public NXNodeMapper(String nodeName) {
100  this.nodeName = nodeName;
101  isRoot = true;
102  allowsChildren = true;
103  documents = new ArrayList<NXNodeMapper>();
104  }
105 
110  public boolean isRoot() {
111  return isRoot;
112  }
113 
120  public boolean isDocument() {
121  return isDocument;
122  }
123 
129  public void setDocument(boolean isDocument) {
130  this.isDocument = isDocument;
131  }
132 
137  public void setRoot(NXNodeMapper root) {
138  this.root = root;
139  }
140 
146  public File getNXSFile() {
147  return nxsFile;
148  }
149 
155  public File getNXDLFile() {
156  return nxdlFile;
157  }
158 
163  public void setNXDLFile(File nxdlFile) {
164  this.nxdlFile = nxdlFile;
165  }
166 
173  public File getReducedFile() {
174  return reducedFile;
175  }
176 
182  public void setReducedFile(File reducedFile) {
183  this.reducedFile = reducedFile;
184  }
185 
191  public File getResultsFile() {
192  return resultsFile;
193  }
194 
200  public void setResultsFile(File resultsFile) {
201  this.resultsFile = resultsFile;
202  }
203 
209  public Document getResultsDoc() {
210  return resultsDoc;
211  }
212 
218  public void setResultsDoc(Document resultsDoc) {
219  this.resultsDoc = resultsDoc;
220  }
221 
228  public Document getReducedDoc() {
229  return reducedDoc;
230  }
231 
238  public void setReducedDoc(Document reducedDoc) {
239  this.reducedDoc = reducedDoc;
240  }
241 
248  public void setBadNode(boolean badNode) {
249  domNode.setUserData("bad", new Boolean(badNode),null);
250  this.badNode = badNode;
251  }
252 
259  public boolean getBadNode() {
260  checkBadNode();
261  return badNode;
262  }
263 
270  public void setWarnNode(boolean warnNode) {
271  domNode.setUserData("warn", new Boolean(warnNode),null);
272  this.warnNode = warnNode;
273  }
274 
281  public boolean getWarnNode() {
282  checkWarnNode();
283  return warnNode;
284  }
285 
291  public void setValidatedNode(boolean validatedNode) {
292  domNode.setUserData("validated", new Boolean(validatedNode),null);
293  this.validatedNode = validatedNode;
294  }
295 
301  public boolean getValidatedNode() {
303  return validatedNode;
304  }
305 
310  public Node getDomNode() {
311  return domNode;
312  }
313 
318  public void checkBadNode() {
319  if (!isRoot) {
320  Boolean bad = (Boolean) domNode.getUserData("bad");
321  if (bad != null) {
322  this.badNode = bad.booleanValue();
323  }
324  }
325  }
326 
331  public void checkWarnNode() {
332  if (!isRoot) {
333  Boolean warn = (Boolean) domNode.getUserData("warn");
334  if (warn != null) {
335  this.warnNode = warn.booleanValue();
336  }
337  }
338  }
339 
344  public void checkValidatedNode() {
345  if (!isRoot) {
346  Boolean validated = (Boolean) domNode.getUserData("validated");
347  if (validated != null) {
348  this.validatedNode = validated.booleanValue();
349  }
350  }
351  }
352 
358  public boolean checkBadChildren() {
359 
360  boolean result = false;
361  Boolean bad;
362 
363 
364  if (!isRoot) {
365 
366  for (int i = 0; i < domNode.getChildNodes().getLength(); i++) {
367  Node node = domNode.getChildNodes().item(i);
368  bad = (Boolean) node.getUserData("bad");
369  if (bad != null) {
370  result = true;
371  }
372 
373  // Now let's check the grandkids... and beyond...
374  bad = (Boolean) checkChildren(node);
375  if (bad) {
376  result = true;
377  }
378  }
379 
380  }
381  return result;
382  }
383 
390  private boolean checkChildren(Node node) {
391  boolean result = false;
392  for (int i = 0; i < node.getChildNodes().getLength(); i++) {
393  Node tmpNode = node.getChildNodes().item(i);
394  if (tmpNode.hasChildNodes()) {
395  checkChildren(tmpNode);
396  }
397  Boolean bad = (Boolean) tmpNode.getUserData("bad");
398  if (bad != null) {
399  result = true;
400  }
401  }
402  return result;
403  }
404 
409  public void resetNode() {
410 
411  badNode = false;
412  domNode.setUserData("texts", null, null);
413  domNode.setUserData("tests", null, null);
414  domNode.setUserData("diags", null, null);
415  domNode.setUserData("diagatts", null, null);
416  domNode.setUserData("bad", new Boolean(false), null);
417  }
418 
424  public ArrayList<String> getNodeTexts() {
425  if (!isRoot) {
426  return (ArrayList<String>) domNode.getUserData("texts");
427  } else {
428  return null;
429  }
430  }
431 
438  public ArrayList<String> getNodeTests() {
439  if (!isRoot) {
440  return (ArrayList<String>) domNode.getUserData("tests");
441  } else {
442  return null;
443  }
444  }
445 
452  public ArrayList<String> getNodeDiags() {
453  if (!isRoot) {
454  return (ArrayList<String>) domNode.getUserData("diags");
455  } else {
456  return null;
457  }
458  }
459 
460  public ArrayList<String> getNodeDiagAtts() {
461  if (!isRoot) {
462  return (ArrayList<String>) domNode.getUserData("diagatts");
463  } else {
464  return null;
465  }
466  }
467 
468  // Return the node name
469  @Override
470  public String toString() {
471  return nodeName;
472  }
473 
474  public int getIndex(TreeNode child1) {
475 
476  NXNodeMapper child = (NXNodeMapper) child1;
477  int count = getChildCount();
478  if (isRoot) {
479 
480  if (documents.contains(child)) {
481 
482  return documents.indexOf(child);
483  }
484 
485  } else {
486  for (int i = 0; i
487  < count; i++) {
488  NXNodeMapper n = this.getChildAt(i);
489 
490  if (child.domNode == n.domNode) {
491  return i;
492  }
493  }
494  }
495  return -1; // Should never get here.
496  }
497 
498  public NXNodeMapper getChildAt(int searchIndex) {
499 
500  if (isRoot) {
501 
502  return documents.get(searchIndex);
503 
504  } else {
505  //Note: JTree index is zero-based.
506  Node node = domNode.getChildNodes().item(searchIndex);
507 
508  // Return Nth displayable node
509  int elementNodeIndex = 0;
510 
511 
512 
513  for (int i = 0; i
514  < domNode.getChildNodes().getLength(); i++) {
515  node = domNode.getChildNodes().item(i);
516 
517  if ((node.getNodeType() == ELEMENT_TYPE)
518  && (elementNodeIndex++ == searchIndex)) {
519  break;
520  }
521  }
522  return new NXNodeMapper(node, false, node.getNodeName());
523  }
524 
525  }
526 
527  public int getChildCount() {
528 
529  if (isRoot) {
530 
531  return documents.size();
532 
533 
534 
535  }
536 
537  return childCount;
538 
539 
540 
541  }
542 
543  public Enumeration children() {
544  return new children();
545 
546  }
547 
548  public boolean isLeaf() {
549  return isLeaf;
550  }
551 
552  public boolean getAllowsChildren() {
553  return allowsChildren;
554  }
555 
556  public TreeNode getParent() {
557 
558  if (isRoot) {
559  return null;
560  } else if (isDocument) {
561 
562  return root;
563 
564  } else if (domNode.getParentNode() != null) {
565 
566  if (domNode.getParentNode().getNodeType() == domNode.DOCUMENT_NODE) {
567 
568  Document doc = (Document) domNode.getParentNode();
569 
570  return new NXNodeMapper(domNode.getParentNode(), true,
571  ((File) doc.getUserData("file")).getAbsolutePath());
572 
573  } else {
574 
575  return new NXNodeMapper(domNode.getParentNode(), false,
576  domNode.getParentNode().getNodeName());
577  }
578  } else {
579  return null;
580  }
581 
582  }
583 
585 
586  if(domNode==null){
587  return null;
588  }
589 
590  Node tempNode = domNode.getParentNode();
591 
592  boolean gotRoot = false;
593  NXNodeMapper rootNode = null;
594 
595  if (isRoot) {
596  return null;
597  } else if (isDocument) {
598 
599  return this;
600 
601  } else if (tempNode != null) {
602 
603  while(!gotRoot){
604 
605  if (tempNode.getNodeType() == domNode.DOCUMENT_NODE) {
606 
607  Document doc = (Document) tempNode;
608 
609  rootNode = new NXNodeMapper(tempNode, true,
610  ((File) doc.getUserData("file")).getAbsolutePath());
611  gotRoot = true;
612  }
613  else{
614  tempNode = tempNode.getParentNode();
615  }
616  }
617 
618  } else {
619  return null;
620  }
621 
622  return rootNode;
623 
624  }
625 
632  public String[] getAttributeList() {
633 
634  if (isRoot) {
635  return new String[0];
636 
637 
638  }
639 
640  ArrayList<String> atts = new ArrayList<String>();
641 
642 
643 
644  if (domNode.hasAttributes()) {
645 
646  NamedNodeMap att = domNode.getAttributes();
647 
648 
649  int na = domNode.getAttributes().getLength();
650 
651 
652 
653  for (int i = 0; i
654  < na;
655  ++i) {
656  atts.add(att.item(i).getNodeName() + " = "
657  + att.item(i).getNodeValue());
658 
659 
660  }
661 
662  }
663  return atts.toArray(new String[0]);
664  }
665 
671  public String getValue() {
672 
673  if (isRoot) {
674  return "";
675 
676 
677  }
678 
679  if (domNode.getNodeType() == ELEMENT_TYPE) {
680  return getTextValue(domNode);
681 
682 
683  }
684 
685  if (domNode.getTextContent() != null) {
686  return domNode.getTextContent().trim();
687 
688 
689  }
690 
691  return "";
692 
693 
694 
695  }
696 
697  private ArrayList<Node> getElements() {
698 
699  ArrayList<Node> nodes = new ArrayList<Node>();
700 
701 
702 
703  for (int i = 0; i
704  < domNode.getChildNodes().getLength(); i++) {
705  Node node = domNode.getChildNodes().item(i);
706 
707 
708 
709  if (node.getNodeType() == ELEMENT_TYPE) {
710  nodes.add(node);
711 
712 
713  ++childCount;
714 
715 
716  }
717  }
718  return nodes;
719 
720 
721  }
722 
723  private String getTextValue(Node node) {
724 
725  if (node.hasChildNodes()) {
726 
727  NodeList nodes = node.getChildNodes();
728 
729 
730 
731  for (int i = 0; i
732  < nodes.getLength();
733  ++i) {
734  if (nodes.item(i).getNodeType() == Node.TEXT_NODE) {
735  return nodes.item(i).getTextContent().trim();
736 
737 
738  }
739  }
740 
741  }
742  return "";
743 
744 
745  }
746 
752  public ArrayList<NXNodeMapper> getOpenNodes() {
753  return documents;
754  }
755 
760  public void removeAllNodes() {
761  documents.clear();
762  }
763 
764 
769  public boolean hasBadChildren(){
770  TreeUtils treeUtils = new TreeUtils();
771  return treeUtils.hasBadChildren(this);
772  }
773 
774 
779  private class children implements Enumeration {
780 
781  private int count = 0;
782  private boolean more = true;
783  private Node node = null;
784  private NXNodeMapper nxNode = null;
785 
786  public boolean hasMoreElements() {
787 
788  if (isRoot) {
789 
790  if (documents == null) {
791  more = false;
792  return more;
793  }
794 
795  if (count < documents.size()) {
796  more = true;
797  } else {
798  more = false;
799  }
800  } else {
801 
802  if (children == null) {
803  more = false;
804  return more;
805  }
806 
807  if (count < children.size()) {
808  more = true;
809  } else {
810  more = false;
811  }
812 
813  }
814  return more;
815  }
816 
817  public Object nextElement() {
818 
819  if (isRoot) {
820 
821  if (count < documents.size()) {
822  nxNode = documents.get(count);
823  count++;
824  return nxNode;
825  } else {
826  throw new NoSuchElementException();
827  }
828 
829  } else {
830 
831  if (count < children.size()) {
832  node = children.get(count);
833  count++;
834  return new NXNodeMapper(node, false, node.getNodeName());
835  } else {
836  throw new NoSuchElementException();
837  }
838 
839  }
840  }
841  }
842 
843  public void insert(MutableTreeNode child, int index) {
844 
845  NXNodeMapper childNode = (NXNodeMapper) child;
846 
847 
848 
849  if (isRoot) {
850  documents.add(index, childNode);
851 
852 
853  } else {
854  NodeList list = domNode.getChildNodes();
855 
856 
857 
858  for (int i = 0; i
859  < list.getLength();
860  ++i) {
861  if (i == index) {
862  domNode.insertBefore(childNode.domNode, list.item(i));
863 
864 
865  }
866  }
867  }
868  }
869 
870  public void remove(int index) {
871  if (isRoot) {
872 
873  documents.remove(index);
874 
875 
876 
877  } else {
878 
879  NodeList list = domNode.getChildNodes();
880 
881 
882 
883  for (int i = 0; i
884  < list.getLength();
885  ++i) {
886  if (i == index) {
887  domNode.removeChild(list.item(index));
888 
889 
890  }
891  }
892 
893  }
894  }
895 
896  public void remove(MutableTreeNode node) {
897 
898  NXNodeMapper childNode = (NXNodeMapper) node;
899 
900 
901 
902  if (isRoot) {
903  documents.remove((NXNodeMapper) node);
904 
905 
906  } else {
907  NodeList list = domNode.getChildNodes();
908 
909 
910 
911  for (int i = 0; i
912  < list.getLength();
913  ++i) {
914  if (list.item(i).isSameNode(childNode.domNode)) {
915  domNode.removeChild(childNode.domNode);
916 
917 
918  }
919  }
920  }
921  }
922 
923  public void removeFromParent() {
924  if (isRoot) {
925  return;
926 
927 
928  } else {
929  NXNodeMapper parentNode = (NXNodeMapper) getParent();
930  parentNode.remove(this);
931 
932 
933  }
934  }
935 
936  public void setParent(MutableTreeNode newParent) {
937  }
938 
939  public void setUserObject(Object object) {
940 
941  userObject = object;
942 
943 
944  }
945 }