Example usage for org.w3c.dom Element cloneNode

List of usage examples for org.w3c.dom Element cloneNode

Introduction

In this page you can find the example usage for org.w3c.dom Element cloneNode.

Prototype

public Node cloneNode(boolean deep);

Source Link

Document

Returns a duplicate of this node, i.e., serves as a generic copy constructor for nodes.

Usage

From source file:org.wso2.developerstudio.eclipse.esb.impl.MediatorSequenceImpl.java

/**
 * {@inheritDoc}//from  w w  w  .  ja v a 2s. c  o  m
 */
public void doLoad(Element self) throws Exception {
    // Extract the name attribute.
    if (self.hasAttribute("name")) {
        setSequenceName(self.getAttribute("name"));
    } else {
        setAnonymous(true);
    }

    // Extract the on error attribute
    if (self.hasAttribute("onError")) {
        getOnError().setKeyValue(self.getAttribute("onError"));
    }

    Element descriptionElem = getChildElement(self, "description");
    if (null != descriptionElem) {
        if (getDescription() == null) {
            Description description = getEsbFactory().createDescription();
            description.setValue(descriptionElem.getTextContent());
            setDescription(description);
        } else {
            getDescription().setValue(descriptionElem.getTextContent());
        }

    } else {
        //TODO
    }

    //Description tag will be unrecognized since it is not a mediator. So we remove it now and we add description tag manually.
    Node node = null;
    Element elementForMedator = (Element) self.cloneNode(true);
    NodeList list = elementForMedator.getChildNodes();

    for (int i = 0; i < list.getLength(); ++i) {
        node = list.item(i);
        if ("description".equals(node.getLocalName())) {
            elementForMedator.removeChild(node);
            break;
        }
    }

    // Load all the children.
    loadObjects(elementForMedator, Mediator.class, new ObjectHandler<Mediator>() {
        public void handle(Mediator object) {
            getMediators().add(object);
        }
    });

    super.doLoad(self);
}

From source file:sos.scheduler.cron.CronConverter.java

private void createRunTime(final Matcher pcronRegExMatcher, final Element runTimeElement) throws Exception {
    try {//from  w  w  w . j ava  2s. co m
        if (!pcronRegExMatcher.matches()) {
            throw new JobSchedulerException("Fail to parse cron line \"" + strCronLine + "\", regexp is "
                    + pcronRegExMatcher.toString());
        }

        String minutes = pcronRegExMatcher.group(1);
        String hours = pcronRegExMatcher.group(2);
        String days = pcronRegExMatcher.group(3);
        String months = pcronRegExMatcher.group(4);
        String weekdays = pcronRegExMatcher.group(5);

        if (minutes.equals("@reboot")) {
            runTimeElement.setAttribute("once", "yes");
            return;
        }
        Vector<Element> childElements = new Vector<Element>();
        Element periodElement = runTimeElement.getOwnerDocument().createElement("period");

        logger.debug("processing hours [" + hours + "] and minutes [" + minutes + "]");
        if (minutes.startsWith("*")) {
            if (minutes.equalsIgnoreCase("*")) {
                // every minute
                periodElement.setAttribute("repeat", "60");
            } else { // repeat interval is given
                String repeat = minutes.substring(2);
                repeat = formatTwoDigits(repeat);
                periodElement.setAttribute("repeat", "00:" + repeat);
            }
            if (hours.startsWith("*")) {
                if (!hours.equalsIgnoreCase("*")) {
                    // repeat interval is given for hours and minutes. Doesn't make sense.
                    // e.g. */2 */3 every 3 hours repeat every 2 minutes
                    throw new JobSchedulerException(
                            "Combination of minutes and hours not supported: " + minutes + " " + hours);
                }
                // every hour: keep interval from minutes
                childElements.add(periodElement);
            } else {
                logger.debug("Found specific hours, creating periods with begin and end.");
                String[] hourArray = hours.split(",");
                for (int i = 0; i < hourArray.length; i++) {
                    String currentHour = hourArray[i];
                    if (currentHour.indexOf("/") != -1) {
                        String[] additionalHours = getArrayFromColumn(currentHour);
                        hourArray = combineArrays(hourArray, additionalHours);
                        continue;
                    }
                    String[] currentHourArray = currentHour.split("-");
                    Element currentPeriodElement = (Element) periodElement.cloneNode(true);
                    String beginHour = currentHourArray[0];

                    int iEndHour = (Integer.parseInt(beginHour) + 1) % 24;
                    // workaround, bis endhour am nchsten Tag erlaubt
                    if (iEndHour == 0)
                        iEndHour = 24;
                    String endHour = "" + iEndHour;
                    if (currentHourArray.length > 1)
                        endHour = currentHourArray[1];
                    beginHour = formatTwoDigits(beginHour);
                    endHour = formatTwoDigits(endHour);
                    currentPeriodElement.setAttribute("begin", beginHour + ":00");
                    currentPeriodElement.setAttribute("end", endHour + ":00");
                    childElements.add(currentPeriodElement);
                }
            }
        } // end if  minutes.startsWith("*")
        else { // one or more minutes are fixed
            String[] minutesArray = getArrayFromColumn(minutes);
            for (String element : minutesArray) {
                Element currentPeriodElement = (Element) periodElement.cloneNode(true);
                String currentMinute = element;

                currentMinute = formatTwoDigits(currentMinute);
                if (hours.startsWith("*")) {
                    currentPeriodElement.setAttribute("absolute_repeat", "01:00");
                    usedNewRunTime = true;
                    if (!hours.equalsIgnoreCase("*")) {// repeat interval is given for hours
                        String repeat = hours.substring(2);
                        repeat = formatTwoDigits(repeat);
                        currentPeriodElement.setAttribute("absolute_repeat", repeat + ":00");
                    }
                    currentPeriodElement.setAttribute("begin", "00:" + currentMinute);
                    childElements.add(currentPeriodElement);
                } else { //fixed hour(s) is set
                    String[] hourArray = hours.split(",");
                    for (String element2 : hourArray) {
                        currentPeriodElement = (Element) periodElement.cloneNode(true);
                        String currentHour = element2;
                        if (currentHour.indexOf("-") == -1) {
                            // fixed hour and fixed minute --> create single_start
                            currentHour = formatTwoDigits(currentHour);
                            currentPeriodElement.setAttribute("single_start",
                                    currentHour + ":" + currentMinute);
                        } else {
                            // range of hours is set, create begin and end attributes
                            String[] currentHourArray = currentHour.split("[-/]");
                            int beginHour = Integer.parseInt(currentHourArray[0]);
                            int endHour = Integer.parseInt(currentHourArray[1]);
                            int beginMinute = Integer.parseInt(currentMinute);
                            int endMinute = beginMinute + 1;
                            // workaround, bis endhour am nchsten Tag erlaubt
                            endMinute = beginMinute;
                            if (endMinute == 60) {
                                endMinute = 0;
                                endHour = endHour + 1;
                            }
                            endHour = endHour % 24;
                            // workaround, bis endhour am nchsten Tag erlaubt
                            if (endHour == 0)
                                endHour = 24;
                            String stepSize = "1";
                            if (currentHourArray.length == 3) {
                                stepSize = formatTwoDigits(currentHourArray[2]);
                            }
                            currentPeriodElement.setAttribute("absolute_repeat", stepSize + ":00");
                            usedNewRunTime = true;
                            currentPeriodElement.setAttribute("begin",
                                    formatTwoDigits(beginHour) + ":" + formatTwoDigits(beginMinute));
                            currentPeriodElement.setAttribute("end",
                                    formatTwoDigits(endHour) + ":" + formatTwoDigits(endMinute));
                        }
                        childElements.add(currentPeriodElement);
                    }
                }
            }

        }

        logger.debug("processing days [" + days + "]");
        boolean monthDaysSet = false;
        if (days.startsWith("*")) {
            if (days.equals("*")) {
                // every day - do nothing, just keep periods
            } else {
                // repeat interval is given for days
                // this is not possible in the JobScheduler but can be poorly emulated
                Element monthDaysElement = runTimeElement.getOwnerDocument().createElement("monthdays");
                String repeat = days.substring(2);
                int iRepeat = Integer.parseInt(repeat);
                // use only 30 days
                for (int i = 1; i <= 30; i = i + iRepeat) {
                    String day = "" + i;
                    addDay(day, monthDaysElement, childElements);
                }
                childElements.clear();
                childElements.add(monthDaysElement);
                monthDaysSet = true;
            }
        } else {
            Element monthDaysElement = runTimeElement.getOwnerDocument().createElement("monthdays");
            String[] daysArray = getArrayFromColumn(days);
            for (String day : daysArray) {
                addDay(day, monthDaysElement, childElements);
            }
            childElements.clear();
            childElements.add(monthDaysElement);
            monthDaysSet = true;
        }

        if (!weekdays.equals("*") && monthDaysSet) {
            logger.info("Weekdays will not be processed as days are already set in current line.");
        } else {
            logger.debug("processing weekdays [" + weekdays + "]");
            weekdays = replaceDayNames(weekdays);
            if (weekdays.startsWith("*/"))
                throw new JobSchedulerException("Repeat intervals for the weekdays column [" + weekdays
                        + "] are not supported. Please use the days column.");
            if (weekdays.equals("*")) {
                // all weekdays, do nothing
            } else {
                Element weekDaysElement = runTimeElement.getOwnerDocument().createElement("weekdays");
                String[] daysArray = getArrayFromColumn(weekdays);
                for (String day : daysArray) {
                    addDay(day, weekDaysElement, childElements);
                }
                childElements.clear();
                childElements.add(weekDaysElement);
            }
        }

        logger.debug("processing months [" + months + "]");
        if (months.startsWith("*")) {
            if (months.equals("*")) {
                // every month - do nothing
            } else {
                months = replaceMonthNames(months);
                // repeat interval is given for months
                // this is not possible in the JobScheduler but can be poorly emulated
                Vector<Element> newChildElements = new Vector<Element>();
                String repeat = months.substring(2);
                int iRepeat = Integer.parseInt(repeat);

                for (int i = 1; i <= 12; i = i + iRepeat) {
                    String month = "" + i;
                    Element monthElement = runTimeElement.getOwnerDocument().createElement("month");
                    usedNewRunTime = true;
                    monthElement.setAttribute("month", month);
                    Iterator<Element> iter = childElements.iterator();
                    while (iter.hasNext()) {
                        Element child = iter.next();
                        monthElement.appendChild(child.cloneNode(true));
                    }
                    newChildElements.add(monthElement);
                }
                childElements = newChildElements;
            }
        } else {// list of months is given
            Vector<Element> newChildElements = new Vector<Element>();
            String[] monthArray = getArrayFromColumn(months);
            for (String month : monthArray) {
                Element monthElement = runTimeElement.getOwnerDocument().createElement("month");
                usedNewRunTime = true;
                monthElement.setAttribute("month", month);
                Iterator<Element> iter = childElements.iterator();
                while (iter.hasNext()) {
                    Element child = iter.next();
                    monthElement.appendChild(child.cloneNode(true));
                }
                newChildElements.add(monthElement);
            }
            childElements = newChildElements;
        }

        // add topmost child elements to run_time element
        Iterator<Element> iter = childElements.iterator();
        while (iter.hasNext()) {
            Element someElement = iter.next();
            runTimeElement.appendChild(someElement);
        }
    } catch (Exception e) {
        throw new JobSchedulerException("Error creating run time: " + e, e);
    }

}

From source file:sos.scheduler.cron.CronConverter.java

private void addDay(final String day, final Element parentDaysElement, final Vector<Element> childElements)
        throws Exception {
    logger.debug("adding day: " + day);
    Element dayElement = parentDaysElement.getOwnerDocument().createElement("day");
    dayElement.setAttribute("day", day);
    Iterator<Element> iter = childElements.iterator();
    while (iter.hasNext()) {
        Element child = iter.next();
        dayElement.appendChild(child.cloneNode(true));
    }/*  w ww .  j  a  v a  2  s.  c om*/
    parentDaysElement.appendChild(dayElement);
}