001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.rewriter.util.xml;
006:
007: import com.sun.portal.rewriter.rom.InvalidXMLException;
008: import com.sun.portal.rewriter.util.Constants;
009: import com.sun.portal.rewriter.util.Debug;
010:
011: import java.util.ArrayList;
012: import java.util.List;
013:
014: /**
015: * wrapper to w3c XML Node
016: *
017: * @version 1.0 12/15/2001
018: * @author Raja Nagendra Kumar, Nagendra.Raja@sun.com
019: */
020: public final class Node {
021: private org.w3c.dom.NamedNodeMap nodeMap = null;
022: private String name = "";
023: private String pcData = "";
024: private org.w3c.dom.Node w3cNode;
025: private Document source = null;
026:
027: public Node(final org.w3c.dom.Node aNode) {
028: if (aNode != null) {
029: w3cNode = aNode;
030: source = new Document(aNode.getOwnerDocument());
031: name = aNode.getNodeName();
032: nodeMap = aNode.getAttributes();
033:
034: org.w3c.dom.Node childNode = aNode.getFirstChild();
035: if (childNode != null) {
036: pcData = childNode.getNodeValue();
037: if (pcData == null) {
038: pcData = "";
039: }
040: }
041: }
042: }//constructor
043:
044: public org.w3c.dom.Node getW3CNode() {
045: return w3cNode;
046: }//getW3CNode()
047:
048: /**
049: * Returns the Name of the Tag, this node represents
050: */
051: public String getName() {
052: return name;
053: }//getName()
054:
055: /**
056: Returns the Value of the String present beween the start and end Tags
057: */
058: public String getPCData() {
059: return pcData;
060: }//getValue()
061:
062: public String getAttributeValue(final String aAttName) {
063: if (nodeMap != null) {
064: try {
065: return nodeMap.getNamedItem(aAttName).getNodeValue();
066: } catch (Exception e) {
067: Object[] param = { getW3CNode().getNodeName(), aAttName };
068: /*Debug.warning( "Error: NodeName: " + getW3CNode().getNodeName() +
069: " Attribute Absent: " + aAttName, e );*/
070: Debug.warning("PSRW_CSPR_0039", param);
071: Debug.warning("PSRW_CSPR_0015", e);
072: }//try/catch
073: }
074: return "";
075: }//getAttributeValue()
076:
077: public Node selectNode(final String aXPath)
078: throws InvalidXMLException {
079: final Node[] list = selectNodes(aXPath);
080: Node result = new Node(null);
081:
082: //check if more than one element is present
083: //this is the workarround as we can't enforce this the order in DTD
084: final int nodeLength = list.length;
085: switch (nodeLength) {
086: case 0: {
087: break;
088: }
089: case 1: {
090: result = list[0];
091: break;
092: }
093: default: {
094: throw new InvalidXMLException("More than One Element of '"
095: + aXPath + "' present : ", null, aXPath,
096: InvalidXMLException.REPEATED_TAG);
097: }
098: }//switch/case
099: return result;
100: }//selectNode()
101:
102: public Document getDocument() {
103: return source;
104: }//getDocument()
105:
106: public Node[] selectNodes(final String aXPath) {
107: return Document.selectNodes(source, aXPath);
108: }//selectNodes()
109:
110: public Node[] getChildren(final String aTagName) {
111: return getChildNodes(this , aTagName);
112: }//getChildren()
113:
114: public static Node[] getChildNodes(final Node aNode,
115: final String aTagName) {
116: final List v = new ArrayList();
117: if (aNode.getW3CNode() != null) {
118: org.w3c.dom.NodeList list = aNode.getW3CNode()
119: .getChildNodes();
120: for (int i = 0; i < list.getLength(); i++) {
121: org.w3c.dom.Node lNode = list.item(i);
122: if (lNode.getNodeName().equals(aTagName)) {
123: v.add(new Node(lNode));
124: }
125: }
126: }
127: return (Node[]) v.toArray(Constants.EMPTY_NODE_ARRAY);
128: }//getChildNodes()
129: }//class Node
|