Java Code Examples for org.jdom2.Element#clone()

The following examples show how to use org.jdom2.Element#clone() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ResourceManager.java    From yawl with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * updates the input datalist with the changed data in the output datalist
 *
 * @param in  - the JDOM Element containing the input params
 * @param out - the JDOM Element containing the output params
 * @return a JDOM Element with the data updated
 */
private Element updateOutputDataList(Element in, Element out) {

    // get a copy of the 'in' list
    Element result = in.clone();

    // for each child in 'out' list, get its value and copy to 'in' list
    for (Element e : out.getChildren()) {

        // if there's a matching 'in' data item, update its value
        Element resData = result.getChild(e.getName());
        if (resData != null) {
            if (resData.getContentSize() > 0) resData.setContent(e.cloneContent());
            else resData.setText(e.getText());
        } else {
            result.addContent(e.clone());
        }
    }

    return result;
}
 
Example 2
Source File: XQueryEvaluator.java    From yawl with GNU Lesser General Public License v3.0 6 votes vote down vote up
public Element execute(Element inData, List<YParameter> inParams,
                       List<YParameter> outParams) throws CodeletExecutionException {

    if (outParams == null) {
       String msg = "Cannot perform evaluation: Missing or empty output parameter list.";
       throw new CodeletExecutionException(msg) ;
    }

    this.setInputs(inData, inParams, outParams);

    // convert input vars to a Doc
    Document dataDoc = new Document(inData.clone());

    String query = (String) getParameterValue("query");
    String output = evaluateQuery(query, dataDoc);

    setParameterValue("result", output);
    return getOutputData() ;
}
 
Example 3
Source File: Marshaller.java    From yawl with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static String getMergedOutputData(Element inputData, Element outputData) {
    try {
        Element merged = inputData.clone();
        JDOMUtil.stripAttributes(merged);
        JDOMUtil.stripAttributes(outputData);

        List<Element> children = outputData.getChildren();

        // iterate through the output vars and add them to the merged doc.
        for (int i = children.size() - 1; i >= 0; i--) {
            Element child = children.get(i);

            // the input data will be removed from the merged doc and
            // the output data will be added.
            merged.removeChild(child.getName());
            merged.addContent(child.detach());
        }
        return JDOMUtil.elementToString(merged);
    }
    catch (Exception e) {
        return "";
    }
}
 
Example 4
Source File: WorkletService.java    From yawl with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * updates the input datalist with the changed data in the output datalist
 *
 * @param in  - the JDOM Element containing the input params
 * @param out - the JDOM Element containing the output params
 * @return a JDOM Element with the data updated
 */
public Element updateDataList(Element in, Element out) {

    // get a copy of the 'in' list   	
    Element result = in.clone();

    // for each child in 'out' list, get its value and copy to 'in' list
    for (Element e : out.getChildren()) {

        // if there's a matching 'in' data item, update its value
        Element resData = result.getChild(e.getName());
        if (resData != null) {
            if (resData.getContentSize() > 0) resData.setContent(e.cloneContent());
            else resData.setText(e.getText());
        } else {
            // if the item is not in the 'in' list, add it.
            result.getChildren().add(e.clone());
        }
    }

    return result;
}
 
Example 5
Source File: MCRXSLInfoServlet.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
private void listTemplates() {
    List<Element> list = xsl.getChildren("template", MCRConstants.XSL_NAMESPACE);
    IteratorIterable<Element> callTemplateElements = xsl
        .getDescendants(Filters.element("call-template", MCRConstants.XSL_NAMESPACE));
    LinkedList<Element> templates = new LinkedList<>(list);
    HashSet<String> callNames = new HashSet<>();
    for (Element callTemplate : callTemplateElements) {
        String name = callTemplate.getAttributeValue("name");
        if (callNames.add(name)) {
            templates.add(callTemplate);
        }
    }
    for (Element template : templates) {
        Element copy = template.clone();
        copy.removeContent();
        this.templates.add(copy);
    }
}
 
Example 6
Source File: MCRIncludeHandler.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
private void handleModify(Map<String, Element> cache, Element element) {
    if ("modify".equals(element.getName())) {
        String refID = element.getAttributeValue(ATTR_REF);
        if (refID == null) {
            throw new MCRUsageException("<xed:modify /> must have a @ref attribute!");
        }

        Element container = cache.get(refID);
        if (container == null) {
            LOGGER.warn("Ignoring xed:modify of " + refID + ", no component with that @id found");
            return;
        }

        container = container.clone();

        String newID = element.getAttributeValue(ATTR_ID);
        if (newID != null) {
            container.setAttribute(ATTR_ID, newID); // extend rather that modify
            LOGGER.debug("extending " + refID + " to " + newID);
        } else {
            LOGGER.debug("modifying " + refID);
        }

        for (Element command : element.getChildren()) {
            String commandType = command.getName();
            if ("remove".equals(commandType)) {
                handleRemove(container, command);
            } else if ("include".equals(commandType)) {
                handleInclude(container, command);
            }
        }
        cacheComponent(cache, container);
    }
}
 
Example 7
Source File: SaxonUtil.java    From yawl with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Evaluates an XQuery against a data document
 * @param query the XQuery to evaluate
 * @param dataElem a JDOM Element containing the data tree
 * @return a List containing the Element(s) resulting from the evaluation
 * @throws SaxonApiException if there's a problem with the XQuery or Element
 */
public static List<Content> evaluateListQuery(String query, Element dataElem)
        throws SaxonApiException {

    // put the element in a jdom document
    Document dataDoc = new Document(dataElem.clone());
    String result = evaluateQuery(query, dataDoc);

    // use the string result to create a doc to get it expressed as an element list
    Document resultDoc = JDOMUtil.stringToDocument(StringUtil.wrap(result, "root"));
    return resultDoc.getRootElement().cloneContent();
}
 
Example 8
Source File: EngineClient.java    From yawl with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Maps the values of the data attributes in the datalist of a
 * checked out workitem to the input params of the worklet case that will
 * run as a substitute for the checked out workitem.
 * The input params for the worklet case are required by the interface's
 * launchcase() method.
 *
 * @param wir the checked out work item
 * @return the loaded input params of the new worklet case
 *         (launchCase() requires the input params as a String)
 */
public String mapItemParamsToWorkletCaseParams(WorkItemRecord wir,
                                               Element itemData,
                                               YSpecificationID workletSpecID) {

    if (itemData == null) itemData = wir.getDataList();     // get datalist of work item
    Element wlData = new Element(workletSpecID.getUri());   // new datalist for worklet
    List<YParameter> inParams = getInputParams(workletSpecID);  // worklet input params

    // if worklet has no net-level inputs, or workitem has no datalist, we're done
    if ((inParams == null) || (itemData == null)) return null;

    // extract the name of each worklet input param
    for (YParameter param : inParams) {
        String paramName = param.getName();

        // get the data element of the workitem with the same name as
        // the one for the worklet (assigns null if no match)
        Element wlElem = itemData.getChild(paramName);

        try {
            // if matching element, copy it and add to worklet datalist
            if (wlElem != null) {
                Element copy = wlElem.clone();
                wlData.addContent(copy);
            }

            // no matching data for input param, so add empty element
            else
                wlData.addContent(new Element(paramName));
        } catch (IllegalAddException iae) {
            _log.error("Exception adding content to worklet data list", iae);
        }
    }

    // return the datalist as as string (as required by launchcase)
    return JDOMUtil.elementToString(wlData);
}
 
Example 9
Source File: MCRMetaISO8601Date.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void setFromDOM(Element element) {
    super.setFromDOM(element);
    setFormat(element.getAttributeValue("format"));
    setDate(element.getTextTrim());
    export = element.clone();
}
 
Example 10
Source File: MCRWebsiteWriteProtection.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
public static org.w3c.dom.Document getMessage() throws JDOMException, IOException {
    Element config = getConfiguration();
    if (config == null) {
        return new DOMOutputter().output(new Document());
    } else {
        Element messageElem = config.getChild("message");
        Document message = new Document(messageElem.clone());
        return new DOMOutputter().output(message);
    }
}
 
Example 11
Source File: MCRExtractRelatedItemsEventHandler.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
private Element cloneRelatedItem(Element relatedItem) {
    Element mods = relatedItem.clone();
    mods.setName("mods");
    mods.removeAttribute("type");
    mods.removeAttribute("href", MCRConstants.XLINK_NAMESPACE);
    mods.removeAttribute("type", MCRConstants.XLINK_NAMESPACE);
    mods.removeChildren("part", MCRConstants.MODS_NAMESPACE);
    return mods;
}
 
Example 12
Source File: MCRSetElementText.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
public static MCRChangeData setText(Element element, String text) {
    Element clone = element.clone();

    for (Iterator<Attribute> attributes = clone.getAttributes().iterator(); attributes.hasNext();) {
        attributes.next();
        attributes.remove();
    }

    MCRChangeData data = new MCRChangeData("set-text", clone, 0, element);
    element.setText(text);
    return data;
}
 
Example 13
Source File: MCRBinding.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
public Element cloneBoundElement(int index) {
    Element template = (Element) (boundNodes.get(index));
    Element newElement = template.clone();
    Element parent = template.getParentElement();
    int indexInParent = parent.indexOf(template) + 1;
    parent.addContent(indexInParent, newElement);
    boundNodes.add(index + 1, newElement);
    trackNodeCreated(newElement);
    return newElement;
}
 
Example 14
Source File: MCRBasketEntry.java    From mycore with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Sets the XML data of the object in the basket entry.
 */
public void setContent(Element content) {
    this.content = content.clone();
}
 
Example 15
Source File: MCRCommandLineInterface.java    From mycore with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Reads XML content from URIResolver and sends output to a local file.
 */
public static void getURI(String uri, String file) throws Exception {
    Element resolved = MCRURIResolver.instance().resolve(uri);
    Element cloned = resolved.clone();
    new MCRJDOMContent(cloned).sendTo(new File(file));
}
 
Example 16
Source File: WSIFController.java    From yawl with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Implements InterfaceBWebsideController.  It receives messages from the engine
 * notifying an enabled task and acts accordingly.  In this case it takes the message,
 * tries to check out the work item, and if successful it begins to start up a web service
 * invocation.
 * @param enabledWorkItem
 */
public void handleEnabledWorkItemEvent(WorkItemRecord enabledWorkItem) {
    try {
        if (!checkConnection(_sessionHandle)) {
            _sessionHandle = connect(engineLogonName, engineLogonPassword);
        }
        if (successful(_sessionHandle)) {
            List<WorkItemRecord> executingChildren =
                    checkOutAllInstancesOfThisTask(enabledWorkItem, _sessionHandle);
            for (WorkItemRecord itemRecord : executingChildren) {
                Element inputData = itemRecord.getDataList();
                String wsdlLocation = inputData.getChildText(WSDL_LOCATION_PARAMNAME);
                String portName = inputData.getChildText(WSDL_PORTNAME_PARAMNAME);
                String operationName = inputData.getChildText(WSDL_OPERATIONNAME_PARAMNAME);

                Element webServiceArgsData = inputData.clone();
                webServiceArgsData.removeChild(WSDL_LOCATION_PARAMNAME);
                webServiceArgsData.removeChild(WSDL_PORTNAME_PARAMNAME);
                webServiceArgsData.removeChild(WSDL_OPERATIONNAME_PARAMNAME);

                Map replyFromWebServiceBeingInvoked =
                        WSIFInvoker.invokeMethod(
                                wsdlLocation,
                                portName,
                                operationName,
                                webServiceArgsData,
                                getAuthenticationConfig());

                _log.warn("\n\nReply from Web service being invoked is : {}",
                        replyFromWebServiceBeingInvoked);

                Element caseDataBoundForEngine = prepareReplyRootElement(enabledWorkItem, _sessionHandle);
                Map<String, String> outputDataTypes = getOutputDataTypes(enabledWorkItem);

                for (Object o : replyFromWebServiceBeingInvoked.keySet()) {
                    String varName = (String) o;
                    Object replyMsg = replyFromWebServiceBeingInvoked.get(varName);
                    System.out.println("replyMsg class = " + replyMsg.getClass().getName());
                    String varVal = replyMsg.toString();
                    String varType = outputDataTypes.get(varName);
                    if ((varType != null) && (! varType.endsWith("string"))) {
                        varVal = validateValue(varType, varVal);
                    }
                    Element content = new Element(varName);
                    content.setText(varVal);
                    caseDataBoundForEngine.addContent(content);
                }

                _logger.debug("\nResult of item [" +
                        itemRecord.getID() + "] checkin is : " +
                        checkInWorkItem(
                                itemRecord.getID(),
                                inputData,
                                caseDataBoundForEngine, null,
                                _sessionHandle));
            }
        }

    } catch (Throwable e) {
        _logger.error(e.getMessage(), e);
    }
}
 
Example 17
Source File: MCRMetaAccessRule.java    From mycore with GNU General Public License v3.0 3 votes vote down vote up
/**
 * This method set the condition.
 * 
 * @param condition
 *            the JDOM Element included the condition tree
 * @exception MCRException
 *                if the condition is null or empty
 */
public final void setCondition(Element condition) throws MCRException {
    if (condition == null || !condition.getName().equals("condition")) {
        throw new MCRException("The condition Element of MCRMetaAccessRule is null.");
    }
    this.condition = condition.clone();
}
 
Example 18
Source File: MCRURIResolver.java    From mycore with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Reads XML from URIs of type session:key. The method MCRSession.get( key ) is called and must return a JDOM
 * element.
 *
 * @see org.mycore.common.MCRSession#get(Object)
 * @param href
 *            the URI in the format session:key
 * @return the root element of the xml document
 */
@Override
public Source resolve(String href, String base) throws TransformerException {
    String key = href.substring(href.indexOf(":") + 1);
    LOGGER.debug("Reading xml from session using key {}", key);
    Element value = (Element) MCRSessionMgr.getCurrentSession().get(key);
    return new JDOMSource(value.clone());
}