Example usage for org.w3c.dom Element getLocalName

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

Introduction

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

Prototype

public String getLocalName();

Source Link

Document

Returns the local part of the qualified name of this node.

Usage

From source file:org.openmrs.projectbuendia.webservices.rest.XformResource.java

/**
 * Removes the relationship nodes added (unconditionally) by xforms. If
 * XFRM-189 is fixed, this method can go away.
 *//*from   w w w  .  j ava 2 s  .  co m*/
static String removeRelationshipNodes(String xml) throws IOException, SAXException {
    Document doc = XmlUtil.parse(xml);
    removeBinding(doc, "patient_relative");
    removeBinding(doc, "patient_relative.person");
    removeBinding(doc, "patient_relative.relationship");

    for (Element relative : toElementIterable(doc.getElementsByTagNameNS("", "patient_relative"))) {
        removeNode(relative);
    }

    // Remove every parent of a label element with a text of
    // "RELATIONSHIPS". (Easiest way to find the ones added...)
    for (Element label : toElementIterable(doc.getElementsByTagNameNS(XFORMS_NAMESPACE, "label"))) {
        Element parent = (Element) label.getParentNode();
        if (XFORMS_NAMESPACE.equals(parent.getNamespaceURI()) && parent.getLocalName().equals("group")
                && "RELATIONSHIPS".equals(label.getTextContent())) {
            removeNode(parent);
            // We don't need to find other labels now, especially if they
            // may already have been removed.
            break;
        }
    }
    return XformsUtil.doc2String(doc);
}

From source file:org.osaf.caldav4j.methods.PropFindMethod.java

/**
 * A lot of this code had to be copied from the parent XMLResponseMethodBase, since it's 
 * initHashtable doesn't allow for new types of Responses.
 * /*from w w  w  . j a va  2 s.c  o m*/
 * Of course, the same mistake is being made here, so it is a TODO to fix that
 *
 */
@SuppressWarnings("unchecked")
private void initHashtable() {
    responseHashtable = new Hashtable<String, CalDAVResponse>();
    responseURLs = new Vector<String>();
    // Also accept OK sent by buggy servers in reply to a PROPFIND
    // or REPORT (Xythos, Catacomb, ...?).
    int statusCode = getStatusCode();
    if (statusCode == WebdavStatus.SC_MULTI_STATUS) {

        Document rdoc = getResponseDocument();

        NodeList list = null;
        if (rdoc != null) {
            Element multistatus = getResponseDocument().getDocumentElement();
            list = multistatus.getChildNodes();
        }

        if (list != null) {
            for (int i = 0; i < list.getLength(); i++) {
                try {
                    Element child = (Element) list.item(i);
                    String name = DOMUtils.getElementLocalName(child);
                    String namespace = DOMUtils.getElementNamespaceURI(child);
                    if (Response.TAG_NAME.equals(name) && "DAV:".equals(namespace)) {
                        CalDAVResponse response = new CalDAVResponse(child);
                        String href = response.getHref();
                        responseHashtable.put(href, response);
                        responseURLs.add(href);
                    }
                } catch (ClassCastException e) {
                }
            }
        }
    } else if (statusCode == WebdavStatus.SC_CONFLICT || statusCode == WebdavStatus.SC_FORBIDDEN) {
        Document rdoc = getResponseDocument();
        Element errorElement = rdoc.getDocumentElement();

        // first make sure that the element is actually an error.
        if (!errorElement.getNamespaceURI().equals(NS_DAV)
                || !errorElement.getLocalName().equals(ELEMENT_ERROR)) {
            Node condition = errorElement.getChildNodes().item(0);
            error = errorMap.get(new QName(condition.getNamespaceURI(), condition.getLocalName()));
        }
    }
}

From source file:org.osaf.cosmo.calendar.hcalendar.HCalendarParser.java

private void buildProperty(Element element, String propName, ContentHandler handler) throws ParserException {
    if (element == null)
        return;/*ww  w  .ja  v  a2s  . co  m*/

    if (log.isDebugEnabled())
        log.debug("Building property " + propName);

    String className = _className(propName);
    String elementName = element.getLocalName().toLowerCase();

    String value = null;
    if (elementName.equals("abbr")) {
        // "If an <abbr> element is used for a property, then the 'title'
        // attribute of the <abbr> element is the value of the property,
        // instead of the contents of the element, which instead provide a
        // human presentable version of the value."
        value = element.getAttribute("title");
        if (StringUtils.isBlank(value))
            throw new ParserException("Abbr element '" + className + "' requires a non-empty title", -1);
        if (log.isDebugEnabled())
            log.debug("Setting value '" + value + "' from title attribute");
    } else if (isHeaderElement(elementName)) {
        // try title first. if that's not set, fall back to text content.
        value = element.getAttribute("title");
        if (!StringUtils.isBlank(value)) {
            if (log.isDebugEnabled())
                log.debug("Setting value '" + value + "' from title attribute");
        } else {
            value = getTextContent(element);
            if (log.isDebugEnabled())
                log.debug("Setting value '" + value + "' from text content");
        }
    } else if (elementName.equals("a") && isUrlProperty(propName)) {
        value = element.getAttribute("href");
        if (StringUtils.isBlank(value))
            throw new ParserException("A element '" + className + "' requires a non-empty href", -1);
        if (log.isDebugEnabled())
            log.debug("Setting value '" + value + "' from href attribute");
    } else if (elementName.equals("img")) {
        if (isUrlProperty(propName)) {
            value = element.getAttribute("src");
            if (StringUtils.isBlank(value))
                throw new ParserException("Img element '" + className + "' requires a non-empty src", -1);
            if (log.isDebugEnabled())
                log.debug("Setting value '" + value + "' from src attribute");
        } else {
            value = element.getAttribute("alt");
            if (StringUtils.isBlank(value))
                throw new ParserException("Img element '" + className + "' requires a non-empty alt", -1);
            if (log.isDebugEnabled())
                log.debug("Setting value '" + value + "' from alt attribute");
        }
    } else {
        value = getTextContent(element);
        if (!StringUtils.isBlank(value)) {
            if (log.isDebugEnabled())
                log.debug("Setting value '" + value + "' from text content");
        }
    }

    if (StringUtils.isBlank(value)) {
        if (log.isDebugEnabled())
            log.debug("Skipping property with empty value");
        return;
    }

    handler.startProperty(propName);

    // if it's a date property, we have to convert from the
    // hCalendar-formatted date (RFC 3339) to an iCalendar-formatted date
    if (isDateProperty(propName)) {
        try {
            Date date = _icalDate(value);
            value = date.toString();

            if (!(date instanceof DateTime))
                try {
                    handler.parameter(Parameter.VALUE, Value.DATE.getValue());
                } catch (Exception e) {
                }
        } catch (ParseException e) {
            throw new ParserException("Malformed date value for element '" + className + "'", -1, e);
        }
    }

    if (isTextProperty(propName)) {
        String lang = element.getAttributeNS(XMLConstants.XML_NS_URI, "lang");
        if (!StringUtils.isBlank(lang))
            try {
                handler.parameter(Parameter.LANGUAGE, lang);
            } catch (Exception e) {
            }
    }

    // XXX: other parameters?

    try {
        handler.propertyValue(value);
    } catch (URISyntaxException e) {
        throw new ParserException("Malformed URI value for element '" + className + "'", -1, e);
    } catch (ParseException e) {
        throw new ParserException("Malformed value for element '" + className + "'", -1, e);
    } catch (IOException e) {
        throw new RuntimeException("Unknown error setting property value for element '" + className + "'", e);
    }

    handler.endProperty(propName);
}

From source file:org.osaf.cosmo.calendar.query.ComponentFilter.java

/**
 * Construct a ComponentFilter object from a DOM Element
 * @param element/*w  ww  . ja v a2 s.  c om*/
 * @throws ParseException
 */
public ComponentFilter(Element element, VTimeZone timezone) throws ParseException {
    // Name must be present
    name = DomUtil.getAttribute(element, ATTR_CALDAV_NAME, null);

    if (name == null) {
        throw new ParseException("CALDAV:comp-filter a calendar component name  (e.g., \"VEVENT\") is required",
                -1);
    }

    if (!(name.equals(Calendar.VCALENDAR) || CalendarUtils.isSupportedComponent(name)
            || name.equals(Component.VALARM) || name.equals(Component.VTIMEZONE)))
        throw new ParseException(name + " is not a supported iCalendar component", -1);

    ElementIterator i = DomUtil.getChildren(element);
    int childCount = 0;

    while (i.hasNext()) {
        Element child = i.nextElement();
        childCount++;

        // if is-not-defined is present, then nothing else can be present
        if (childCount > 1 && isNotDefinedFilter != null)
            throw new ParseException("CALDAV:is-not-defined cannnot be present with other child elements", -1);

        if (ELEMENT_CALDAV_TIME_RANGE.equals(child.getLocalName())) {

            // Can only have one time-range element in a comp-filter
            if (timeRangeFilter != null) {
                throw new ParseException("CALDAV:comp-filter only one time-range element permitted", -1);
            }

            timeRangeFilter = new TimeRangeFilter(child, timezone);
        } else if (ELEMENT_CALDAV_COMP_FILTER.equals(child.getLocalName())) {

            // Add to list
            componentFilters.add(new ComponentFilter(child, timezone));

        } else if (ELEMENT_CALDAV_PROP_FILTER.equals(child.getLocalName())) {

            // Add to list
            propFilters.add(new PropertyFilter(child, timezone));

        } else if (ELEMENT_CALDAV_IS_NOT_DEFINED.equals(child.getLocalName())) {
            if (childCount > 1)
                throw new ParseException("CALDAV:is-not-defined cannnot be present with other child elements",
                        -1);
            isNotDefinedFilter = new IsNotDefinedFilter();
        } else if (ELEMENT_CALDAV_IS_DEFINED.equals(child.getLocalName())) {
            // XXX provided for backwards compatibility with
            // Evolution 2.6, which does not implement
            // is-not-defined;
            if (childCount > 1)
                throw new ParseException("CALDAV:is-defined cannnot be present with other child elements", -1);
            log.warn("old style 'is-defined' ignored from (outdated) client!");
        } else
            throw new ParseException("CALDAV:comp-filter an invalid element name found", -1);

    }
}

From source file:org.osaf.cosmo.calendar.query.ParamFilter.java

/**
 * Construct a ParamFilter object from a DOM Element
 * @param element//from  w  ww  .j  a va  2s  . c  o m
 * @throws ParseException
 */
public ParamFilter(Element element) throws ParseException {
    // Get name which must be present
    name = DomUtil.getAttribute(element, ATTR_CALDAV_NAME, null);

    if (name == null) {
        throw new ParseException(
                "CALDAV:param-filter a property parameter name (e.g., \"PARTSTAT\") is required", -1);
    }

    // Can only have a single ext-match element
    ElementIterator i = DomUtil.getChildren(element);

    if (i.hasNext()) {

        Element child = i.nextElement();

        if (i.hasNext()) {
            throw new ParseException(
                    "CALDAV:param-filter only a single text-match or is-not-defined element is allowed", -1);
        }

        if (ELEMENT_CALDAV_TEXT_MATCH.equals(child.getLocalName())) {
            textMatchFilter = new TextMatchFilter(child);

        } else if (ELEMENT_CALDAV_IS_NOT_DEFINED.equals(child.getLocalName())) {

            isNotDefinedFilter = new IsNotDefinedFilter();
        } else
            throw new ParseException("CALDAV:param-filter an invalid element name found", -1);
    }
}

From source file:org.osaf.cosmo.calendar.query.PropertyFilter.java

/**
 * Construct a PropertyFilter object from a DOM Element
 * @param element/*www  . j  a  v a 2  s .c om*/
 * @throws ParseException
 */
public PropertyFilter(Element element, VTimeZone timezone) throws ParseException {
    // Name must be present
    name = DomUtil.getAttribute(element, ATTR_CALDAV_NAME, null);
    if (name == null) {
        throw new ParseException("CALDAV:prop-filter a calendar property name (e.g., \"ATTENDEE\") is required",
                -1);
    }

    ElementIterator i = DomUtil.getChildren(element);
    int childCount = 0;

    while (i.hasNext()) {
        Element child = i.nextElement();
        childCount++;

        // if is-not-defined is present, then nothing else can be present
        if (childCount > 1 && isNotDefinedFilter != null)
            throw new ParseException("CALDAV:is-not-defined cannnot be present with other child elements", -1);

        if (ELEMENT_CALDAV_TIME_RANGE.equals(child.getLocalName())) {

            // Can only have one time-range or text-match
            if (timeRangeFilter != null)
                throw new ParseException(
                        "CALDAV:prop-filter only one time-range or text-match element permitted", -1);

            timeRangeFilter = new TimeRangeFilter(child, timezone);
        } else if (ELEMENT_CALDAV_TEXT_MATCH.equals(child.getLocalName())) {

            // Can only have one time-range or text-match
            if (textMatchFilter != null) {
                throw new ParseException(
                        "CALDAV:prop-filter only one time-range or text-match element permitted", -1);
            }

            textMatchFilter = new TextMatchFilter(child);

        } else if (ELEMENT_CALDAV_PARAM_FILTER.equals(child.getLocalName())) {

            // Add to list
            paramFilters.add(new ParamFilter(child));
        } else if (ELEMENT_CALDAV_IS_NOT_DEFINED.equals(child.getLocalName())) {
            if (childCount > 1)
                throw new ParseException("CALDAV:is-not-defined cannnot be present with other child elements",
                        -1);
            isNotDefinedFilter = new IsNotDefinedFilter();
        } else
            throw new ParseException("CALDAV:prop-filter an invalid element name found", -1);
    }
}

From source file:org.osaf.cosmo.dav.caldav.report.CaldavOutputFilter.java

/**
 * Returns an <code>OutputFilter</code> representing the given
 * <code>&lt;C:calendar-data/&gt;> element.
 *//*from  w ww . j a v  a 2 s  . co m*/
public static OutputFilter createFromXml(Element cdata) throws DavException {
    OutputFilter result = null;
    Period expand = null;
    Period limit = null;
    Period limitfb = null;

    String contentType = DomUtil.getAttribute(cdata, ATTR_CALDAV_CONTENT_TYPE, NAMESPACE_CALDAV);
    if (contentType != null && !contentType.equals(ICALENDAR_MEDIA_TYPE))
        throw new UnsupportedCalendarDataException(contentType);
    String version = DomUtil.getAttribute(cdata, ATTR_CALDAV_CONTENT_TYPE, NAMESPACE_CALDAV);
    if (version != null && !version.equals(ICALENDAR_VERSION))
        throw new UnsupportedCalendarDataException();

    // Look at each child element of calendar-data
    for (ElementIterator iter = DomUtil.getChildren(cdata); iter.hasNext();) {

        Element child = iter.nextElement();
        if (ELEMENT_CALDAV_COMP.equals(child.getLocalName())) {

            // At the top-level of calendar-data there should only be one
            // <comp> element as VCALENDAR components are the only top-level
            // components allowed in iCalendar data
            if (result != null)
                return null;

            // Get required name attribute and verify it is VCALENDAR
            String name = DomUtil.getAttribute(child, ATTR_CALDAV_NAME, null);
            if ((name == null) || !Calendar.VCALENDAR.equals(name))
                return null;

            // Now parse filter item
            result = parseCalendarDataComp(child);

        } else if (ELEMENT_CALDAV_EXPAND.equals(child.getLocalName())) {
            expand = parsePeriod(child, true);
        } else if (ELEMENT_CALDAV_LIMIT_RECURRENCE_SET.equals(child.getLocalName())) {
            limit = parsePeriod(child, true);
        } else if (ELEMENT_CALDAV_LIMIT_FREEBUSY_SET.equals(child.getLocalName())) {
            limitfb = parsePeriod(child, true);
        } else {
            log.warn("Ignoring child " + child.getTagName() + " of " + cdata.getTagName());
        }
    }

    // Now add any limit/expand options, creating a filter if one is not
    // already present
    if ((result == null) && ((expand != null) || (limit != null) || (limitfb != null))) {
        result = new OutputFilter("VCALENDAR");
        result.setAllSubComponents();
        result.setAllProperties();
    }
    if (expand != null) {
        result.setExpand(expand);
    }
    if (limit != null) {
        result.setLimit(limit);
    }
    if (limitfb != null) {
        result.setLimitfb(limitfb);
    }

    return result;
}

From source file:org.osaf.cosmo.dav.caldav.report.CaldavOutputFilter.java

private static OutputFilter parseCalendarDataComp(Element comp) {
    // Get required name attribute
    String name = DomUtil.getAttribute(comp, ATTR_CALDAV_NAME, null);
    if (name == null)
        return null;

    // Now create filter item
    OutputFilter result = new OutputFilter(name);

    // Look at each child element
    ElementIterator i = DomUtil.getChildren(comp);
    while (i.hasNext()) {
        Element child = i.nextElement();
        if (ELEMENT_CALDAV_ALLCOMP.equals(child.getLocalName())) {
            // Validity check
            if (result.hasSubComponentFilters()) {
                result = null;//from w  w  w.jav a  2s.  c o m
                return null;
            }
            result.setAllSubComponents();

        } else if (ELEMENT_CALDAV_ALLPROP.equals(child.getLocalName())) {
            // Validity check
            if (result.hasPropertyFilters()) {
                result = null;
                return null;
            }
            result.setAllProperties();

        } else if (ELEMENT_CALDAV_COMP.equals(child.getLocalName())) {
            // Validity check
            if (result.isAllSubComponents()) {
                result = null;
                return null;
            }
            OutputFilter subfilter = parseCalendarDataComp(child);
            if (subfilter == null) {
                result = null;
                return null;
            } else
                result.addSubComponent(subfilter);
        } else if (ELEMENT_CALDAV_PROP.equals(child.getLocalName())) {
            // Validity check
            if (result.isAllProperties()) {
                result = null;
                return null;
            }

            // Get required name attribute
            String propname = DomUtil.getAttribute(child, ATTR_CALDAV_NAME, null);
            if (propname == null) {
                result = null;
                return null;
            }

            // Get optional novalue attribute
            boolean novalue = false;
            String novaluetxt = DomUtil.getAttribute(child, ATTR_CALDAV_NOVALUE, null);
            if (novaluetxt != null) {
                if (VALUE_YES.equals(novaluetxt))
                    novalue = true;
                else if (VALUE_NO.equals(novaluetxt))
                    novalue = false;
                else {
                    result = null;
                    return null;
                }
            }

            // Now add property item
            result.addProperty(propname, novalue);
        }
    }

    return result;
}

From source file:org.osaf.cosmo.xml.DomWriter.java

private static void writeElement(Element e, XMLStreamWriter writer) throws XMLStreamException {
    //if (log.isDebugEnabled())
    //log.debug("Writing element " + e.getNodeName());

    String local = e.getLocalName();
    if (local == null)
        local = e.getNodeName();/*from   www.  jav a2s  .com*/

    String ns = e.getNamespaceURI();
    if (ns != null) {
        String prefix = e.getPrefix();
        if (prefix != null) {
            writer.writeStartElement(prefix, local, ns);
            writer.writeNamespace(prefix, ns);
        } else {
            writer.setDefaultNamespace(ns);
            writer.writeStartElement(ns, local);
            writer.writeDefaultNamespace(ns);
        }
    } else {
        writer.writeStartElement(local);
    }

    NamedNodeMap attributes = e.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++)
        writeAttribute((Attr) attributes.item(i), writer);

    NodeList children = e.getChildNodes();
    for (int i = 0; i < children.getLength(); i++)
        writeNode(children.item(i), writer);

    writer.writeEndElement();
}

From source file:org.ow2.petals.binding.soap.SoapBootstrapOperations.java

/**
 * Set a SOAP parameter.//from   w  w  w  .j  a  v  a 2 s. com
 * 
 * @param name
 * @param value
 * @param jbi
 */
public void setParam(String name, String value, Jbi jbi) {
    for (Element element : jbi.getComponent().getAny()) {
        if (element.getLocalName().equals(name)) {
            element.setTextContent(value);
        }
    }
}