dmitri 2002/10/19 20:44:52
Modified: jxpath/src/java/org/apache/commons/jxpath/ri/model/jdom
JDOMNodePointer.java
jxpath/src/java/org/apache/commons/jxpath/ri/model/dom
DOMNodePointer.java
Log:
Updated the setValue method
Revision Changes Path
1.3 +129 -33
jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/model/jdom/JDOMNodePointer.java
Index: JDOMNodePointer.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/model/jdom/JDOMNodePointer.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- JDOMNodePointer.java 13 Oct 2002 02:59:02 -0000 1.2
+++ JDOMNodePointer.java 20 Oct 2002 03:44:51 -0000 1.3
@@ -81,6 +81,7 @@
import org.jdom.Attribute;
import org.jdom.CDATA;
import org.jdom.Comment;
+import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.ProcessingInstruction;
@@ -139,7 +140,11 @@
public String getNamespaceURI(){
if (node instanceof Element){
Element element = (Element)node;
- return element.getNamespaceURI();
+ String ns = element.getNamespaceURI();
+ if (ns != null && ns.equals("")){
+ ns = null;
+ }
+ return ns;
}
return null;
}
@@ -148,7 +153,6 @@
if (node instanceof Element){
Element element = (Element)node;
Namespace ns = element.getNamespace(prefix);
-// System.err.println("PREFIX: " + prefix + " NS: " + ns);
if (ns == null){
return null;
}
@@ -215,6 +219,23 @@
return node;
}
+ public boolean isCollection(){
+ return false;
+ }
+
+ public int getLength(){
+ return 1;
+ }
+
+ public boolean isLeaf(){
+ if (node instanceof Element){
+ return ((Element)node).getContent().size() == 0;
+ }
+ else if (node instanceof Document){
+ return ((Document)node).getContent().size() == 0;
+ }
+ return true;
+ }
/**
* @see org.apache.commons.jxpath.ri.model.NodePointer#getName()
@@ -277,17 +298,44 @@
/**
* @see org.apache.commons.jxpath.Pointer#setValue(Object)
*/
- public void setValue(Object value) {
- String string = null;
- if (value != null){
- string = (String)TypeUtils.convert(value, String.class);
- if (string.equals("")){
- string = null;
- }
- }
+// String string = null;
+// if (value != null){
+// string = (String)TypeUtils.convert(value, String.class);
+// if (string.equals("")){
+// string = null;
+// }
+// }
+//
+// if (node instanceof Text){
+// if (string != null){
+// ((Text)node).setText(string);
+// }
+// else {
+// nodeParent(node).removeContent((Text)node);
+// }
+// }
+// else {
+// Element element = (Element)node;
+// // First remove all text from the element
+// List content = new ArrayList(element.getContent());
+// for (int i = content.size(); --i >= 0;){
+// Object child = content.get(i);
+// if (child instanceof Text){
+// element.removeContent((Text)node);
+// }
+// else if (child instanceof CDATA){
+// element.removeContent((CDATA)node);
+// }
+// }
+// if (string != null){
+// element.addContent(new Text(string));
+// }
+// }
+ public void setValue(Object value) {
if (node instanceof Text){
- if (string != null){
+ String string = (String)TypeUtils.convert(value, String.class);
+ if (string != null && !string.equals("")){
((Text)node).setText(string);
}
else {
@@ -295,24 +343,69 @@
}
}
else {
- Element element = (Element)node;
- // First remove all text from the element
- List content = new ArrayList(element.getContent());
- for (int i = content.size(); --i >= 0;){
- Object child = content.get(i);
- if (child instanceof Text){
- element.removeContent((Text)node);
- }
- else if (child instanceof CDATA){
- element.removeContent((CDATA)node);
- }
- }
- if (string != null){
- element.addContent(new Text(string));
- }
- }
+ Element element = (Element)node;
+ element.getContent().clear();
+
+ if (value instanceof Element){
+ Element valueElement = (Element)value;
+ addContent(valueElement.getContent());
+ }
+ else if (value instanceof Document){
+ Document valueDocument = (Document)value;
+ addContent(valueDocument.getContent());
+ }
+ else if (value instanceof Text ||
+ value instanceof CDATA){
+ String string = ((Text)value).getText();
+ element.addContent(new Text(string));
+ }
+ else if (value instanceof ProcessingInstruction){
+ ProcessingInstruction pi = (ProcessingInstruction)
+ ((ProcessingInstruction)value).clone();
+ element.addContent(pi);
+ }
+ else if (value instanceof Comment){
+ Comment comment = (Comment)((Comment)value).clone();
+ element.addContent(comment);
+ }
+ else {
+ String string = (String)TypeUtils.convert(value, String.class);
+ if (string != null && !string.equals("")){
+ element.addContent(new Text(string));
+ }
+ }
+ }
+ }
+
+ private void addContent(List content){
+ Element element = (Element)node;
+ int count = content.size();
+
+ for (int i = 0; i < count; i++){
+ Object child = content.get(i);
+ if (child instanceof Element){
+ child = ((Element)child).clone();
+ element.addContent((Element)child);
+ }
+ else if (child instanceof Text){
+ child = ((Text)child).clone();
+ element.addContent((Text)child);
+ }
+ else if (node instanceof CDATA){
+ child = ((CDATA)child).clone();
+ element.addContent((CDATA)child);
+ }
+ else if (node instanceof ProcessingInstruction){
+ child = ((ProcessingInstruction)child).clone();
+ element.addContent((ProcessingInstruction)child);
+ }
+ else if (node instanceof Comment){
+ child = ((Comment)child).clone();
+ element.addContent((Comment)child);
+ }
+ }
}
-
+
public boolean testNode(NodeTest test){
return testNode(this, node, test);
}
@@ -536,7 +629,10 @@
// the parent's responsibility to produce the node test part
// of the path
if (parent instanceof JDOMNodePointer){
- buffer.append('/');
+ if (buffer.length() == 0 ||
+ buffer.charAt(buffer.length()-1) != '/'){
+ buffer.append('/');
+ }
buffer.append(getName());
buffer.append('[');
buffer.append(getRelativePositionByName());
1.11 +89 -49
jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/model/dom/DOMNodePointer.java
Index: DOMNodePointer.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/model/dom/DOMNodePointer.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- DOMNodePointer.java 13 Oct 2002 02:59:02 -0000 1.10
+++ DOMNodePointer.java 20 Oct 2002 03:44:52 -0000 1.11
@@ -100,8 +100,10 @@
private String defaultNamespace;
private String id;
- public static final String XML_NAMESPACE_URI =
"http://www.w3.org/XML/1998/namespace";
- public static final String XMLNS_NAMESPACE_URI =
"http://www.w3.org/2000/xmlns/";
+ public static final String XML_NAMESPACE_URI =
+ "http://www.w3.org/XML/1998/namespace";
+ public static final String XMLNS_NAMESPACE_URI =
+ "http://www.w3.org/2000/xmlns/";
public DOMNodePointer(Node node, Locale locale){
super(null, locale);
@@ -123,7 +125,9 @@
return testNode(this, node, test);
}
- public static boolean testNode(NodePointer pointer, Node node, NodeTest test){
+ public static boolean testNode(
+ NodePointer pointer, Node node, NodeTest test)
+ {
if (test == null){
return true;
}
@@ -134,7 +138,8 @@
QName testName = ((NodeNameTest)test).getNodeName();
String testLocalName = testName.getName();
- if (testLocalName.equals("*") ||
testLocalName.equals(DOMNodePointer.getLocalName(node))){
+ if (testLocalName.equals("*") ||
+ testLocalName.equals(DOMNodePointer.getLocalName(node))){
String testPrefix = testName.getPrefix();
String nodePrefix = DOMNodePointer.getPrefix(node);
if (equalStrings(testPrefix, nodePrefix)){
@@ -345,19 +350,16 @@
}
/**
- * Sets text contents of the node to the specified value
+ * Sets contents of the node to the specified value. If the value is
+ * a String, the contents of the node are replaced with this text.
+ * If the value is an Element or Document, the children of the
+ * node are replaced with the children of the passed node.
*/
public void setValue(Object value){
- String string = null;
- if (value != null){
- string = (String)TypeUtils.convert(value, String.class);
- if (string.equals("")){
- string = null;
- }
- }
-
- if (node.getNodeType() == Node.TEXT_NODE){
- if (string != null){
+ if (node.getNodeType() == Node.TEXT_NODE ||
+ node.getNodeType() == Node.CDATA_SECTION_NODE){
+ String string = (String)TypeUtils.convert(value, String.class);
+ if (string != null && !string.equals("")){
node.setNodeValue(string);
}
else {
@@ -367,37 +369,60 @@
else {
NodeList children = node.getChildNodes();
int count = children.getLength();
- for (int i = count; --i >= 0;){
- Node child = children.item(i);
- if (child.getNodeType() == Node.TEXT_NODE ||
- child.getNodeType() == Node.CDATA_SECTION_NODE){
- node.removeChild(child);
- }
- }
- if (string != null){
- Node text = node.getOwnerDocument().createTextNode(string);
- node.appendChild(text);
- }
- }
- }
-
- public NodePointer createChild(JXPathContext context, QName name, int index){
+ for (int i = count; --i >= 0;){
+ Node child = children.item(i);
+ node.removeChild(child);
+ }
+
+ if (value instanceof Node){
+ Node valueNode = (Node)value;
+ if (valueNode instanceof Element ||
+ valueNode instanceof Document){
+ children = valueNode.getChildNodes();
+ for (int i = 0; i < children.getLength(); i++){
+ Node child = children.item(i);
+ node.appendChild(child.cloneNode(true));
+ }
+ }
+ else {
+ node.appendChild(valueNode.cloneNode(true));
+ }
+ }
+ else {
+ String string = (String)TypeUtils.convert(value, String.class);
+ if (string != null && !string.equals("")){
+ Node textNode =
+ node.getOwnerDocument().createTextNode(string);
+ node.appendChild(textNode);
+ }
+ }
+ }
+ }
+
+ public NodePointer createChild(JXPathContext context,
+ QName name, int index)
+ {
if (index == WHOLE_COLLECTION){
index = 0;
}
- if (!getAbstractFactory(context).createObject(context, this, node,
name.toString(), index)){
- throw new JXPathException("Factory could not create a child node for
path: " +
+ if (!getAbstractFactory(context).
+ createObject(context, this, node, name.toString(), index)){
+ throw new JXPathException(
+ "Factory could not create a child node for path: " +
asPath() + "/" + name + "[" + (index+1) + "]");
}
NodeIterator it = childIterator(new NodeNameTest(name), false, null);
if (it == null || !it.setPosition(index + 1)){
- throw new JXPathException("Factory could not create a child node for
path: " +
+ throw new JXPathException(
+ "Factory could not create a child node for path: " +
asPath() + "/" + name + "[" + (index+1) + "]");
}
return it.getNodePointer();
}
- public NodePointer createChild(JXPathContext context, QName name, int index,
Object value){
+ public NodePointer createChild(JXPathContext context,
+ QName name, int index, Object value)
+ {
NodePointer ptr = createChild(context, name, index);
ptr.setValue(value);
return ptr;
@@ -412,7 +437,8 @@
if (prefix != null){
String ns = getNamespaceURI(prefix);
if (ns == null){
- throw new JXPathException("Unknown namespace prefix: " + prefix);
+ throw new JXPathException(
+ "Unknown namespace prefix: " + prefix);
}
element.setAttributeNS(ns, name.toString(), "");
}
@@ -449,20 +475,27 @@
// the parent's responsibility to produce the node test part
// of the path
if (parent instanceof DOMNodePointer){
- buffer.append('/');
+ if (buffer.length() == 0 ||
+ buffer.charAt(buffer.length()-1) != '/'){
+ buffer.append('/');
+ }
buffer.append(getName());
-
buffer.append('[').append(getRelativePositionByName()).append(']');
+ buffer.append('[');
+ buffer.append(getRelativePositionByName()).append(']');
}
break;
case Node.TEXT_NODE:
case Node.CDATA_SECTION_NODE:
buffer.append("/text()");
-
buffer.append('[').append(getRelativePositionOfTextNode()).append(']');
+ buffer.append('[');
+ buffer.append(getRelativePositionOfTextNode()).append(']');
break;
case Node.PROCESSING_INSTRUCTION_NODE:
String target = ((ProcessingInstruction)node).getTarget();
-
buffer.append("/processing-instruction(\'").append(target).append("')");
-
buffer.append('[').append(getRelativePositionOfPI(target)).append(']');
+ buffer.append("/processing-instruction(\'");
+ buffer.append(target).append("')");
+ buffer.append('[');
+ buffer.append(getRelativePositionOfPI(target)).append(']');
break;
case Node.DOCUMENT_NODE:
// That'll be empty
@@ -473,12 +506,14 @@
private String escape(String string){
int index = string.indexOf('\'');
while (index != -1){
- string = string.substring(0, index) + "'" + string.substring(index
+ 1);
+ string = string.substring(0, index) +
+ "'" + string.substring(index + 1);
index = string.indexOf('\'');
}
index = string.indexOf('\"');
while (index != -1){
- string = string.substring(0, index) + """ + string.substring(index
+ 1);
+ string = string.substring(0, index) +
+ """ + string.substring(index + 1);
index = string.indexOf('\"');
}
return string;
@@ -503,7 +538,8 @@
int count = 1;
Node n = node.getPreviousSibling();
while (n != null){
- if (n.getNodeType() == Node.TEXT_NODE || n.getNodeType() ==
Node.CDATA_SECTION_NODE){
+ if (n.getNodeType() == Node.TEXT_NODE ||
+ n.getNodeType() == Node.CDATA_SECTION_NODE){
count ++;
}
n = n.getPreviousSibling();
@@ -629,12 +665,16 @@
private AbstractFactory getAbstractFactory(JXPathContext context){
AbstractFactory factory = context.getFactory();
if (factory == null){
- throw new JXPathException("Factory is not set on the JXPathContext -
cannot create path: " + asPath());
+ throw new JXPathException(
+ "Factory is not set on the JXPathContext - " +
+ "cannot create path: " + asPath());
}
return factory;
}
- public int compareChildNodePointers(NodePointer pointer1, NodePointer pointer2){
+ public int compareChildNodePointers(
+ NodePointer pointer1, NodePointer pointer2)
+ {
Node node1 = (Node)pointer1.getBaseValue();
Node node2 = (Node)pointer2.getBaseValue();
if (node1 == node2){
--
To unsubscribe, e-mail: <mailto:commons-dev-unsubscribe@;jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@;jakarta.apache.org>