Changeset 1752 for trunk/docx4j/src
- Timestamp:
- 01/17/12 09:14:49 (4 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/docx4j/src/main/java/org/docx4j/utils/XPathAwareCloner.java
r1751 r1752 30 30 31 31 /** Clone this JAXB object, using default JAXBContext. */ 32 public <T> T deepCopy(T value) {33 return deepCopy( value, Context.jc);32 public Object deepCopy(Object o) { 33 return deepCopy(o, Context.jc); 34 34 } 35 35 … … 41 41 * @return 42 42 */ 43 public <T> T deepCopy(T value, JAXBContext jc) {43 public Object deepCopy(Object o, JAXBContext jc) { 44 44 45 if ( value==null) {45 if (o==null) { 46 46 throw new IllegalArgumentException("Can't clone a null argument"); 47 47 } 48 48 49 49 try { 50 JAXBElement<?> elem;51 Class<?> valueClass;52 if (value instanceof JAXBElement<?>) {53 log.debug("deep copy of JAXBElement..");54 elem = (JAXBElement<?>) value;55 valueClass = elem.getDeclaredType();56 } else {57 log.debug("deep copy of " + value.getClass().getName() );58 @SuppressWarnings("unchecked")59 Class<T> classT = (Class<T>) value.getClass();60 elem = new JAXBElement<T>(new QName("temp"), classT, value);61 valueClass = classT;62 }63 64 50 // To be XPath aware, we need a binder. 65 51 // But to unmarshall using a binder, we need to unmarshal a node. 66 52 // So, our marshall should be to a W3C document 67 org.w3c.dom.Document doc = XmlUtils.marshaltoW3CDomDocument( elem, jc);53 org.w3c.dom.Document doc = XmlUtils.marshaltoW3CDomDocument(o, jc); 68 54 69 55 // OK, unmarshall to binder … … 72 58 eventHandler.setContinue(false); 73 59 binder.setEventHandler(eventHandler); 74 elem = binder.unmarshal( doc, valueClass); 75 76 T res; 77 if (value instanceof JAXBElement<?>) { 78 @SuppressWarnings("unchecked") 79 T resT = (T) elem; 80 res = resT; 81 } else { 82 @SuppressWarnings("unchecked") 83 T resT = (T) elem.getValue(); 84 res = resT; 85 } 86 jaxbElement = res; 87 return res; 60 jaxbElement = binder.unmarshal( doc); 61 62 //log.debug("Clone: " + XmlUtils.marshaltoString(jaxbElement, true, true)); 63 64 return jaxbElement; 88 65 } catch (JAXBException ex) { 89 66 throw new IllegalArgumentException(ex); … … 196 173 197 174 XPathAwareCloner cloner = new XPathAwareCloner(); 198 P clonedP = cloner.deepCopy(pIn);175 P clonedP = (P)cloner.deepCopy(pIn); 199 176 200 177 List<Object> results = cloner.getJAXBNodesViaXPath("//w:r[contains( w:t, 'seeking')]", false); 178 //List<Object> results = cloner.getJAXBNodesViaXPath("//w:r", false); 201 179 202 System.out.println(XmlUtils.marshaltoString(results.get(0), true, true)); 180 int i=1; 181 for (Object result: results) { 182 System.out.println("\n\r" + i + ": " + XmlUtils.marshaltoString(result, true, true)); 183 i++; 184 } 203 185 204 186
Note: See TracChangeset
for help on using the changeset viewer.
