isxmlfilter.java

来自「提供ESB 应用mule源代码 提供ESB 应用mule源代码」· Java 代码 · 共 82 行

JAVA
82
字号
/* * $Id: IsXmlFilter.java 12422 2008-07-29 19:28:48Z tcarlson $ * -------------------------------------------------------------------------------------- * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com * * The software in this package is published under the terms of the CPAL v1.0 * license, a copy of which has been included with this distribution in the * LICENSE.txt file. */package org.mule.module.xml.filters;import org.mule.api.MuleMessage;import org.mule.api.routing.filter.Filter;import org.mule.module.xml.util.XMLUtils;import javax.xml.stream.XMLInputFactory;import javax.xml.stream.XMLStreamConstants;import javax.xml.stream.XMLStreamException;import javax.xml.stream.XMLStreamReader;/** * <code>IsXmlFilter</code> accepts a String or byte[] if its contents are valid * (well-formed) XML. */// @ThreadSafepublic class IsXmlFilter implements Filter{    private final XMLInputFactory factory = XMLInputFactory.newInstance();    // TODO: add validation against a DTD, see MULE-1055    public IsXmlFilter()    {        super();    }    public boolean accept(MuleMessage obj)    {        return accept(obj.getPayload());    }    private boolean accept(Object obj)    {        XMLStreamReader parser = null;        try        {            parser = XMLUtils.toXMLStreamReader(factory, obj);            if (parser == null)            {                return false;            }            while (parser.next() != XMLStreamConstants.END_DOCUMENT)            {                // meep meep!            }            return true;        }        catch (XMLStreamException ex)        {            return false;        }        finally        {            if (parser != null)            {                try                {                    parser.close();                }                catch (XMLStreamException ignored)                {                    // bummer                }            }        }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?