Package de.innovationgate.wgpublisher.expressions.tmlscript.wgaglobal

Source Code of de.innovationgate.wgpublisher.expressions.tmlscript.wgaglobal.Xml

/*******************************************************************************
* Copyright 2009, 2010 Innovation Gate GmbH. All Rights Reserved.
*
* This file is part of the OpenWGA server platform.
*
* OpenWGA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition, a special exception is granted by the copyright holders
* of OpenWGA called "OpenWGA plugin exception". You should have received
* a copy of this exception along with OpenWGA in file COPYING.
* If not, see <http://www.openwga.com/gpl-plugin-exception>.
*
* OpenWGA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenWGA in file COPYING.
* If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

package de.innovationgate.wgpublisher.expressions.tmlscript.wgaglobal;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.jxpath.JXPathContext;
import org.dom4j.Attribute;
import org.dom4j.CDATA;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Node;
import org.dom4j.Text;
import org.dom4j.io.SAXReader;
import org.xml.sax.SAXException;

import de.innovationgate.ext.org.mozilla.javascript.NativeJavaObject;
import de.innovationgate.webgate.api.WGAPIException;
import de.innovationgate.wga.common.CodeCompletion;

@CodeCompletion()
public class Xml {
   
    public static Document create() {
        return DocumentHelper.createDocument();
    }
   
    public static Document parse(String xml) throws DocumentException {
        // Cutoff eventual content in prolog which may be a problem for the parser (like BOMs, see B00005006)
        int beginTagIndex = xml.indexOf("<");
        if (beginTagIndex != -1) {
            xml = xml.substring(beginTagIndex);
        }
       
        return DocumentHelper.parseText(xml);
    }
   
    public static Object xpath(Object object, String xpath) throws DocumentException {
        Object result;
        if (object instanceof String) {
            Document doc = DocumentHelper.parseText((String) object);
            result = doc.selectObject(xpath);
        }
       
        // Do JXPath on Bean
        else {
            if (object instanceof NativeJavaObject) {
                object = ((NativeJavaObject) object).unwrap();
            }
            JXPathContext jxContext = JXPathContext.newContext(object);
            jxContext.setLenient(true);
            result = jxContext.getValue(xpath);
        }
        return convertXMLObjects(result, false);
    }
   
    public static Object xpathList(Object object, String xpath) throws DocumentException {
        List results;
        if (object instanceof String) {
            Document doc = DocumentHelper.parseText((String) object);
            results = doc.selectNodes(xpath);
        }
       
        // Do JXPath on Bean
        else {
            if (object instanceof NativeJavaObject) {
                object = ((NativeJavaObject) object).unwrap();
            }
            JXPathContext jxContext = JXPathContext.newContext(object);
            jxContext.setLenient(true);
            results = jxContext.selectNodes(xpath);
        }
        return convertXMLObjects(results, true);
    }
   
    public static Document load(String url) throws UnsupportedEncodingException, WGAPIException, IOException, HttpException, SAXException, DocumentException {
        return load(null, url);
    }
   
    public static Document load(HttpClient client, String url) throws UnsupportedEncodingException, WGAPIException, IOException, HttpException, SAXException, DocumentException {
       
        if (client == null) {
            client = new HttpClient();
        }
       
        GetMethod homePageMethod = new GetMethod(url);
        int status = client.executeMethod(homePageMethod);
       
        if (homePageMethod.getStatusCode() != 200) {
            throw new HttpException("HTTP Status Code is " + homePageMethod.getStatusCode());
        }
       
        SAXReader reader = new SAXReader();
        return reader.read(homePageMethod.getResponseBodyAsStream());
       
       
    }

    private static Object convertXMLObjects(Object result, boolean descent) {
       
        if (result instanceof List && descent) {
            List list = (List) result;
            for (int idx=0; idx < list.size(); idx++) {
                list.set(idx,convertXMLObjects(list.get(idx), false));
            }
        }
        if (result instanceof Text || result instanceof CDATA || result instanceof Attribute) {
            return ((Node) result).getText();
        }
        else {
            return result;
        }
    }
   
   
}
TOP

Related Classes of de.innovationgate.wgpublisher.expressions.tmlscript.wgaglobal.Xml

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.