Java org.dom4j.QName 代码实例

・17 分钟阅读

以下是展示如何使用org.dom4j.QName的最佳示例。 我们使用了代码质量辨别算法从开源项目中提取出了最佳的优秀示例。

实例 1


private QName getPdmQName(String sPreFix,String sName)
{
        String uri="attribute";
        // xmlns:a="attribute" xmlns:c="collection" xmlns:o="object">
        if(sPreFix.equals("c"))
                uri="collection";
        else if(sPreFix.equals("o"))
                uri="object";
        return new QName(sName,Namespace.get(sPreFix, uri), sPreFix+':'+sName);
}
 

实例 2


/**
 * Creates a new notification IQ and returns it.
 */
private IQ createNotificationIQ(String apiKey, String title,
                                String message, String uri) {
    Log.info("createNotificationIQ()............ title = " + title);
    Random random = new Random();
    String id = Integer.toHexString(random.nextInt());
    Element notification = DocumentHelper.createElement(QName.get(
            "notification", NOTIFICATION_NAMESPACE));
    notification.addElement("id").setText(id);
    notification.addElement("apiKey").setText(apiKey);
    notification.addElement("title").setText(title);
    notification.addElement("message").setText(message);
    notification.addElement("uri").setText(uri);
    IQ iq = new IQ();
    iq.setType(IQ.Type.set);
    iq.setChildElement(notification);
    return iq;
}
 

实例 3


/**
 * Generates a Result Set Management 'set' element that describes the parto
 * of the result set that was generated. You typically would use the List
 * that was returned by {@link #applyRSMDirectives(Element)} as an argument
 * to this method.
 * 
 * @param returnedResults
 *            The subset of Results that is returned by the current query.
 * @return An Element named 'set' that can be included in the result IQ
 *         stanza, which returns the subset of results.
 */
public Element generateSetElementFromResults(List<E> returnedResults) {
        if (returnedResults == null) {
                throw new IllegalArgumentException(
                                "Argument 'returnedResults' cannot be null.");
        }
        final Element setElement = DocumentHelper.createElement(QName.get(
                        "set", ResultSet.NAMESPACE_RESULT_SET_MANAGEMENT));
        // the size element contains the size of this entire result set.
        setElement.addElement("count").setText(String.valueOf(size()));
        // if the query wasn't a 'count only' query, add two more elements
        if (returnedResults.size() > 0) {
                final Element firstElement = setElement.addElement("first");
                firstElement.addText(returnedResults.get(0).getUID());
                firstElement.addAttribute("index", String
                                .valueOf(indexOf(returnedResults.get(0))));
                setElement.addElement("last").addText(
                                returnedResults.get(returnedResults.size() - 1).getUID());
        }
        return setElement;
}
 

实例 4


@Override
public void toElement(org.dom4j.Element parent) {
    if (parent == null) {
        String msg = "Argument 'parent' cannot be null.";
        throw new IllegalArgumentException(msg);
    }
    org.dom4j.Element elementTestGroup = DocumentHelper.createElement(new QName("test-group"));
    for (IPersonAttributesGroupTestDefinition test : tests) {
        test.toElement(elementTestGroup);
    }
    parent.add(elementTestGroup);
}
 

实例 5


private void readProperty(Element parent, Namespace targetNs, Field field,
        Object value, boolean inlineBlobs) throws IOException {
    Type type = field.getType();
    QName name = QName.get(field.getName().getLocalName(), targetNs.prefix,
            targetNs.uri);
    Element element = parent.addElement(name);
    if (value == null) {
        return; // have no content
    }
    // extract the element content
    if (type.isSimpleType()) {
        element.addText(type.encode(value));
    } else if (type.isComplexType()) {
        ComplexType ctype = (ComplexType) type;
        if (TypeConstants.isContentType(ctype)) {
            readBlob(element, ctype, (Blob) value, inlineBlobs);
        } else {
            readComplex(element, ctype, (Map) value, inlineBlobs);
        }
    } else if (type.isListType()) {
        if (value instanceof List) {
            readList(element, (ListType) type, (List) value, inlineBlobs);
        } else if (value.getClass().getComponentType() != null) {
            readList(element, (ListType) type,
                    PrimitiveArrays.toList(value), inlineBlobs);
        } else {
            throw new IllegalArgumentException(
                    "A value of list type is neither list neither array: "
                            + value);
        }
    }
}
 

实例 6


public void testAuth() {
        String NAMESPACE = "jabber:iq:auth";
        Element probeResponse = DocumentHelper.createElement(QName.get("query", NAMESPACE));
        probeResponse.addElement("username");
        probeResponse.addElement("password");
        probeResponse.addElement("digest");
        System.out.println(probeResponse.asXML());
}
 

实例 7


/**
 * Marshall package core properties to an XML document. Always return
 * <code>true</code>.
 */
public boolean marshall(PackagePart part, OutputStream out)
                throws OpenXML4JException {
        if (!(part instanceof PackagePropertiesPart))
                throw new IllegalArgumentException(
                                "'part' must be a PackagePropertiesPart instance.");
        propsPart = (PackagePropertiesPart) part;
        // Configure the document
        xmlDoc = DocumentHelper.createDocument();
        Element rootElem = xmlDoc.addElement(new QName("coreProperties",
                        namespaceCoreProperties));
        rootElem.addNamespace("cp", PackagePropertiesPart.NAMESPACE_CP_URI);
        rootElem.addNamespace("dc", PackagePropertiesPart.NAMESPACE_DC_URI);
        rootElem.addNamespace("dcterms",
                        PackagePropertiesPart.NAMESPACE_DCTERMS_URI);
        rootElem.addNamespace("xsi", PackagePropertiesPart.NAMESPACE_XSI_URI);
        addCategory();
        addContentStatus();
        addContentType();
        addCreated();
        addCreator();
        addDescription();
        addIdentifier();
        addKeywords();
        addLanguage();
        addLastModifiedBy();
        addLastPrinted();
        addModified();
        addRevision();
        addSubject();
        addTitle();
        addVersion();
        return true;
}
 

实例 8


public static org.dom4j.Element createModsCollectionEl() {
    org.dom4j.Element modsCollection =
            DocumentHelper.createElement(new QName("modsCollection", Namespaces.mods));
    modsCollection.add(Namespaces.mods);
    String modsSchema =
            "https://raw.github.com/moravianlibrary/MEditor/master/resources/xml/mods-3-3.xsd";//"http://www.loc.gov/standards/mods/v3/mods-3-3.xsd";
    modsCollection.addAttribute("schemaLocation", Namespaces.mods.getURI() + " " + modsSchema);
    return modsCollection;
}
 

实例 9


/**
 * Creates a document for the preservation action plan.
 * 
 * @return the document
 */
private Document createPapDoc() {
    Document doc = DocumentHelper.createDocument();
    Element root = doc.addElement(new QName("preservationActionPlan", platoNS));
    root.add(xsi);
    root.add(platoNS);
    root.addAttribute(xsi.getPrefix() + ":schemaLocation", PlanXMLConstants.PLATO_NS + " "
        + PlanXMLConstants.PAP_SCHEMA);
    // Set version of corresponding schema
    root.addAttribute("version", "1.0");
    return doc;
}
 

实例 10


protected static Element addTextElement(Element parent,
        QName newElementName, String value) {
    Element element = parent.addElement(newElementName);
    if (value != null) {
        element.setText(value);
    }
    return element;
}
 

实例 11


public Document dispatchRequest(Document request)
                throws InvalidInputFormatException {
        Namespace ns = new Namespace(NS_PREFIX, NS_URI);
        Element rootElement = request.getRootElement();
       userProcessNamespace = rootElement.getNamespaceURI();
       userProcessPrefix =rootElement.getNamespacePrefix();
        List nodes = DocumentHelper.createXPath("//*").selectNodes(request);
        for (int i = 0; i < nodes.size(); ++i) {
                Element element = (Element) nodes.get(i);
                element.remove(element.getNamespaceForURI(userProcessNamespace));
                element.setQName(new QName(element.getName(),ns));
        }
       rootElement.setQName(new QName("escalateTaskRequest", ns));
        // TODO: fix this in VC!
        return request;
}
 

实例 12


public String getAuthor() {
    if(this.author != null)
        return this.author;
    Element authorElement = this.parent.element(QName.get("author", "itunes"));
    if(authorElement == null)
        return null;
    return this.author = authorElement.getText();
}
 

实例 13


/**
 * Marshall package core properties to an XML document. Always return
 * <code>true</code>.
 */
public boolean marshall(PackagePart part, OutputStream out)
                throws OpenXML4JException {
        if (!(part instanceof PackagePropertiesPart))
                throw new IllegalArgumentException(
                                "'part' must be a PackagePropertiesPart instance.");
        propsPart = (PackagePropertiesPart) part;
        // Configure the document
        xmlDoc = DocumentHelper.createDocument();
        Element rootElem = xmlDoc.addElement(new QName("coreProperties",
                        namespaceCoreProperties));
        rootElem.addNamespace("cp", PackagePropertiesPart.NAMESPACE_CP_URI);
        rootElem.addNamespace("dc", PackagePropertiesPart.NAMESPACE_DC_URI);
        rootElem.addNamespace("dcterms",
                        PackagePropertiesPart.NAMESPACE_DCTERMS_URI);
        rootElem.addNamespace("xsi", PackagePropertiesPart.NAMESPACE_XSI_URI);
        addCategory();
        addContentStatus();
        addContentType();
        addCreated();
        addCreator();
        addDescription();
        addIdentifier();
        addKeywords();
        addLanguage();
        addLastModifiedBy();
        addLastPrinted();
        addModified();
        addRevision();
        addSubject();
        addTitle();
        addVersion();
        return true;
}
 

实例 14


private void addIgnoreExchangeEventProperty(CamelProcessItem item) throws PersistenceException, DocumentException {
        String springContent = item.getSpringContent();
        if (null != springContent && !springContent.isEmpty()) {
                Document document = DocumentHelper.parseText(springContent);
                QName qname = QName.get("bean", SPRING_BEANS_NAMESPACE);
                List<Element> beans = document.getRootElement().elements(qname);
                for (Element bean : beans) {
                        if ("jmxEventNotifier".equals(bean.attributeValue("id")) &&
                                        "org.apache.camel.management.JmxNotificationEventNotifier".equals(bean.attributeValue("class")))
                        {
                                List<Element> properties = bean.elements(QName.get("property", SPRING_BEANS_NAMESPACE));
                                boolean hasIgnore = false;
                                for (Element property : properties) {
                                        List<Attribute> propertyAttributes = property.attributes();
                                        for (Attribute propertyAttribute : propertyAttributes) {
                                                if (propertyAttribute.getValue().equals(IGNORE_EXCHANGE_EVENTS)) {
                                                        hasIgnore = true;
                                                        break;
                                                }
                                        }
                                }
                                if (!hasIgnore)
                                {
                                        DefaultElement ignoreExchangeElement = new DefaultElement("property", bean.getNamespace());
                                        ignoreExchangeElement.add(DocumentHelper.createAttribute(ignoreExchangeElement, "name", IGNORE_EXCHANGE_EVENTS));
                                        ignoreExchangeElement.add(DocumentHelper.createAttribute(ignoreExchangeElement, "value", "true"));
                                        bean.add(ignoreExchangeElement);
                                        item.setSpringContent(document.asXML());
                                        saveItem(item);
                                }
                                break;
                        }
                }
        }
}
 

实例 15


public String createShell() {
    logger.debug("Sending WinRM Create Shell request");
    final Element bodyContent = DocumentHelper.createElement(QName.get("Shell", Namespaces.NS_WIN_SHELL));
    bodyContent.addElement(QName.get("InputStreams", Namespaces.NS_WIN_SHELL)).addText("stdin");
    bodyContent.addElement(QName.get("OutputStreams", Namespaces.NS_WIN_SHELL)).addText("stdout stderr");
    final Document requestDocument = getRequestDocument(Action.WS_ACTION, ResourceURI.RESOURCE_URI_CMD, OptionSet.OPEN_SHELL, bodyContent);
    Document responseDocument = sendRequest(requestDocument, SoapAction.SHELL);
    shellId = getFirstElement(responseDocument, ResponseExtractor.SHELL_ID);
    logger.debug("Received WinRM Create Shell response: shell with ID {} start created", shellId);
    return shellId;
}
 
讨论
淘淘あ西西 profile image