Example usage for org.jdom2 Element getName

List of usage examples for org.jdom2 Element getName

Introduction

In this page you can find the example usage for org.jdom2 Element getName.

Prototype

public String getName() 

Source Link

Document

Returns the (local) name of the element (without any namespace prefix).

Usage

From source file:net.instantcom.mm7.MM7Message.java

License:Open Source License

/**
 * Loads a correct subclass of a MM7 message by looking at a SOAP request
 * and instantiating an object of related class. Handles SOAP with
 * attachments.//w  w  w.  ja va  2s.  com
 *
 * @param in
 *            input stream to load message from
 * @param contentType
 *            of a request. Can be an multipart or text/xml
 * @param ctx
 *            configuration for loading of message
 *
 * @return an MM7Message instance
 * @throws IOException
 *             if can't deserialize SOAP message or some other IO problem
 *             occurs
 * @throws MM7Error
 *             if SOAP fault is received.
 */
public static MM7Message load(InputStream in, String contentType, MM7Context ctx) throws IOException, MM7Error {
    ContentType ct = new ContentType(contentType);
    BasicContent content = fromStream(in, ct);
    SoapContent soap = null;
    if (content.getParts() != null) {
        String start = (String) ct.getParameter("start");
        if (start != null) {
            if (start.length() == 0) {
                throw new MM7Error("invalid content type, start parameter is empty: " + contentType);
            }
            if (start.charAt(0) == '<') {
                start = start.substring(1, start.length() - 1);
            }
            for (Content c : content.getParts()) {
                if (start.equals(c.getContentId())) {
                    soap = (SoapContent) c;
                    break;
                }
            }
        } else {
            for (Content c : content.getParts()) {
                if (c instanceof SoapContent) {
                    soap = (SoapContent) c;
                    break;
                }
            }
        }
    } else {
        soap = (SoapContent) content;
    }

    // Parse SOAP message to JDOM
    if (soap == null) {
        throw new MM7Error("can't find SOAP parts");
    }
    Document doc = soap.getDoc();

    Element body = doc.getRootElement().getChild("Body", ENVELOPE);
    Element e = (Element) body.getChildren().get(0);
    String message = e.getName();

    // Check if we've got a SOAP fault
    if ("Fault".equals(message)) {
        MM7Error mm7error = new MM7Error();
        mm7error.load(doc.getRootElement());
        throw mm7error;
    }

    // Instantiate a correct message class
    try {
        Class<?> clazz = Class.forName("net.instantcom.mm7." + message);
        MM7Message mm7 = (MM7Message) clazz.newInstance();

        // Load response
        mm7.load(doc.getRootElement());

        // Set content if any
        if (content.getParts() != null && mm7 instanceof HasContent) {
            Element contentElement = e.getChild("Content", e.getNamespace());
            String href = contentElement.getAttributeValue("href", contentElement.getNamespace());
            if (href == null) {
                href = contentElement.getAttributeValue("href");
            }

            // Loop over content, try to match content-location or
            // content-id
            Content payload = null;
            if (href.startsWith("cid:")) {
                // Match by content-id
                String cid = href.substring(4).trim();
                for (Content c : content.getParts()) {
                    if (cid.equals(c.getContentId())) {
                        payload = c;
                        break;
                    }
                }
            } else {
                // Match by content-location
                for (Content c : content.getParts()) {
                    if (href.equals(c.getContentLocation())) {
                        payload = c;
                        break;
                    }
                }
            }
            // We've got a junk message here... try to be a smart cookie and
            // use first non-SOAP part
            if (payload == null) {
                for (Content c : content.getParts()) {
                    if (!(c instanceof SoapContent)) {
                        payload = c;
                        break;
                    }
                }
            }
            if (payload != null) {
                ((HasContent) mm7).setContent(payload);
            }
        }
        return mm7;
    } catch (Throwable t) {
        throw new MM7Error("failed to instantiate message " + message, t);
    }
}

From source file:net.instantcom.mm7.MM7Response.java

License:Open Source License

@Override
public void load(Element element) {
    super.load(element);

    Element body = element.getChild("Body", MM7Message.ENVELOPE);
    Element child = (Element) body.getChildren().get(0);

    // Handle SOAP faults, status will be found in a Fault detail element
    if ("Fault".equals(child.getName())) {
        //child = (Element) child.getChild("detail").getChildren().get(0);
        if (element.getNamespace("") != null) {
            child = (Element) child.getChild("detail", element.getNamespace("")).getChildren().get(0);
        } else {//from w w  w . j a va 2s.com
            child = (Element) child.getChild("detail").getChildren().get(0);
        }
    }

    Element status = child.getChild("Status", namespace);

    if (status != null) {
        setStatusCode(Integer.parseInt(status.getChildTextTrim("StatusCode", namespace)));
        setStatusText(status.getChildTextTrim("StatusText", namespace));
    }
}

From source file:nl.b3p.imro.harvester.parser.ParserFactory.java

License:Open Source License

private static boolean isElementEqual(Element elem1, Element elem2) {
    return elem1.getName().equals(elem2.getName()) && elem1.getNamespaceURI().equals(elem2.getNamespaceURI());
}

From source file:nl.colorize.multimedialib.graphics.ImageAtlasLoader.java

License:Apache License

private void parseImageAtlasXML(Document xml, ImageAtlas imageAtlas) {
    Element imageAtlasElement = xml.getRootElement();
    if (!imageAtlasElement.getName().equals("imageAtlas")) {
        throw new RendererException("XML file does not appear to contain image atlas");
    }//from   www .ja  v a 2s  .com

    for (Element subImageElement : imageAtlasElement.getChildren("subImage")) {
        String name = subImageElement.getAttributeValue("name");
        Rect region = parseRegionElement(subImageElement.getChild("region"));
        imageAtlas.markSubImage(name, region);
    }
}

From source file:nl.colorize.util.xml.PropertyList.java

License:Apache License

private static boolean isPropertyListXmlFile(Document xml) {
    Element rootElement = xml.getRootElement();
    return rootElement.getName().equals("plist") && rootElement.getChildren().size() >= 1
            && rootElement.getAttributeValue("version").equals(PLIST_FILE_FORMAT_VERSION);
}

From source file:nl.colorize.util.xml.PropertyList.java

License:Apache License

private static Object parsePropertyElement(Element element) {
    String value = element.getText();
    switch (getTypeForTag(element.getName(), value)) {
    case STRING:/*from  ww  w  .  ja v  a 2s  .  co m*/
        return value;
    case INTEGER:
        return Integer.parseInt(value);
    case REAL:
        return Float.parseFloat(value);
    case BOOLEAN:
        return "true".equals(element.getName());
    case DATE:
        return parseDate(value);
    case DATA:
        return BaseEncoding.base64Url().decode(value);
    case ARRAY:
        return parseArrayElement(element);
    case DICT:
        return parseDictElement(element);
    default:
        throw new IllegalStateException();
    }
}

From source file:no.imr.stox.functions.utils.JDOMUtils.java

public static List<Element> readXML(String fileName, String rootLevel) {
    try {/*ww  w . j  av a 2s  . c om*/
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build(new File(fileName));
        Element docRoot = doc.getRootElement();
        if (rootLevel == null) {
            rootLevel = docRoot.getName();
        }
        return JDOMUtils.getElementsByTagName(docRoot, rootLevel);
    } catch (JDOMException | IOException ex) {
        Logger.getLogger(JDOMUtils.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

From source file:no.imr.stox.functions.utils.JDOMUtils.java

public static void getDataHeadersFromAttributes(Element elm, String topLevel, Collection<HeaderElm> set) {
    if (elm == null) {
        return;/*from ww w. ja v a  2  s.  com*/
    }
    if (!elm.getName().equals(topLevel)) {
        getDataHeadersFromAttributes(elm.getParentElement(), topLevel, set);
    }
    elm.getAttributes().stream().forEach(a -> {
        set.add(new HeaderElm(elm.getName(), a.getName()));
    });
}

From source file:no.imr.stox.functions.utils.JDOMUtils.java

public static void insertElement(Element elm, int idx, Element source) {
    if (source == null) {
        return;//from   ww w  . j ava  2s.  c  o  m
    }
    insertElement(elm, idx, source.getName(), source.getText());
}

From source file:no.imr.stox.functions.utils.JDOMUtils.java

public static Element getTargetElm(Element elm, String elmLevel) {
    if (elm == null) {
        return null;
    }//from   w ww .  ja  v a  2  s. co  m
    if (elm.getName().equals(elmLevel)) {
        return elm;
    }
    return getTargetElm(elm.getParentElement(), elmLevel);
}