Example usage for javax.xml.stream XMLStreamReader getAttributeValue

List of usage examples for javax.xml.stream XMLStreamReader getAttributeValue

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamReader getAttributeValue.

Prototype

public String getAttributeValue(String namespaceURI, String localName);

Source Link

Document

Returns the normalized attribute value of the attribute with the namespace and localName If the namespaceURI is null the namespace is not checked for equality

Usage

From source file:org.rhq.enterprise.server.sync.SynchronizationManagerBean.java

private <E, X> String importSingle(Subject subject, Map<String, Configuration> importConfigs,
        XMLStreamReader rdr) throws Exception {
    String synchronizerClassName = rdr.getAttributeValue(null, SynchronizationConstants.ID_ATTRIBUTE);

    @SuppressWarnings("unchecked")
    Synchronizer<E, X> synchronizer = instantiate(synchronizerClassName, Synchronizer.class,
            "The synchronizer denoted in the export file ('%s') does not implement the importer interface. This should not happen.");

    synchronizer.initialize(subject, entityManager);

    Importer<E, X> importer = synchronizer.getImporter();

    ExportedEntityMatcher<E, X> matcher = null; //this will be initialized once the importer is configured

    boolean configured = false;
    Configuration importConfiguration = importConfigs.get(synchronizerClassName);

    //the passed in configuration has precedence over the default one inlined in 
    //the config file.
    if (importConfiguration != null) {
        importer.configure(importConfiguration);
        matcher = importer.getExportedEntityMatcher();
        configured = true;//ww  w  .jav  a  2  s .co  m
    }

    while (rdr.hasNext()) {
        boolean bailout = false;
        switch (rdr.next()) {
        case XMLStreamConstants.START_ELEMENT:
            if (SynchronizationConstants.DEFAULT_CONFIGURATION_ELEMENT.equals(rdr.getName().getLocalPart())) {
                if (!configured) {
                    importConfiguration = getDefaultConfiguration(rdr);
                }
            } else if (SynchronizationConstants.DATA_ELEMENT.equals(rdr.getName().getLocalPart())) {

                //first check if the configure method has been called
                if (!configured) {
                    importer.configure(importConfiguration);
                    matcher = importer.getExportedEntityMatcher();
                    configured = true;
                }

                //now do the import

                rdr.nextTag();
                X exportedEntity = importer.unmarshallExportedEntity(new ExportReader(rdr));
                E entity = matcher == null ? null : matcher.findMatch(exportedEntity);
                importer.update(entity, exportedEntity);
            }
            break;
        case XMLStreamConstants.END_ELEMENT:
            if (SynchronizationConstants.ENTITIES_EXPORT_ELEMENT.equals(rdr.getName().getLocalPart())) {
                bailout = true;
            }
        }

        if (bailout) {
            break;
        }
    }

    //we might have had no data and because we configure the importer lazily, it might
    //be left unconfigured by the above loop.
    if (!configured) {
        importer.configure(importConfiguration);
    }

    return importer.finishImport();
}

From source file:org.sakaiproject.nakamura.importer.ImportSiteArchiveServlet.java

private void processContentXml(InputStream in, String sitePath, Session session, ZipFile zip)
        throws XMLStreamException {
    Map<String, Resource> resources = new HashMap<String, Resource>();
    String currentResourceId = null;
    XMLStreamReader reader = xmlInputFactory.createXMLStreamReader(in);
    for (int event = reader.next(); event != XMLStreamReader.END_DOCUMENT; event = reader.next()) {
        String localName = null;/*from   w  w w .j  av  a  2s  .c  o m*/
        switch (event) {
        case XMLStreamReader.START_ELEMENT:
            localName = reader.getLocalName();
            if ("archive".equalsIgnoreCase(localName)) {
                final String system = reader.getAttributeValue(null, "system");
                boolean supportedVersion = false;
                for (String version : supportedVersions) {
                    if (version.equalsIgnoreCase(system)) {
                        supportedVersion = true;
                    }
                }
                if (!supportedVersion) {
                    throw new Error("Not a supported version: " + system);
                }
                break;
            }
            if ("collection".equalsIgnoreCase(localName) || "resource".equalsIgnoreCase(localName)) {
                // grab the resource's attributes
                Resource resource = new Resource();
                for (int i = 0; i < reader.getAttributeCount(); i++) {
                    resource.attributes.put(reader.getAttributeLocalName(i).toLowerCase(),
                            reader.getAttributeValue(i));
                }
                currentResourceId = resource.getId();
                resources.put(currentResourceId, resource);
                break;
            }
            if ("property".equalsIgnoreCase(localName)) {
                Resource resource = resources.get(currentResourceId);
                final String name = reader.getAttributeValue(null, "name");
                String value = reader.getAttributeValue(null, "value");
                if (value != null && !"".equals(value)) {
                    if (reader.getAttributeValue(null, "enc").equalsIgnoreCase("BASE64")) {
                        value = new String(base64.decode(value));
                    }
                    resource.properties.put(name, value);
                }
                break;
            }
            break;
        case XMLStreamReader.END_ELEMENT:
            localName = reader.getLocalName();
            if ("collection".equalsIgnoreCase(localName) || "resource".equalsIgnoreCase(localName)) {
                makeResource(resources.get(currentResourceId), sitePath, session, zip);
            }
            break;
        } // end switch
    } // end for
    reader.close();
}

From source file:org.slc.sli.api.resources.config.StAXMsgBodyReader.java

/**
 * Helper method for determining if the element we are reading is unbounded or singular
 * @param reader xml reader//from w w  w.  ja  va 2  s.  c o m
 * @return True if part of a list, false if singular
 */
private static Boolean isMember(final XMLStreamReader reader) {
    final String member = reader.getAttributeValue(NS, LIST_ELEM);
    return (member != null && (member.equals("true") || member.equals("1")));
}

From source file:org.slc.sli.modeling.wadl.reader.WadlReader.java

private static final String getBase(final XMLStreamReader reader, final String defaultName) {
    final String name = reader.getAttributeValue(GLOBAL_NAMESPACE, WadlAttributeName.BASE.getLocalName());
    if (name != null) {
        return name;
    } else {// w  w w  .  ja v a2  s  .c om
        return defaultName;
    }
}

From source file:org.slc.sli.modeling.wadl.reader.WadlReader.java

private static final boolean getBooleanAttribute(final WadlAttributeName name, final boolean defaultValue,
        final XMLStreamReader reader) {
    final String value = reader.getAttributeValue(GLOBAL_NAMESPACE, name.getLocalName());
    if (value != null) {
        return Boolean.valueOf(value);
    } else {//from  w  w  w . j  a  va2 s  .  co m
        return defaultValue;
    }
}

From source file:org.slc.sli.modeling.wadl.reader.WadlReader.java

private static final String getHRef(final XMLStreamReader reader) {
    final String value = reader.getAttributeValue(GLOBAL_NAMESPACE, WadlAttributeName.HREF.getLocalName());
    if (value != null) {
        return value;
    } else {//from  w w w .java2  s  .  co  m
        throw new WadlRuntimeException(WadlAttributeName.HREF.getLocalName());
    }
}

From source file:org.slc.sli.modeling.wadl.reader.WadlReader.java

private static final String getHRef(final XMLStreamReader reader, final String defaultName) {
    final String name = reader.getAttributeValue(GLOBAL_NAMESPACE, WadlAttributeName.HREF.getLocalName());
    if (name != null) {
        return name;
    } else {/*from w w w  . j  a  va2 s .  c  o  m*/
        return defaultName;
    }
}

From source file:org.slc.sli.modeling.wadl.reader.WadlReader.java

private static final String getId(final XMLStreamReader reader) {
    return reader.getAttributeValue(GLOBAL_NAMESPACE, WadlAttributeName.ID.getLocalName());
}

From source file:org.slc.sli.modeling.wadl.reader.WadlReader.java

private static final String getLang(final XMLStreamReader reader, final String defaultName) {
    final String name = reader.getAttributeValue(GLOBAL_NAMESPACE, WadlAttributeName.LANG.getLocalName());
    if (name != null) {
        return name;
    } else {//from  w  w  w . j  a  v a2 s. c  o m
        return defaultName;
    }
}

From source file:org.slc.sli.modeling.wadl.reader.WadlReader.java

private static final String getName(final XMLStreamReader reader, final String defaultName) {
    final String name = reader.getAttributeValue(GLOBAL_NAMESPACE, WadlAttributeName.NAME.getLocalName());
    if (name != null) {
        return name;
    } else {/*w w w.  j  a  v a  2  s .c om*/
        return defaultName;
    }
}