Example usage for javax.xml.stream XMLStreamReader getLocalName

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

Introduction

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

Prototype

public String getLocalName();

Source Link

Document

Returns the (local) name of the current event.

Usage

From source file:org.roda.core.index.utils.SolrUtils.java

public static SolrInputDocument getDescriptiveMetadataFields(Binary binary, String metadataType,
        String metadataVersion) throws GenericException {
    SolrInputDocument doc;//www .j  a va 2s.  c  om

    Map<String, String> parameters = new HashMap<>();
    parameters.put("prefix", RodaConstants.INDEX_OTHER_DESCRIPTIVE_DATA_PREFIX);
    Reader transformationResult = RodaUtils.applyMetadataStylesheet(binary,
            RodaConstants.CORE_CROSSWALKS_INGEST, metadataType, metadataVersion, parameters);

    try {
        XMLLoader loader = new XMLLoader();
        XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(transformationResult);

        boolean parsing = true;
        doc = null;
        while (parsing) {
            int event = parser.next();

            if (event == XMLStreamConstants.END_DOCUMENT) {
                parser.close();
                parsing = false;
            } else if (event == XMLStreamConstants.START_ELEMENT) {
                String currTag = parser.getLocalName();
                if ("doc".equals(currTag)) {
                    doc = loader.readDoc(parser);
                }
            }
        }

    } catch (XMLStreamException | FactoryConfigurationError e) {
        throw new GenericException("Could not process descriptive metadata binary " + binary.getStoragePath(),
                e);
    } finally {
        IOUtils.closeQuietly(transformationResult);
    }

    return doc == null ? new SolrInputDocument() : validateDescriptiveMetadataFields(doc);
}

From source file:org.roda.core.index.utils.SolrUtils.java

public static SolrInputDocument premisToSolr(PreservationMetadataType preservationMetadataType, AIP aip,
        String representationUUID, String fileUUID, Binary binary) throws GenericException {
    SolrInputDocument doc;//from w w  w .jav  a  2 s.  c  o  m

    Map<String, String> stylesheetOpt = new HashMap<>();
    stylesheetOpt.put(RodaConstants.PRESERVATION_EVENT_OBJECT_CLASS,
            PreservationMetadataEventClass.REPOSITORY.toString());

    if (aip != null) {
        stylesheetOpt.put(RodaConstants.PRESERVATION_EVENT_OBJECT_CLASS,
                PreservationMetadataEventClass.AIP.toString());
        stylesheetOpt.put(RodaConstants.PRESERVATION_EVENT_AIP_ID, aip.getId());

        if (representationUUID != null) {
            stylesheetOpt.put(RodaConstants.PRESERVATION_EVENT_REPRESENTATION_UUID, representationUUID);
            stylesheetOpt.put(RodaConstants.PRESERVATION_EVENT_OBJECT_CLASS,
                    PreservationMetadataEventClass.REPRESENTATION.toString());
        }

        if (fileUUID != null) {
            stylesheetOpt.put(RodaConstants.PRESERVATION_EVENT_FILE_UUID, fileUUID);
            stylesheetOpt.put(RodaConstants.PRESERVATION_EVENT_OBJECT_CLASS,
                    PreservationMetadataEventClass.FILE.toString());
        }
    }

    Reader reader = null;

    try {
        reader = RodaUtils.applyMetadataStylesheet(binary, RodaConstants.CORE_CROSSWALKS_INGEST_OTHER,
                RodaConstants.PREMIS_METADATA_TYPE, RodaConstants.PREMIS_METADATA_VERSION, stylesheetOpt);

        XMLLoader loader = new XMLLoader();
        XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(reader);

        boolean parsing = true;
        doc = null;
        while (parsing) {
            int event = parser.next();

            if (event == XMLStreamConstants.END_DOCUMENT) {
                parser.close();
                parsing = false;
            } else if (event == XMLStreamConstants.START_ELEMENT && "doc".equals(parser.getLocalName())) {
                doc = loader.readDoc(parser);
            }

        }

    } catch (XMLStreamException | FactoryConfigurationError e) {
        throw new GenericException("Could not process PREMIS " + binary.getStoragePath(), e);
    } finally {
        IOUtils.closeQuietly(reader);
    }

    if (preservationMetadataType == PreservationMetadataType.EVENT && doc != null) {
        try {
            List<LinkingIdentifier> agents = PremisV3Utils.extractAgentsFromEvent(binary);
            for (LinkingIdentifier id : agents) {
                doc.addField(RodaConstants.PRESERVATION_EVENT_LINKING_AGENT_IDENTIFIER,
                        JsonUtils.getJsonFromObject(id));
            }
        } catch (org.roda.core.data.v2.validation.ValidationException e) {
            LOGGER.warn("Error setting linking agent field: {}", e.getMessage());
        }
        try {
            List<LinkingIdentifier> sources = PremisV3Utils.extractObjectFromEvent(binary);
            for (LinkingIdentifier id : sources) {
                doc.addField(RodaConstants.PRESERVATION_EVENT_LINKING_SOURCE_OBJECT_IDENTIFIER,
                        JsonUtils.getJsonFromObject(id));
            }
        } catch (org.roda.core.data.v2.validation.ValidationException e) {
            LOGGER.warn("Error setting linking source field: {}", e.getMessage());
        }
        try {
            List<LinkingIdentifier> outcomes = PremisV3Utils.extractObjectFromEvent(binary);
            for (LinkingIdentifier id : outcomes) {
                doc.addField(RodaConstants.PRESERVATION_EVENT_LINKING_OUTCOME_OBJECT_IDENTIFIER,
                        JsonUtils.getJsonFromObject(id));
            }
        } catch (org.roda.core.data.v2.validation.ValidationException e) {
            LOGGER.warn("Error setting linking outcome field: {}", e.getMessage());
        }

        // indexing active state and permissions
        if (aip != null) {
            doc.addField(RodaConstants.STATE, aip.getState().toString());
            setPermissions(aip.getPermissions(), doc);
        } else {
            doc.addField(RodaConstants.STATE, AIPState.ACTIVE);
        }
    }

    // set uuid from id defined in xslt
    if (doc != null) {
        doc.addField(RodaConstants.INDEX_UUID, doc.getFieldValue(RodaConstants.PRESERVATION_EVENT_ID));
    } else {
        doc = new SolrInputDocument();
    }

    return doc;
}

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   ww w .j  a v a2 s .  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 digesting XML documents
 * @param reader XML reader//from www .  jav  a2s .co m
 * @return EntityBody representation that corresponds to the xml
 * @throws XMLStreamException on malformed XML
 */
private static final EntityBody readDocument(final XMLStreamReader reader) throws XMLStreamException {
    if (XMLStreamConstants.START_DOCUMENT == reader.getEventType()) {
        EntityBody body = null;
        while (reader.hasNext()) {
            reader.next();
            switch (reader.getEventType()) {
            case XMLStreamConstants.START_ELEMENT: {
                body = readDocumentElement(reader);
                return body;
            }
            case XMLStreamConstants.END_DOCUMENT: {
                return body;
            }
            case XMLStreamConstants.CHARACTERS: {
                // Ignore
                break;
            }
            default: {
                throw new XMLStreamException();
            }
            }
        }
    } else {
        throw new XMLStreamException(reader.getLocalName());
    }
    throw new XMLStreamException();
}

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

/**
 * Reads everything under the main document wrapper tag
 * @param reader Reader that we have for XML
 * @return EntityBody representation of the document
 * @throws XMLStreamException on malformed XML
 *//* w ww .  j ava2  s .c  om*/
private static final EntityBody readDocumentElement(final XMLStreamReader reader) throws XMLStreamException {
    final Map<String, Object> elements = new HashMap<String, Object>();
    while (reader.hasNext()) {
        reader.next();
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            final Pair<Object, Boolean> memberDataPair = readElement(reader);
            addToElements(reader.getLocalName(), memberDataPair, elements);
            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            return new EntityBody(elements);
        }
        case XMLStreamConstants.CHARACTERS: {
            // Ignore
            break;
        }
        default: {
            throw new XMLStreamException();
        }
        }
    }
    throw new XMLStreamException();
}

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

/**
 * A programmatic assertion that we have the reader positioned on the correct element.
 *
 * @param expectLocalName//  ww w. java2s . co  m
 *            The local name that we expect.
 * @param reader
 *            The reader.
 */
private static final void assertName(final WadlElementName name, final XMLStreamReader reader) {
    if (!match(name, reader)) {
        throw new AssertionError(reader.getLocalName());
    }
}

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

private static final boolean match(final WadlElementName name, final XMLStreamReader reader) {
    return name.getLocalName().equals(reader.getLocalName());
}

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

private static final Application readApplication(final XMLStreamReader reader) throws XMLStreamException {
    assertName(WadlElementName.APPLICATION, reader);
    final List<Documentation> doc = new LinkedList<Documentation>();
    Grammars grammars = null;//from  w  w  w  . j a  va2 s.com
    Resources resources = null;
    final List<ResourceType> resourceTypes = new LinkedList<ResourceType>();
    final List<Method> methods = new LinkedList<Method>();
    final List<Representation> representations = new LinkedList<Representation>();
    final List<Representation> faults = new LinkedList<Representation>();
    while (reader.hasNext()) {
        reader.next();
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            if (match(WadlElementName.DOCUMENTATION, reader)) {
                doc.add(readDocumentation(reader));
                break;
            } else if (match(WadlElementName.GRAMMARS, reader)) {
                grammars = assertNotNull(readGrammars(reader));
                break;
            } else if (match(WadlElementName.RESOURCES, reader)) {
                resources = assertNotNull(readResources(reader));
                break;
            } else {
                throw new AssertionError(reader.getLocalName());
            }
        }
        case XMLStreamConstants.END_ELEMENT: {
            assertName(WadlElementName.APPLICATION, reader);
            return new Application(doc, grammars, resources, resourceTypes, methods, representations, faults);
        }
        case XMLStreamConstants.CHARACTERS: {
            // Ignore.
            break;
        }
        default: {
            throw new AssertionError(reader.getEventType());
        }
        }
    }
    throw new AssertionError();
}

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

private static final Application readDocument(final XMLStreamReader reader) {
    try {/* w w w  .  j ava 2 s  .  c  om*/
        if (XMLStreamConstants.START_DOCUMENT == reader.getEventType()) {
            Application app = null;
            while (reader.hasNext()) {
                reader.next();
                switch (reader.getEventType()) {
                case XMLStreamConstants.START_ELEMENT: {
                    if (match(WadlElementName.APPLICATION, reader)) {
                        app = readApplication(reader);
                        if (app == null) {
                            throw new IllegalStateException();
                        }
                        break;
                    } else {
                        throw new AssertionError(reader.getLocalName());
                    }
                }
                case XMLStreamConstants.END_DOCUMENT: {
                    if (app == null) {
                        throw new IllegalStateException();
                    }
                    return app;
                }
                default: {
                    throw new AssertionError(reader.getEventType());
                }
                }
            }
            throw new AssertionError();
        } else {
            throw new AssertionError(reader.getLocalName());
        }
    } catch (final XMLStreamException e) {
        throw new WadlRuntimeException(e);
    }
}

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

private static final DmElement readMixedElement(final XMLStreamReader reader) throws XMLStreamException {
    final QName name = reader.getName();
    final List<DmNode> children = new LinkedList<DmNode>();
    while (reader.hasNext()) {
        reader.next();//w  w  w  .  j  a  v a2  s  .c  o m
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            children.add(readMixedElement(reader));
            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            if (name.equals(reader.getName())) {
                return new DmElement(name, children);
            } else {
                throw new AssertionError(reader.getLocalName());
            }
        }
        case XMLStreamConstants.CHARACTERS: {
            // Assume that the element will coalesce text nodes.
            children.add(new DmText(reader.getText()));
            break;
        }
        case XMLStreamConstants.COMMENT: {
            // Ignore for now.
            break;
        }
        default: {
            throw new AssertionError(reader.getEventType());
        }
        }
    }
    throw new AssertionError();
}