Example usage for javax.xml.stream XMLStreamReader getAttributeLocalName

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

Introduction

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

Prototype

public String getAttributeLocalName(int index);

Source Link

Document

Returns the localName of the attribute at the provided index

Usage

From source file:org.orbisgis.core.layerModel.mapcatalog.Workspace.java

private int parsePublishResponse(XMLStreamReader parser) throws XMLStreamException {
    List<String> hierarchy = new ArrayList<String>();
    for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
        // For each XML elements
        switch (event) {
        case XMLStreamConstants.START_ELEMENT:
            hierarchy.add(parser.getLocalName());
            // Parse attributes
            if (RemoteCommons.endsWith(hierarchy, "context")) {
                for (int attributeId = 0; attributeId < parser.getAttributeCount(); attributeId++) {
                    String attributeName = parser.getAttributeLocalName(attributeId);
                    if (attributeName.equals("id")) {
                        return Integer.parseInt(parser.getAttributeValue(attributeId));
                    }/*from w  ww.  java  2  s  . c o  m*/
                }
            }
            break;
        case XMLStreamConstants.END_ELEMENT:
            hierarchy.remove(hierarchy.size() - 1);
            break;
        }
    }
    throw new XMLStreamException("Bad response on publishing a map context");
}

From source file:org.orbisgis.core.layerModel.mapcatalog.Workspace.java

/**
 * Read the parser and feed the provided list with workspaces
 * @param mapContextList Writable, empty list of RemoteMapContext
 * @param parser Opened parser/*from w  w  w.  j ava  2  s . co m*/
 * @throws XMLStreamException 
 */
public void parseXML(List<RemoteMapContext> mapContextList, XMLStreamReader parser)
        throws XMLStreamException, UnsupportedEncodingException {
    List<String> hierarchy = new ArrayList<String>();
    RemoteMapContext curMapContext = null;
    Locale curLocale = null;
    StringBuilder characters = new StringBuilder();
    for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
        // For each XML elements
        switch (event) {
        case XMLStreamConstants.START_ELEMENT:
            hierarchy.add(parser.getLocalName());
            if (RemoteCommons.endsWith(hierarchy, "contexts", "context")) {
                curMapContext = new RemoteOwsMapContext(cParams);
                curMapContext.setWorkspaceName(workspaceName);
            }
            // Parse attributes
            for (int attributeId = 0; attributeId < parser.getAttributeCount(); attributeId++) {
                String attributeName = parser.getAttributeLocalName(attributeId);
                if (attributeName.equals("id")) {
                    curMapContext.setId(Integer.parseInt(parser.getAttributeValue(attributeId)));
                } else if (attributeName.equals("date")) {
                    String attributeValue = parser.getAttributeValue(attributeId);
                    try {
                        curMapContext.setDate(parseDate(attributeValue));
                    } catch (ParseException ex) {
                        LOGGER.warn(I18N.tr("Cannot parse the provided date {0}", attributeValue), ex);
                    }
                } else if (attributeName.equals("lang")) {
                    curLocale = LocalizedText.forLanguageTag(parser.getAttributeValue(attributeId));
                }
            }
            break;
        case XMLStreamConstants.END_ELEMENT:
            if (RemoteCommons.endsWith(hierarchy, "contexts", "context")) {
                mapContextList.add(curMapContext);
                curMapContext = null;
            } else if (RemoteCommons.endsWith(hierarchy, "contexts", "context", "title")) {
                Locale descLocale = Locale.getDefault();
                if (curLocale != null) {
                    descLocale = curLocale;
                }
                curMapContext.getDescription().addTitle(descLocale,
                        StringEscapeUtils.unescapeHtml(characters.toString().trim()));
            } else if (RemoteCommons.endsWith(hierarchy, "contexts", "context", "abstract")) {
                Locale descLocale = Locale.getDefault();
                if (curLocale != null) {
                    descLocale = curLocale;
                }
                curMapContext.getDescription().addAbstract(descLocale,
                        StringEscapeUtils.unescapeHtml(characters.toString().trim()));
            }
            characters = new StringBuilder();
            curLocale = null;
            hierarchy.remove(hierarchy.size() - 1);
            break;
        case XMLStreamConstants.CHARACTERS:
            characters.append(StringEscapeUtils.unescapeHtml(parser.getText()));
            break;
        }
    }
}

From source file:org.orbisgis.coremap.layerModel.mapcatalog.Workspace.java

/**
 * Read the parser and feed the provided list with workspaces
 * @param mapContextList Writable, empty list of RemoteMapContext
 * @param parser Opened parser//from www . ja v  a  2 s. c  o m
 * @throws XMLStreamException 
 */
public void parseXML(List<RemoteMapContext> mapContextList, XMLStreamReader parser)
        throws XMLStreamException, UnsupportedEncodingException {
    List<String> hierarchy = new ArrayList<String>();
    RemoteMapContext curMapContext = null;
    Locale curLocale = null;
    StringBuilder characters = new StringBuilder();
    for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
        // For each XML elements
        switch (event) {
        case XMLStreamConstants.START_ELEMENT:
            hierarchy.add(parser.getLocalName());
            if (RemoteCommons.endsWith(hierarchy, "contexts", "context")) {
                curMapContext = new RemoteOwsMapContext(cParams);
                curMapContext.setWorkspaceName(workspaceName);
            }
            // Parse attributes
            for (int attributeId = 0; attributeId < parser.getAttributeCount(); attributeId++) {
                String attributeName = parser.getAttributeLocalName(attributeId);
                if (attributeName.equals("id")) {
                    curMapContext.setId(Integer.parseInt(parser.getAttributeValue(attributeId)));
                } else if (attributeName.equals("date")) {
                    String attributeValue = parser.getAttributeValue(attributeId);
                    try {
                        curMapContext.setDate(parseDate(attributeValue));
                    } catch (ParseException ex) {
                        LOGGER.warn(I18N.tr("Cannot parse the provided date {0}", attributeValue), ex);
                    }
                } else if (attributeName.equals("lang")) {
                    curLocale = LocalizedText.forLanguageTag(parser.getAttributeValue(attributeId));
                }
            }
            break;
        case XMLStreamConstants.END_ELEMENT:
            if (RemoteCommons.endsWith(hierarchy, "contexts", "context")) {
                mapContextList.add(curMapContext);
                curMapContext = null;
            } else if (RemoteCommons.endsWith(hierarchy, "contexts", "context", "title")) {
                Locale descLocale = Locale.getDefault();
                if (curLocale != null) {
                    descLocale = curLocale;
                }
                curMapContext.getDescription().addTitle(descLocale,
                        StringEscapeUtils.unescapeHtml4(characters.toString().trim()));
            } else if (RemoteCommons.endsWith(hierarchy, "contexts", "context", "abstract")) {
                Locale descLocale = Locale.getDefault();
                if (curLocale != null) {
                    descLocale = curLocale;
                }
                curMapContext.getDescription().addAbstract(descLocale,
                        StringEscapeUtils.unescapeHtml4(characters.toString().trim()));
            }
            characters = new StringBuilder();
            curLocale = null;
            hierarchy.remove(hierarchy.size() - 1);
            break;
        case XMLStreamConstants.CHARACTERS:
            characters.append(StringEscapeUtils.unescapeHtml4(parser.getText()));
            break;
        }
    }
}

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

private static Attr readAttribute(int i, Document d, XMLStreamReader reader) throws XMLStreamException {
    Attr a = null;/*w  ww . ja  v  a  2s.  c  om*/

    String local = reader.getAttributeLocalName(i);
    String ns = reader.getAttributeNamespace(i);
    if (ns != null) {
        String prefix = reader.getAttributePrefix(i);
        String qualified = prefix != null ? prefix + ":" + local : local;
        a = d.createAttributeNS(ns, qualified);
    } else {
        a = d.createAttribute(reader.getAttributeLocalName(i));
    }

    //if (log.isDebugEnabled())
    //log.debug("Reading attribute " + a.getName());

    a.setValue(reader.getAttributeValue(i));

    return a;
}

From source file:org.restcomm.connect.interpreter.rcml.Parser.java

private void start(final Stack<Tag.Builder> builders, final XMLStreamReader stream) {
    final Tag.Builder builder = Tag.builder();
    // Read the next tag.
    builder.setName(stream.getLocalName());
    // Read the attributes.
    final int limit = stream.getAttributeCount();
    for (int index = 0; index < limit; index++) {
        final String name = stream.getAttributeLocalName(index);
        final String value = stream.getAttributeValue(index).trim();
        final Attribute attribute = new Attribute(name, value);
        builder.addAttribute(attribute);
    }/*ww  w  .ja  va  2 s  . c om*/
    builders.push(builder);
}

From source file:org.rhq.plugins.hadoop.HadoopServerConfigurationDelegate.java

private static void updateFile(File configFile, Map<String, PropertySimple> allProps)
        throws IOException, InterruptedException, XMLStreamException {
    InputStream in = null;/*from   ww w . jav a  2s.c  om*/
    XMLStreamReader rdr = null;

    OutputStream out = null;
    XMLStreamWriter outWrt = null;

    try {
        Set<String> processedPropertyNames = new HashSet<String>();

        in = new BufferedInputStream(new FileInputStream(configFile));
        rdr = XML_INPUT_FACTORY.createXMLStreamReader(in);

        File tmpFile = File.createTempFile("hadoop-plugin", null);
        out = new FileOutputStream(tmpFile);
        outWrt = XML_OUTPUT_FACTORY.createXMLStreamWriter(out);

        ByteArrayOutputStream stash = new ByteArrayOutputStream();
        XMLStreamWriter stashWrt = XML_OUTPUT_FACTORY.createXMLStreamWriter(stash);
        boolean outputActive = true;

        outWrt.writeStartDocument();

        while (rdr.hasNext()) {
            int event = rdr.next();

            XMLStreamWriter wrt = outputActive ? outWrt : stashWrt;

            switch (event) {
            case XMLStreamConstants.ATTRIBUTE:
                break;
            case XMLStreamConstants.CDATA:
                wrt.writeCData(rdr.getText());
                break;
            case XMLStreamConstants.CHARACTERS:
                wrt.writeCharacters(rdr.getText());
                break;
            case XMLStreamConstants.COMMENT:
                wrt.writeComment(rdr.getText());
                break;
            case XMLStreamConstants.DTD:
                wrt.writeDTD(rdr.getText());
                break;
            case XMLStreamConstants.END_DOCUMENT:
                wrt.writeEndDocument();
                break;
            case XMLStreamConstants.END_ELEMENT:
                if (PROPERTY_TAG_NAME.equals(rdr.getName().getLocalPart())) {
                    String encoding = rdr.getEncoding();
                    if (encoding == null) {
                        encoding = "UTF-8";
                    }

                    String propertyTagSoFar = Charset.forName(encoding)
                            .decode(ByteBuffer.wrap(stash.toByteArray())).toString();
                    DetectedPropertyNameAndUpdatedTag propAndTag = updateProperty(propertyTagSoFar, allProps);

                    //yes, we're intentionally circumventing the xml stream writer, because we already have the XML data we want to write.
                    outWrt.flush();
                    out.write(propAndTag.updatedTag.getBytes("UTF-8"));

                    processedPropertyNames.add(propAndTag.propertyName);

                    //reset stuff
                    stash.reset();
                    wrt = outWrt;
                    outputActive = true;
                } else if (CONFIGURATION_TAG_NAME.equals(rdr.getName().getLocalPart())) {
                    //now add the new props
                    for (String prop : processedPropertyNames) {
                        allProps.remove(prop);
                    }

                    for (Map.Entry<String, PropertySimple> e : allProps.entrySet()) {
                        outWrt.writeStartElement(PROPERTY_TAG_NAME);

                        outWrt.writeStartElement(NAME_TAG_NAME);
                        outWrt.writeCharacters(e.getKey());
                        outWrt.writeEndElement();

                        outWrt.writeStartElement(VALUE_TAG_NAME);
                        outWrt.writeCharacters(e.getValue().getStringValue());
                        outWrt.writeEndElement();

                        outWrt.writeEndElement();
                    }
                }
                wrt.writeEndElement();
                break;
            case XMLStreamConstants.ENTITY_DECLARATION:
                //XXX could not find what to do with this
                break;
            case XMLStreamConstants.ENTITY_REFERENCE:
                wrt.writeEntityRef(rdr.getText());
                break;
            case XMLStreamConstants.NAMESPACE:
                for (int i = 0; i < rdr.getNamespaceCount(); ++i) {
                    wrt.writeNamespace(rdr.getNamespacePrefix(i), rdr.getNamespaceURI(i));
                }
                break;
            case XMLStreamConstants.NOTATION_DECLARATION:
                //XXX could not find what to do with this
                break;
            case XMLStreamConstants.PROCESSING_INSTRUCTION:
                wrt.writeProcessingInstruction(rdr.getPITarget(), rdr.getPIData());
                break;
            case XMLStreamConstants.SPACE:
                wrt.writeCharacters(rdr.getText());
                break;
            case XMLStreamConstants.START_DOCUMENT:
                //this seems to be never called for some strange reason
                //wrt.writeStartDocument();
                break;
            case XMLStreamConstants.START_ELEMENT:
                wrt.writeStartElement(rdr.getName().getPrefix(), rdr.getName().getLocalPart(),
                        rdr.getName().getNamespaceURI());

                for (int i = 0; i < rdr.getAttributeCount(); ++i) {
                    wrt.writeAttribute(rdr.getAttributePrefix(i), rdr.getAttributeNamespace(i),
                            rdr.getAttributeLocalName(i), rdr.getAttributeValue(i));
                }

                if (PROPERTY_TAG_NAME.equals(rdr.getName().getLocalPart())) {
                    wrt.writeCharacters("");
                    outputActive = false;
                }
                break;
            }
        }

        outWrt.flush();
        out.flush();
        out.close();

        in.close();

        //now copy the temp file in the place of the original one
        FileUtil.copyFile(tmpFile, configFile);
    } finally {
        rdr.close();

        outWrt.flush();
        outWrt.close();

        try {
            in.close();
        } finally {
            out.flush();
            out.close();
        }
    }
}

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  ava2  s. c om
        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.talend.dataprep.schema.xls.XlsUtils.java

/**
 * transform all attributes to a Map (key: att name, value: att value)
 *
 * @param streamReader/*from   ww w  . j  a va2  s  . c  o  m*/
 * @return
 */
private static Map<String, String> getAttributesNameValue(XMLStreamReader streamReader) {
    int size = streamReader.getAttributeCount();
    if (size > 0) {
        Map<String, String> attributesValues = new HashMap<>(size);
        for (int i = 0; i < size; i++) {
            String attributeName = streamReader.getAttributeLocalName(i);
            String attributeValue = streamReader.getAttributeValue(i);
            if (StringUtils.isNotEmpty(attributeName)) {
                attributesValues.put(attributeName, attributeValue);
            }
        }
        return attributesValues;
    }
    return Collections.emptyMap();
}

From source file:org.unitedinternet.cosmo.util.DomReader.java

private static Attr readAttribute(int i, Document d, XMLStreamReader reader) throws XMLStreamException {
    Attr a = null;//from w w  w . java  2s  . c  o  m

    String local = reader.getAttributeLocalName(i);
    String ns = reader.getAttributeNamespace(i);
    if (ns != null && !ns.equals("")) {
        String prefix = reader.getAttributePrefix(i);
        String qualified = prefix != null ? prefix + ":" + local : local;
        a = d.createAttributeNS(ns, qualified);
    } else {
        a = d.createAttribute(reader.getAttributeLocalName(i));
    }

    //if (log.isDebugEnabled())
    //log.debug("Reading attribute " + a.getName());

    a.setValue(reader.getAttributeValue(i));

    return a;
}

From source file:rjc.jplanner.model.Plan.java

/***************************************** loadXmlPlan *****************************************/
private int loadXmlPlan(XMLStreamReader xsr) throws XMLStreamException {
    // as calendars not yet loaded just keep calendar-id
    int calendarId = -1;

    // read XML plan attributes
    for (int i = 0; i < xsr.getAttributeCount(); i++)
        switch (xsr.getAttributeLocalName(i)) {
        case XmlLabels.XML_TITLE:
            m_title = xsr.getAttributeValue(i);
            break;
        case XmlLabels.XML_START:
            m_start = new DateTime(xsr.getAttributeValue(i));
            break;
        case XmlLabels.XML_DT_FORMAT:
            m_datetimeFormat = xsr.getAttributeValue(i);
            break;
        case XmlLabels.XML_D_FORMAT:
            m_dateFormat = xsr.getAttributeValue(i);
            break;
        case XmlLabels.XML_CALENDAR:
            calendarId = Integer.parseInt(xsr.getAttributeValue(i));
            break;
        case XmlLabels.XML_NOTES:
            m_notes = xsr.getAttributeValue(i);
            break;
        default:/*w  w w. ja v a2 s .c  om*/
            JPlanner.trace("Unhandled attribute '" + xsr.getAttributeLocalName(i) + "'");
            break;
        }

    // return calendar-id to be set as default calendar
    return calendarId;
}