1 10 11 package org.mule.routing.filters.xml; 12 13 import java.io.ByteArrayInputStream ; 14 import java.io.StringReader ; 15 16 import javax.xml.stream.XMLInputFactory; 17 import javax.xml.stream.XMLStreamConstants; 18 import javax.xml.stream.XMLStreamException; 19 import javax.xml.stream.XMLStreamReader; 20 21 import org.mule.umo.UMOFilter; 22 import org.mule.umo.UMOMessage; 23 24 28 public class IsXmlFilter implements UMOFilter 30 { 31 private final XMLInputFactory factory = XMLInputFactory.newInstance(); 32 33 35 public IsXmlFilter() 36 { 37 super(); 38 } 39 40 public boolean accept(UMOMessage obj) 41 { 42 return accept(obj.getPayload()); 43 } 44 45 private boolean accept(Object obj) 46 { 47 XMLStreamReader parser = null; 48 49 try 50 { 51 if (obj instanceof String ) 52 { 53 parser = factory.createXMLStreamReader(new StringReader ((String )obj)); 54 } 55 else if (obj instanceof byte[]) 56 { 57 parser = factory.createXMLStreamReader(new ByteArrayInputStream ((byte[])obj)); 58 } 59 else 60 { 61 return false; 63 } 64 65 while (parser.next() != XMLStreamConstants.END_DOCUMENT) 66 { 67 } 69 70 return true; 71 } 72 catch (XMLStreamException ex) 73 { 74 return false; 75 } 76 finally 77 { 78 if (parser != null) 79 { 80 try 81 { 82 parser.close(); 83 } 84 catch (XMLStreamException ignored) 85 { 86 } 88 } 89 } 90 } 91 92 } 93 | Popular Tags |