Java XML String Transform getIntegerAttribute(XMLStreamReader in, String attribute)

Here you can find the source of getIntegerAttribute(XMLStreamReader in, String attribute)

Description

get Integer Attribute

License

Apache License

Declaration

public static Integer getIntegerAttribute(XMLStreamReader in,
            String attribute) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import static com.google.common.base.Optional.absent;
import static com.google.common.base.Optional.fromNullable;
import static com.google.common.base.Optional.of;
import static com.google.common.base.Throwables.propagate;

import static java.lang.Integer.parseInt;

import java.io.StringWriter;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stax.StAXSource;
import javax.xml.transform.stream.StreamResult;
import com.google.common.base.Optional;

public class Main {
    public static Integer getIntegerAttribute(String in, String attribute) {
        return parseInt(getAttribute(in, attribute));
    }//  w  w  w  . j ava  2s . c o  m

    public static Integer getIntegerAttribute(XMLStreamReader in,
            String attribute) {
        return parseInt(getAttribute(in, attribute));
    }

    public static String getAttribute(String in, String attribute) {
        Optional<String> foundOpt = findAttribute(in, attribute);
        if (foundOpt.isPresent()) {
            return foundOpt.get();
        }
        throw new RuntimeException("\"" + attribute + "\" not found in \""
                + in + "\"");
    }

    public static String getAttribute(XMLStreamReader in, String attribute) {
        String foundOpt = in.getAttributeValue("", attribute);
        if (foundOpt == null) {
            try {
                throw new RuntimeException("\"" + attribute
                        + "\" not found in:\n" + asString(in));
            } catch (Exception e) {
                propagate(e);
            }
        }
        return foundOpt;
    }

    public static Optional<String> findAttribute(String in, String attribute) {
        Pattern pattern = Pattern.compile(attribute + "='([^']+?)'");
        Matcher matcher = pattern.matcher(in);
        if (matcher.find()) {
            return of(matcher.group(1));
        }
        pattern = Pattern.compile(attribute + "=\"([^\"]+?)\"");
        matcher = pattern.matcher(in);
        if (matcher.find()) {
            return of(matcher.group(1));
        }
        return absent();
    }

    public static Optional<String> findAttribute(XMLStreamReader in,
            String attribute) {
        return fromNullable(in.getAttributeValue("", attribute));
    }

    public static String asString(XMLStreamReader xmlr) throws Exception {
        Transformer transformer = TransformerFactory.newInstance()
                .newTransformer();
        StringWriter stringWriter = new StringWriter();
        transformer.transform(new StAXSource(xmlr), new StreamResult(
                stringWriter));
        return stringWriter.toString();
    }
}

Related

  1. fileToXMLString(String filename)
  2. getAsXMLString(final Node node)
  3. getBeanDefinition(String beanId, File file)
  4. getConfigurationAsXML(Map properties)
  5. getHtmlAsString(Node node)
  6. getNode(String text)
  7. getNotFoundXmlForObjectId(final String pid, final String detailCode, final String description)
  8. getOutputMimeType(String xslName)
  9. getOutputSQLPageXML(String inStatement, Map map)