fr.mael.microrss.util.XMLUtil.java Source code

Java tutorial

Introduction

Here is the source code for fr.mael.microrss.util.XMLUtil.java

Source

/*
   Copyright  2013 Mael Le Guvel
   This work is free. You can redistribute it and/or modify it under the
   terms of the Do What The Fuck You Want To Public License, Version 2,
   as published by Sam Hocevar. See the COPYING file for more details.
*/
package fr.mael.microrss.util;

import java.lang.reflect.Method;
import java.util.List;

import javax.xml.bind.JAXBElement;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.sun.org.apache.xerces.internal.dom.ElementNSImpl;

import fr.mael.microrss.xml.atom.ContentType;
import fr.mael.microrss.xml.atom.DateTimeType;
import fr.mael.microrss.xml.atom.LinkType;
import fr.mael.microrss.xml.atom.TextType;

@SuppressWarnings("rawtypes")
@Component
public class XMLUtil {

    @Autowired
    private RssHttpClient client;

    public boolean isAtom(String URL) throws Exception {
        DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
        f.setNamespaceAware(true);
        DocumentBuilder builder = f.newDocumentBuilder();
        HttpGet get = new HttpGet(URL);
        HttpResponse response = client.execute(get);
        try {
            Document doc = builder.parse(response.getEntity().getContent());
            Element e = doc.getDocumentElement();
            return e.getLocalName().equals("feed") && e.getNamespaceURI().equals("http://www.w3.org/2005/Atom");
        } catch (Exception e) {
            EntityUtils.consume(response.getEntity());
            throw new Exception(e);
        }
    }

    public static String readProperty(String name, List<Object> objects) {
        for (Object o : objects) {
            if (o instanceof JAXBElement) {
                JAXBElement element = (JAXBElement) o;
                if (name.equals(element.getName().getLocalPart())) {
                    return String.valueOf(element.getValue());
                }
            } else if (o instanceof ElementNSImpl) {
                ElementNSImpl element = (ElementNSImpl) o;
                if (name.equals(element.getNodeName())) {
                    return element.getFirstChild().getNodeValue();
                }
            }
        }
        return null;
    }

    public static String readComplexProperty(String name, List<Object> objects, String methodName) {
        for (Object o : objects) {
            if (o instanceof JAXBElement) {
                JAXBElement element = (JAXBElement) o;
                if (name.equals(element.getName().getLocalPart())) {
                    return callMethod(element.getValue(), methodName);
                }
            }
        }
        return null;
    }

    public static String readTextType(String name, List<Object> objects) {
        for (Object o : objects) {
            if (o instanceof JAXBElement) {
                JAXBElement element = (JAXBElement) o;
                if (name.equals(element.getName().getLocalPart()) && element.getValue() instanceof TextType) {
                    StringBuffer result = new StringBuffer();
                    TextType textType = (TextType) element.getValue();
                    for (Object content : textType.getContent()) {
                        result.append(content);
                    }
                    return result.toString();

                }
            }
        }
        return null;

    }

    public static String readContentType(String name, List<Object> objects) {
        for (Object o : objects) {
            if (o instanceof JAXBElement) {
                JAXBElement element = (JAXBElement) o;
                if (name.equals(element.getName().getLocalPart()) && element.getValue() instanceof ContentType) {
                    StringBuffer result = new StringBuffer();
                    ContentType contentType = (ContentType) element.getValue();
                    for (Object content : contentType.getContent()) {
                        result.append(content);
                    }
                    return result.toString();

                }
            }
        }
        return null;

    }

    public static String readLinkType(String name, List<Object> objects) {
        for (Object o : objects) {
            if (o instanceof JAXBElement) {
                JAXBElement element = (JAXBElement) o;
                if (name.equals(element.getName().getLocalPart()) && element.getValue() instanceof LinkType) {
                    LinkType textType = (LinkType) element.getValue();
                    return textType.getHref();

                }
            }
        }
        return null;

    }

    public static XMLGregorianCalendar readDateTimeType(String name, List<Object> objects) {
        for (Object o : objects) {
            if (o instanceof JAXBElement) {
                JAXBElement element = (JAXBElement) o;
                if (name.equals(element.getName().getLocalPart()) && element.getValue() instanceof DateTimeType) {
                    DateTimeType dateTimeType = (DateTimeType) element.getValue();
                    return dateTimeType.getValue();

                }
            }
        }
        return null;

    }

    private static String callMethod(Object o, String methodName) {
        try {
            Method method = o.getClass().getMethod(methodName);
            String value = (String) method.invoke(o);
            return value;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}