Example usage for org.springframework.util.xml DomUtils getChildElementsByTagName

List of usage examples for org.springframework.util.xml DomUtils getChildElementsByTagName

Introduction

In this page you can find the example usage for org.springframework.util.xml DomUtils getChildElementsByTagName.

Prototype

public static List<Element> getChildElementsByTagName(Element ele, String childEleName) 

Source Link

Document

Retrieves all child elements of the given DOM element that match the given element name.

Usage

From source file:org.metaeffekt.dcc.commons.spring.xml.DCCConfigurationBeanDefinitionParser.java

private void parseAndRegisterBindings(Element profileElement, File origin, BeanDefinitionRegistry registry,
        ParserContext parserContext) {//from   w  w  w.  j  a  v  a  2 s  .  co m

    List<Element> bindingElements = DomUtils.getChildElementsByTagName(profileElement, ELEMENT_BINDING);
    for (Element bindingElement : bindingElements) {
        Element sourceElement = DomUtils.getChildElementByTagName(bindingElement, ELEMENT_BINDING_SOURCE);
        Element targetElement = DomUtils.getChildElementByTagName(bindingElement, ELEMENT_BINDING_TARGET);

        String sourceUnitRef = parseString(sourceElement, "unitRef", null, registry);
        String sourceCapabilityId = parseString(sourceElement, "capabilityId", null, registry);
        String targetUnitRef = parseString(targetElement, "unitRef", null, registry);
        String targetCapabilityId = parseString(targetElement, "capabilityId", null, registry);

        // convention: if target capabilityId is omitted then use the source capability id
        if (StringUtils.isEmpty(targetCapabilityId)) {
            targetCapabilityId = sourceCapabilityId;
        }

        String description = extractDescription(bindingElement, registry);

        BeanDefinitionBuilder bindingBeanBuilder = BeanDefinitionBuilder
                .genericBeanDefinition(BindingFactoryBean.class);
        bindingBeanBuilder.addPropertyValue(PROPERTY_ORIGIN, origin);
        bindingBeanBuilder.addPropertyValue(PROPERTY_DESCRIPTION, description);
        bindingBeanBuilder.addPropertyValue("sourceCapabilityId", Id.createCapabilityId(sourceCapabilityId));
        bindingBeanBuilder.addPropertyReference("sourceUnit", sourceUnitRef);
        bindingBeanBuilder.addPropertyValue("targetCapabilityId", Id.createCapabilityId(targetCapabilityId));
        bindingBeanBuilder.addPropertyReference("targetUnit", targetUnitRef);

        String bindingBeanName = parserContext.getReaderContext()
                .generateBeanName(bindingBeanBuilder.getBeanDefinition());
        registry.registerBeanDefinition(bindingBeanName, bindingBeanBuilder.getBeanDefinition());

        RuntimeBeanReference beanRef = new RuntimeBeanReference(bindingBeanName);
        beanRef.setSource(parserContext.extractSource(bindingElement));

    }
}

From source file:org.mule.security.oauth.config.AbstractDevkitBasedDefinitionParser.java

protected ManagedList parseList(Element element, String childElementName,
        AbstractDevkitBasedDefinitionParser.ParseDelegate parserDelegate) {
    ManagedList managedList = new ManagedList();
    List<Element> childDomElements = DomUtils.getChildElementsByTagName(element, childElementName);
    for (Element childDomElement : childDomElements) {
        if (hasAttribute(childDomElement, "value-ref")) {
            if (!isMuleExpression(childDomElement.getAttribute("value-ref"))) {
                managedList.add(new RuntimeBeanReference(childDomElement.getAttribute("value-ref")));
            } else {
                managedList.add(childDomElement.getAttribute("value-ref"));
            }//from w  w  w.j av a  2  s . co m
        } else {
            managedList.add(parserDelegate.parse(childDomElement));
        }
    }
    return managedList;
}

From source file:org.mule.security.oauth.config.AbstractDevkitBasedDefinitionParser.java

protected ManagedSet parseSet(Element element, String childElementName,
        AbstractDevkitBasedDefinitionParser.ParseDelegate parserDelegate) {
    ManagedSet managedSet = new ManagedSet();
    List<Element> childDomElements = DomUtils.getChildElementsByTagName(element, childElementName);
    for (Element childDomElement : childDomElements) {
        if (hasAttribute(childDomElement, "value-ref")) {
            if (!isMuleExpression(childDomElement.getAttribute("value-ref"))) {
                managedSet.add(new RuntimeBeanReference(childDomElement.getAttribute("value-ref")));
            } else {
                managedSet.add(childDomElement.getAttribute("value-ref"));
            }/*from   w  w  w  .  j av  a  2 s .  com*/
        } else {
            managedSet.add(parserDelegate.parse(childDomElement));
        }
    }
    return managedSet;
}

From source file:org.mule.security.oauth.config.AbstractDevkitBasedDefinitionParser.java

protected ManagedMap parseMap(Element element, String childElementName,
        AbstractDevkitBasedDefinitionParser.ParseDelegate parserDelegate) {
    ManagedMap managedMap = new ManagedMap();
    List<Element> childDomElements = DomUtils.getChildElementsByTagName(element, childElementName);
    if (childDomElements.size() == 0) {
        childDomElements = DomUtils.getChildElements(element);
    }// www  .j a  va2 s  .  com
    for (Element childDomElement : childDomElements) {
        Object key = null;
        if (hasAttribute(childDomElement, "key-ref")) {
            key = new RuntimeBeanReference(childDomElement.getAttribute("key-ref"));
        } else {
            if (hasAttribute(childDomElement, "key")) {
                key = childDomElement.getAttribute("key");
            } else {
                key = childDomElement.getTagName();
            }
        }
        if (hasAttribute(childDomElement, "value-ref")) {
            if (!isMuleExpression(childDomElement.getAttribute("value-ref"))) {
                managedMap.put(key, new RuntimeBeanReference(childDomElement.getAttribute("value-ref")));
            } else {
                managedMap.put(key, childDomElement.getAttribute("value-ref"));
            }
        } else {
            managedMap.put(key, parserDelegate.parse(childDomElement));
        }
    }
    return managedMap;
}

From source file:org.openlegacy.ide.eclipse.editors.TrailEditor.java

private void createTable(final Composite composite, final TerminalPersistedTrail terminalSessionTrail) {
    tableViewer = new TableViewer(composite);
    tableViewer.setContentProvider(new ArrayContentProvider());
    tableViewer.setInput(terminalSessionTrail.getSnapshots().toArray());

    Menu menu = new Menu(tableViewer.getTable());
    MenuItem menuItem = new MenuItem(menu, SWT.PUSH);
    menuItem.setText(Messages.getString("menu_generate_model"));
    menuItem.addSelectionListener(new SelectionListener() {

        @Override//from w w w . ja  v a2  s  .c o m
        public void widgetSelected(SelectionEvent event) {
            int[] selectionIndexes = tableViewer.getTable().getSelectionIndices();
            TerminalSnapshot[] snapshots = new TerminalSnapshot[selectionIndexes.length];
            for (int i = 0; i < selectionIndexes.length; i++) {
                TerminalSnapshot snapshot = terminalSessionTrail.getSnapshots().get(selectionIndexes[i]);
                snapshots[i] = snapshot;
            }
            GenerateScreenModelDialog dialog = new GenerateScreenModelDialog(getEditorSite().getShell(),
                    ((FileEditorInput) getEditorInput()).getFile(), false, snapshots);
            dialog.open();

        }

        @Override
        public void widgetDefaultSelected(SelectionEvent arg0) {
            widgetSelected(arg0);
        }
    });

    menuItem = new MenuItem(menu, SWT.PUSH);
    menuItem.setText(Messages.getString("menu_new_screen"));
    menuItem.addSelectionListener(new SelectionListener() {

        @Override
        public void widgetSelected(SelectionEvent event) {
            int[] selectionIndexes = tableViewer.getTable().getSelectionIndices();
            TerminalSnapshot snapshot = terminalSessionTrail.getSnapshots().get(selectionIndexes[0]);

            GenerateScreenModelDialog dialog = new GenerateScreenModelDialog(getEditorSite().getShell(),
                    ((FileEditorInput) getEditorInput()).getFile(), true, snapshot);
            dialog.open();
        }

        @Override
        public void widgetDefaultSelected(SelectionEvent arg0) {
            widgetSelected(arg0);
        }
    });

    menuItem = new MenuItem(menu, SWT.PUSH);
    menuItem.setText(Messages.getString("menu_update_screen_image"));
    menuItem.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            String title = null;
            ScreenPreview screenPreview = (ScreenPreview) PlatformUI.getWorkbench().getActiveWorkbenchWindow()
                    .getActivePage().findView(ScreenPreview.ID);
            if (screenPreview != null) {
                IEditorPart lastActiveEditor = screenPreview.getLastActiveEditor();
                if (lastActiveEditor != null) {
                    title = lastActiveEditor.getTitle();
                }
            }
            if (StringUtils.isEmpty(title) && !StringUtils.isEmpty(editorId)) {
                IEditorReference editorReference = getEditorReferenceById(editorId);
                if (editorReference != null) {
                    title = editorReference.getTitle();
                }
            }
            if (StringUtils.isEmpty(title)) {
                return;
            }
            String entityName = title.replace(".java", "");
            String message = MessageFormat.format("Are you sure you want to replace the image of {0} screen?",
                    entityName);
            boolean confirmed = MessageDialog.openQuestion(getEditorSite().getShell(), "Update Screen image",
                    message);
            if (confirmed) {
                int[] selectionIndexes = tableViewer.getTable().getSelectionIndices();
                TerminalSnapshot snapshot = terminalSessionTrail.getSnapshots().get(selectionIndexes[0]);
                IFile file = ((FileEditorInput) getEditorInput()).getFile();

                EclipseDesignTimeExecuter.instance().generateScreenEntityResources(file, snapshot, entityName);
            }
        }

    });

    tableViewer.getTable().setMenu(menu);
    GridData data = new GridData(GridData.FILL_BOTH);
    data.horizontalSpan = 1;
    data.widthHint = 200;
    tableViewer.getTable().setLayoutData(data);
    tableViewer.getTable().addKeyListener(new KeyListener() {

        @Override
        public void keyReleased(KeyEvent event) {
        }

        @Override
        public void keyPressed(KeyEvent event) {
            // delete
            if (event.keyCode == SWT.DEL) {
                Table table = (Table) event.getSource();
                int selectionIndex = table.getSelectionIndex();
                terminalSessionTrail.getSnapshots().remove(selectionIndex);
                table.remove(selectionIndex);
                try {
                    Element root = extractDocumentRoot();
                    List<Element> snapshots = DomUtils.getChildElementsByTagName(root, "snapshot"); //$NON-NLS-1$
                    root.removeChild(snapshots.get(selectionIndex));
                } catch (Exception e) {
                    throw (new DesigntimeException(e));
                }
            }
        }

    });
    TableViewerColumn column = new TableViewerColumn(tableViewer, SWT.NONE);
    column.getColumn().setWidth(100);
    column.getColumn().setText(Messages.getString("label_sequence"));
    column.setLabelProvider(new ColumnLabelProvider() {

        @Override
        public String getText(Object element) {
            TerminalSnapshot snapshot = (TerminalSnapshot) element;
            String direction = snapshot.getSnapshotType() == SnapshotType.INCOMING
                    ? Messages.getString("label_screen_in")
                    : Messages.getString("label_screen_out");
            return String
                    .valueOf(Messages.getString("label_screen_prefix") + snapshot.getSequence() + direction);
        }

    });
    tableViewer.addSelectionChangedListener(new ISelectionChangedListener() {

        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            ISelection selection = event.getSelection();
            Object firstElement = ((StructuredSelection) selection).getFirstElement();
            TerminalSnapshot snapshot = (TerminalSnapshot) firstElement;
            snapshotComposite.setSnapshot(snapshot);
            tableViewer.getTable().getMenu().getItem(0)
                    .setEnabled(snapshot.getSnapshotType() == SnapshotType.INCOMING);
            int[] selectionIndexes = tableViewer.getTable().getSelectionIndices();

            tableViewer.getTable().getMenu().getItem(1).setEnabled(selectionIndexes.length == 1);

            editorId = null;
            // editorId will be set in isOpenlegacyEditorOpened() if it will be found
            tableViewer.getTable().getMenu().getItem(2)
                    .setEnabled(isOpenlegacyEditorOpened() && (selectionIndexes.length == 1));
        }
    });
    refreshSnapshots();
}

From source file:org.openlegacy.ide.eclipse.editors.TrailEditor.java

/**
 * Saves the multi-page editor's document.
 *///from   ww  w.  j a  v  a 2  s  . c om
@Override
public void doSave(IProgressMonitor monitor) {

    Element root = extractDocumentRoot();
    List<Element> snapshots = DomUtils.getChildElementsByTagName(root, "snapshot"); //$NON-NLS-1$
    for (int i = 0; i < snapshots.size(); i++) {
        snapshots.get(i).setAttribute("sequence", String.valueOf(i + 1)); //$NON-NLS-1$
    }

    getEditor(getPageCount() - 1).doSave(monitor);
    createTrailPage();
}

From source file:org.springframework.batch.core.jsr.configuration.xml.BatchParser.java

private void parseRefElements(Element element, BeanDefinitionRegistry registry) {
    List<Element> beanElements = DomUtils.getChildElementsByTagName(element, "ref");

    if (beanElements.size() > 0) {
        for (Element curElement : beanElements) {
            AbstractBeanDefinition beanDefintion = BeanDefinitionBuilder
                    .genericBeanDefinition(curElement.getAttribute("class")).getBeanDefinition();

            beanDefintion.setScope("step");

            String beanName = curElement.getAttribute("id");

            if (!registry.containsBeanDefinition(beanName)) {
                registry.registerBeanDefinition(beanName, beanDefintion);
            } else {
                logger.info("Ignoring batch.xml bean defintion for " + beanName
                        + " because another bean of the same name has been registered");
            }/*from   w  ww.ja v a 2 s  . c  om*/
        }
    }

}

From source file:org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.java

/**
 * Parse replaced-method sub-elements of the given bean element.
 *//*from   w w  w  .j  a  va 2s  . c om*/
public void parseReplacedMethodSubElements(Element beanEle, MethodOverrides overrides) {
    NodeList nl = beanEle.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);
        if (isCandidateElement(node) && nodeNameEquals(node, REPLACED_METHOD_ELEMENT)) {
            Element replacedMethodEle = (Element) node;
            String name = replacedMethodEle.getAttribute(NAME_ATTRIBUTE);
            String callback = replacedMethodEle.getAttribute(REPLACER_ATTRIBUTE);
            ReplaceOverride replaceOverride = new ReplaceOverride(name, callback);
            // Look for arg-type match elements.
            List<Element> argTypeEles = DomUtils.getChildElementsByTagName(replacedMethodEle, ARG_TYPE_ELEMENT);
            for (Element argTypeEle : argTypeEles) {
                String match = argTypeEle.getAttribute(ARG_TYPE_MATCH_ATTRIBUTE);
                match = (StringUtils.hasText(match) ? match : DomUtils.getTextValue(argTypeEle));
                if (StringUtils.hasText(match)) {
                    replaceOverride.addTypeIdentifier(match);
                }
            }
            replaceOverride.setSource(extractSource(replacedMethodEle));
            overrides.addOverride(replaceOverride);
        }
    }
}

From source file:org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.java

/**
 * Parse a map element.//ww  w.jav  a 2s  .c  o  m
 */
public Map<Object, Object> parseMapElement(Element mapEle, @Nullable BeanDefinition bd) {
    String defaultKeyType = mapEle.getAttribute(KEY_TYPE_ATTRIBUTE);
    String defaultValueType = mapEle.getAttribute(VALUE_TYPE_ATTRIBUTE);

    List<Element> entryEles = DomUtils.getChildElementsByTagName(mapEle, ENTRY_ELEMENT);
    ManagedMap<Object, Object> map = new ManagedMap<>(entryEles.size());
    map.setSource(extractSource(mapEle));
    map.setKeyTypeName(defaultKeyType);
    map.setValueTypeName(defaultValueType);
    map.setMergeEnabled(parseMergeAttribute(mapEle));

    for (Element entryEle : entryEles) {
        // Should only have one value child element: ref, value, list, etc.
        // Optionally, there might be a key child element.
        NodeList entrySubNodes = entryEle.getChildNodes();
        Element keyEle = null;
        Element valueEle = null;
        for (int j = 0; j < entrySubNodes.getLength(); j++) {
            Node node = entrySubNodes.item(j);
            if (node instanceof Element) {
                Element candidateEle = (Element) node;
                if (nodeNameEquals(candidateEle, KEY_ELEMENT)) {
                    if (keyEle != null) {
                        error("<entry> element is only allowed to contain one <key> sub-element", entryEle);
                    } else {
                        keyEle = candidateEle;
                    }
                } else {
                    // Child element is what we're looking for.
                    if (nodeNameEquals(candidateEle, DESCRIPTION_ELEMENT)) {
                        // the element is a <description> -> ignore it
                    } else if (valueEle != null) {
                        error("<entry> element must not contain more than one value sub-element", entryEle);
                    } else {
                        valueEle = candidateEle;
                    }
                }
            }
        }

        // Extract key from attribute or sub-element.
        Object key = null;
        boolean hasKeyAttribute = entryEle.hasAttribute(KEY_ATTRIBUTE);
        boolean hasKeyRefAttribute = entryEle.hasAttribute(KEY_REF_ATTRIBUTE);
        if ((hasKeyAttribute && hasKeyRefAttribute)
                || ((hasKeyAttribute || hasKeyRefAttribute)) && keyEle != null) {
            error("<entry> element is only allowed to contain either "
                    + "a 'key' attribute OR a 'key-ref' attribute OR a <key> sub-element", entryEle);
        }
        if (hasKeyAttribute) {
            key = buildTypedStringValueForMap(entryEle.getAttribute(KEY_ATTRIBUTE), defaultKeyType, entryEle);
        } else if (hasKeyRefAttribute) {
            String refName = entryEle.getAttribute(KEY_REF_ATTRIBUTE);
            if (!StringUtils.hasText(refName)) {
                error("<entry> element contains empty 'key-ref' attribute", entryEle);
            }
            RuntimeBeanReference ref = new RuntimeBeanReference(refName);
            ref.setSource(extractSource(entryEle));
            key = ref;
        } else if (keyEle != null) {
            key = parseKeyElement(keyEle, bd, defaultKeyType);
        } else {
            error("<entry> element must specify a key", entryEle);
        }

        // Extract value from attribute or sub-element.
        Object value = null;
        boolean hasValueAttribute = entryEle.hasAttribute(VALUE_ATTRIBUTE);
        boolean hasValueRefAttribute = entryEle.hasAttribute(VALUE_REF_ATTRIBUTE);
        boolean hasValueTypeAttribute = entryEle.hasAttribute(VALUE_TYPE_ATTRIBUTE);
        if ((hasValueAttribute && hasValueRefAttribute)
                || ((hasValueAttribute || hasValueRefAttribute)) && valueEle != null) {
            error("<entry> element is only allowed to contain either "
                    + "'value' attribute OR 'value-ref' attribute OR <value> sub-element", entryEle);
        }
        if ((hasValueTypeAttribute && hasValueRefAttribute) || (hasValueTypeAttribute && !hasValueAttribute)
                || (hasValueTypeAttribute && valueEle != null)) {
            error("<entry> element is only allowed to contain a 'value-type' "
                    + "attribute when it has a 'value' attribute", entryEle);
        }
        if (hasValueAttribute) {
            String valueType = entryEle.getAttribute(VALUE_TYPE_ATTRIBUTE);
            if (!StringUtils.hasText(valueType)) {
                valueType = defaultValueType;
            }
            value = buildTypedStringValueForMap(entryEle.getAttribute(VALUE_ATTRIBUTE), valueType, entryEle);
        } else if (hasValueRefAttribute) {
            String refName = entryEle.getAttribute(VALUE_REF_ATTRIBUTE);
            if (!StringUtils.hasText(refName)) {
                error("<entry> element contains empty 'value-ref' attribute", entryEle);
            }
            RuntimeBeanReference ref = new RuntimeBeanReference(refName);
            ref.setSource(extractSource(entryEle));
            value = ref;
        } else if (valueEle != null) {
            value = parsePropertySubElement(valueEle, bd, defaultValueType);
        } else {
            error("<entry> element must specify a value", entryEle);
        }

        // Add final key and value to the Map.
        map.put(key, value);
    }

    return map;
}

From source file:org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.java

/**
 * Parse a props element./*w  ww .j ava2s.co m*/
 */
public Properties parsePropsElement(Element propsEle) {
    ManagedProperties props = new ManagedProperties();
    props.setSource(extractSource(propsEle));
    props.setMergeEnabled(parseMergeAttribute(propsEle));

    List<Element> propEles = DomUtils.getChildElementsByTagName(propsEle, PROP_ELEMENT);
    for (Element propEle : propEles) {
        String key = propEle.getAttribute(KEY_ATTRIBUTE);
        // Trim the text value to avoid unwanted whitespace
        // caused by typical XML formatting.
        String value = DomUtils.getTextValue(propEle).trim();
        TypedStringValue keyHolder = new TypedStringValue(key);
        keyHolder.setSource(extractSource(propEle));
        TypedStringValue valueHolder = new TypedStringValue(value);
        valueHolder.setSource(extractSource(propEle));
        props.put(keyHolder, valueHolder);
    }

    return props;
}