Example usage for org.dom4j Element getQName

List of usage examples for org.dom4j Element getQName

Introduction

In this page you can find the example usage for org.dom4j Element getQName.

Prototype

QName getQName();

Source Link

Document

Returns the QName of this element which represents the local name, the qualified name and the Namespace.

Usage

From source file:com.zimbra.common.soap.SoapTestHarness.java

License:Open Source License

private void checkGlobals(Element e) throws HarnessException {
    if (e.getQName().equals(E_ECHO)) {
        doEcho(e);/* w w  w.  j a  v a  2  s .c om*/
    } else if (e.getQName().equals(E_PROPERTY)) {
        doProperty(e);
    } else if (e.getQName().equals(E_NAMESPACE)) {
        doNamespace(e);
    }
}

From source file:com.zimbra.common.soap.SoapTestHarness.java

License:Open Source License

protected void doTests(Element root) throws HarnessException, IOException, ServiceException {
    if (!root.getQName().equals(E_TESTS))
        throw new HarnessException("root document node must be " + E_TESTS.getQualifiedName());

    for (Element e : root.listElements()) {
        //            e = expandProps(e.createCopy());
        e = expandProps(e);/*from w  w  w .  j a va2 s.c om*/
        if (e.getQName().equals(E_TEST)) {
            if (!doTest(e))
                break;
        } else {
            checkGlobals(e);
        }
    }

    for (Test t : mTests)
        System.out.println(t.getStatus());
}

From source file:com.zimbra.common.soap.SoapTestHarness.java

License:Open Source License

private boolean doTest(Element test) throws IOException, HarnessException, ServiceException {
    mCurrent = new Test();
    mTests.add(mCurrent);/* ww w .  jav  a  2  s .c  om*/
    mCurrent.mTestNum = ++mTestNum;
    mCurrent.mTest = test;

    for (Element e : test.listElements()) {
        if (e.getQName().equals(E_REQUEST)) {
            doRequest(e);
        } else if (e.getQName().equals(E_RESPONSE)) {
            doResponse(e);
        } else {
            checkGlobals(e);
        }
    }

    //System.out.println(mCurrent.getStatus());
    //System.out.println("  Response: " + mDocResponse.getQualifiedName());
    if (mDebug || mCurrent.testFailed() || mCurrent.dumpTest()) {
        System.out.println("----");
        System.out.println(mCurrent.mSoapRequest.prettyPrint());
        System.out.println("----");
        System.out.println(mCurrent.mSoapResponse.prettyPrint());
        System.out.println("----");
    }

    if (!mCurrent.isRequired())
        return true;
    else
        return !mCurrent.testFailed();

    //return !mCurrent.isRequired() || !mCurrent.testFailed();
}

From source file:com.zimbra.common.soap.SoapTestHarness.java

License:Open Source License

private void doSelect(Element context, Element parent)
        throws SoapFaultException, IOException, HarnessException {

    String path = parent.getAttribute(A_PATH, null);
    String attr = parent.getAttribute(A_ATTR, null);
    String match = parent.getAttribute(A_MATCH, null);

    Element se;//ww w  . j a v a  2s . co m
    if (path != null) {
        // FIXME: hacky!
        org.dom4j.Element d4context = context.toXML();
        org.dom4j.XPath xpath = d4context.createXPath(path);
        xpath.setNamespaceURIs(getURIs(mResponseProto));
        org.dom4j.Node node = xpath.selectSingleNode(d4context);
        //System.out.println("path = " + path + " node = " + node);
        if (!(node instanceof org.dom4j.Element)) {
            mCurrent.check(false, "select failed: " + path);
            return;
        } else {
            se = Element.convertDOM((org.dom4j.Element) node);
            mCurrent.check(true, "select ok: " + path);
        }
    } else {
        se = context;
    }

    String value;
    if (attr != null) {
        value = se.getAttribute(attr, null);
    } else {
        value = se.getText();
    }

    if (match != null) {
        boolean ok = Pattern.matches(match, value);
        mCurrent.check(ok, "match " + (ok ? "ok" : "failed") + " (" + match + ")" + " (" + value + ")");
    }

    //System.out.println(se.getText());
    String property = parent.getAttribute(A_SET, null);
    if (property != null) {
        //System.out.println(property+" "+value);
        setProperty(property, value);
    }

    for (Element e : parent.listElements()) {
        if (e.getQName().equals(E_SELECT)) {
            doSelect(se, e);
        } else {
            checkGlobals(e);
        }
    }
}

From source file:com.zimbra.common.soap.SoapTestHarness.java

License:Open Source License

private void doResponse(Element test) throws SoapFaultException, IOException, HarnessException {
    for (Element e : test.listElements()) {
        if (e.getQName().equals(E_SELECT)) {
            doSelect(mCurrent.mDocResponse, e);
        } else {//w  ww.  j  a va2 s.com
            checkGlobals(e);
        }
    }
}

From source file:com.zimbra.cs.client.soap.LmcGetInfoRequest.java

License:Open Source License

protected LmcSoapResponse parseResponseXML(Element responseXML) throws ServiceException {
    HashMap prefMap = new HashMap();
    LmcGetInfoResponse response = new LmcGetInfoResponse();

    // iterate over all the elements we received
    for (Iterator it = responseXML.elementIterator(); it.hasNext();) {
        Element e = (Element) it.next();

        // find out what element it is and go process that
        String elementType = e.getQName().getName();
        if (elementType.equals(AccountConstants.E_NAME)) {
            response.setAcctName(e.getText());
        } else if (elementType.equals(AccountConstants.E_LIFETIME)) {
            response.setLifetime(e.getText());
        } else if (elementType.equals(AccountConstants.E_PREF)) {
            // add this preference to our map
            addPrefToMultiMap(prefMap, e);
        }/*from  ww w. j a  v  a  2s .  co  m*/
    }

    if (!prefMap.isEmpty())
        response.setPrefMap(prefMap);

    return response;
}

From source file:com.zimbra.cs.client.soap.LmcSearchRequest.java

License:Open Source License

protected void parseResponse(LmcSearchResponse response, Element responseXML)
        throws ServiceException, LmcSoapClientException {
    // get the offset and more attributes from the <SearchResponse> element
    response.setOffset(DomUtil.getAttr(responseXML, MailConstants.A_QUERY_OFFSET));
    response.setMore(DomUtil.getAttr(responseXML, MailConstants.A_QUERY_MORE));

    /*/*from  w w  w .j a  v a2s .  c o m*/
     * Iterate through the elements and put them in a generic ArrayList.  
     * XXX: Should validate that the correct types are returned.
     */
    ArrayList mailItems = new ArrayList();
    for (Iterator it = responseXML.elementIterator(); it.hasNext();) {
        Element e = (Element) it.next();

        // find out what element it is and go process that
        String elementType = e.getQName().getName();
        Object o;
        if (elementType.equals(MailConstants.E_CONV)) {
            o = parseConversation(e);
        } else if (elementType.equals(MailConstants.E_MSG)) {
            o = parseMessage(e);
        } else if (elementType.equals(MailConstants.E_MIMEPART)) {
            o = parseMimePart(e);
        } else if (elementType.equals(MailConstants.E_CONTACT)) {
            o = parseContact(e);
        } else if (elementType.equals(MailConstants.E_NOTE)) {
            o = parseNote(e);
        } else if (elementType.equals(MailConstants.E_DOC)) {
            o = parseDocument(e);
        } else if (elementType.equals(MailConstants.E_WIKIWORD)) {
            o = parseWiki(e);
        } else {
            // unknown element type as search result
            throw new LmcSoapClientException("Unexpected element type " + elementType);
        }

        // add the object to the ArrayList
        mailItems.add(o);
    }

    response.setResults(mailItems);
}

From source file:com.zimbra.cs.client.soap.LmcSoapRequest.java

License:Open Source License

protected LmcConversation parseConversation(Element conv) throws LmcSoapClientException, ServiceException {
    LmcConversation result = new LmcConversation();

    // get the conversation attributes
    result.setID(conv.attributeValue(MailConstants.A_ID));
    result.setTags(conv.attributeValue(MailConstants.A_TAGS));
    String numMsgs = conv.attributeValue(MailConstants.A_NUM);
    if (numMsgs != null)
        result.setNumMessages(Integer.parseInt(numMsgs));
    result.setDate(conv.attributeValue(MailConstants.A_DATE));
    result.setFlags(conv.attributeValue(MailConstants.A_FLAGS));
    result.setFolder(conv.attributeValue(MailConstants.A_FOLDER));

    /*/*ww  w.  jav  a 2 s  . c  o  m*/
       * iterate over subelements. allowed subelements are e-mail addresses,
       * subject, fragment, and messages. this assumes no particular order is
       * required for correctness.
       */
    ArrayList emailAddrs = new ArrayList();
    ArrayList msgs = new ArrayList();
    for (Iterator it = conv.elementIterator(); it.hasNext();) {
        Element e = (Element) it.next();

        // find out what element it is and go process that
        String elementType = e.getQName().getName();
        if (elementType.equals(MailConstants.E_FRAG)) {
            // fragment
            result.setFragment(e.getText());
        } else if (elementType.equals(MailConstants.E_EMAIL)) {
            // e-mail address
            LmcEmailAddress ea = parseEmailAddress(e);
            emailAddrs.add(ea);
        } else if (elementType.equals(MailConstants.E_SUBJECT)) {
            // subject
            result.setSubject(e.getText());
        } else if (elementType.equals(MailConstants.E_MSG)) {
            // message
            LmcMessage m = parseMessage(e);
            msgs.add(m);
        } else {
            // don't know what it is
            throw new LmcSoapClientException("unknown element " + elementType + " within conversation");
        }
    }

    // set the arrays in the result object
    if (!emailAddrs.isEmpty()) {
        LmcEmailAddress a[] = new LmcEmailAddress[emailAddrs.size()];
        result.setParticipants((LmcEmailAddress[]) emailAddrs.toArray(a));
    }
    if (!msgs.isEmpty()) {
        LmcMessage m[] = new LmcMessage[msgs.size()];
        result.setMessages((LmcMessage[]) msgs.toArray(m));
    }
    return result;
}

From source file:com.zimbra.cs.client.soap.LmcSoapRequest.java

License:Open Source License

protected LmcMessage parseMessage(Element msg) throws ServiceException, LmcSoapClientException {
    LmcMessage result = new LmcMessage();

    // get the message attributes
    result.setID(DomUtil.getAttr(msg, MailConstants.A_ID));
    result.setFlags(msg.attributeValue(MailConstants.A_FLAGS));
    String size = msg.attributeValue(MailConstants.A_SIZE);
    if (size != null)
        result.setSize(Integer.parseInt(size));
    result.setContentMatched(msg.attributeValue(MailConstants.A_CONTENTMATCHED));
    result.setDate(msg.attributeValue(MailConstants.A_DATE));
    result.setConvID(msg.attributeValue(MailConstants.A_CONV_ID));
    result.setFolder(msg.attributeValue(MailConstants.A_FOLDER));
    result.setOriginalID(msg.attributeValue(MailConstants.A_ORIG_ID));

    /*/* ww  w .ja v  a 2  s . c om*/
       * iterate over subelements. allowed subelements are content (at most
       * 1), subject, fragment, and msg parts. this assumes no particular
       * order is required for correctness.
       */
    ArrayList emailAddrs = new ArrayList();
    for (Iterator it = msg.elementIterator(); it.hasNext();) {
        Element e = (Element) it.next();

        // find out what element it is and go process that
        String elementType = e.getQName().getName();
        if (elementType.equals(MailConstants.E_FRAG)) {
            // fragment
            result.setFragment(e.getText());
        } else if (elementType.equals(MailConstants.E_EMAIL)) {
            // e-mail address
            LmcEmailAddress ea = parseEmailAddress(e);
            emailAddrs.add(ea);
        } else if (elementType.equals(MailConstants.E_SUBJECT)) {
            // subject
            result.setSubject(e.getText());
        } else if (elementType.equals(MailConstants.E_MIMEPART)) {
            // MIME part
            LmcMimePart mp = parseMimePart(e);
            result.addMimePart(mp);
        } else if (elementType.equals(MailConstants.E_MSG_ID_HDR)) {
            // message ID header
            result.setMsgIDHeader(e.getText());
        } else if (elementType.equals(MailConstants.E_INVITE)) {
            // ignore appointment invites for now
        } else {
            // don't know what it is
            throw new LmcSoapClientException("unknown element " + elementType + " within message");
        }
    }

    if (!emailAddrs.isEmpty()) {
        LmcEmailAddress a[] = new LmcEmailAddress[emailAddrs.size()];
        result.setEmailAddresses((LmcEmailAddress[]) emailAddrs.toArray(a));
    }

    return result;
}

From source file:com.zimbra.cs.client.soap.LmcSoapRequest.java

License:Open Source License

protected LmcMimePart parseMimePart(Element mp) throws LmcSoapClientException, ServiceException {
    LmcMimePart result = new LmcMimePart();

    // get the attributes
    result.setPartName(DomUtil.getAttr(mp, MailConstants.A_PART));
    result.setIsBody(mp.attributeValue(MailConstants.A_BODY));
    result.setSize(mp.attributeValue(MailConstants.A_SIZE));
    result.setMessageID(mp.attributeValue(MailConstants.A_MESSAGE_ID));
    result.setConvID(mp.attributeValue(MailConstants.A_CONV_ID));
    result.setContentType(mp.attributeValue(MailConstants.A_CONTENT_TYPE));
    result.setContentTypeName(mp.attributeValue(MailConstants.A_CONTENT_NAME));
    result.setContentDisp(mp.attributeValue(MailConstants.A_CONTENT_DISPOSITION));
    result.setContentDispFilename(mp.attributeValue(MailConstants.A_CONTENT_FILENAME));
    // XXX assume that content description is an attr of <mp> and not <content>
    result.setContentDesc(mp.attributeValue(MailConstants.A_CONTENT_DESCRIPTION));

    // parse any interior elements (content or another MIME part)
    ArrayList subMimeParts = new ArrayList();
    for (Iterator it = mp.elementIterator(); it.hasNext();) {
        Element e = (Element) it.next();

        // find out what element it is and go process that
        String elementType = e.getQName().getName();
        if (elementType.equals(MailConstants.E_CONTENT)) {
            addContent(result, e);/* w ww .  j a  va 2s.  c  o  m*/
        } else if (elementType.equals(MailConstants.E_MIMEPART)) {
            LmcMimePart nextPart = parseMimePart(e);
            subMimeParts.add(nextPart);
        } else {
            // unexpected element
            throw new LmcSoapClientException("unexpected element " + elementType);
        }
    }

    if (!subMimeParts.isEmpty()) {
        LmcMimePart mpArr[] = new LmcMimePart[subMimeParts.size()];
        result.setSubParts((LmcMimePart[]) subMimeParts.toArray(mpArr));
    }
    return result;
}