1 16 package org.apache.commons.jxpath.ri.model.jdom; 17 18 import java.util.List ; 19 20 import junit.framework.Test; 21 import junit.framework.TestSuite; 22 23 import org.apache.commons.jxpath.AbstractFactory; 24 import org.apache.commons.jxpath.ri.model.XMLModelTestCase; 25 import org.apache.commons.jxpath.xml.DocumentContainer; 26 import org.jdom.Attribute; 27 import org.jdom.CDATA; 28 import org.jdom.Document; 29 import org.jdom.Element; 30 import org.jdom.Text; 31 32 38 39 public class JDOMModelTest extends XMLModelTestCase { 40 45 public JDOMModelTest(String name) { 46 super(name); 47 } 48 49 52 public static Test suite() { 53 return (new TestSuite(JDOMModelTest.class)); 54 } 55 56 protected String getModel() { 57 return DocumentContainer.MODEL_JDOM; 58 } 59 60 public void testGetNode() { 61 assertXPathNodeType(context, "/", Document.class); 62 assertXPathNodeType(context, "/vendor/location", Element.class); 63 assertXPathNodeType(context, "//location/@name", Attribute.class); 64 } 65 66 public void testID() { 67 } 69 70 protected AbstractFactory getAbstractFactory() { 71 return new TestJDOMFactory(); 72 } 73 74 protected String getXMLSignature( 75 Object node, 76 boolean elements, 77 boolean attributes, 78 boolean text, 79 boolean pi) 80 { 81 StringBuffer buffer = new StringBuffer (); 82 appendXMLSignature(buffer, node, elements, attributes, text, pi); 83 return buffer.toString(); 84 } 85 86 private void appendXMLSignature( 87 StringBuffer buffer, 88 Object object, 89 boolean elements, 90 boolean attributes, 91 boolean text, 92 boolean pi) 93 { 94 if (object instanceof Document) { 95 buffer.append("<D>"); 96 appendXMLSignature( 97 buffer, 98 ((Document) object).getContent(), 99 elements, 100 attributes, 101 text, 102 pi); 103 buffer.append("</D"); 104 } 105 else if (object instanceof Element) { 106 String tag = elements ? ((Element) object).getName() : "E"; 107 buffer.append("<"); 108 buffer.append(tag); 109 buffer.append(">"); 110 appendXMLSignature( 111 buffer, 112 ((Element) object).getContent(), 113 elements, 114 attributes, 115 text, 116 pi); 117 buffer.append("</"); 118 buffer.append(tag); 119 buffer.append(">"); 120 } 121 else if (object instanceof Text || object instanceof CDATA) { 122 if (text) { 123 String string = ((Text) object).getText(); 124 string = string.replace('\n', '='); 125 buffer.append(string); 126 } 127 } 128 } 129 130 private void appendXMLSignature( 131 StringBuffer buffer, 132 List children, 133 boolean elements, 134 boolean attributes, 135 boolean text, 136 boolean pi) 137 { 138 for (int i = 0; i < children.size(); i++) { 139 appendXMLSignature( 140 buffer, 141 children.get(i), 142 elements, 143 attributes, 144 text, 145 pi); 146 } 147 } 148 } | Popular Tags |