Example usage for javax.xml.stream XMLStreamConstants START_ELEMENT

List of usage examples for javax.xml.stream XMLStreamConstants START_ELEMENT

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamConstants START_ELEMENT.

Prototype

int START_ELEMENT

To view the source code for javax.xml.stream XMLStreamConstants START_ELEMENT.

Click Source Link

Document

Indicates an event is a start element

Usage

From source file:net.xy.jcms.controller.configurations.parser.TranslationParser.java

/**
 * goes over any <rule reactOn="^du" buildOff="du" usecase="contentgroup">
 * // w  w w .  j  a v a  2 s .  c om
 * @param parser
 * @return value
 * @throws XMLStreamException
 * @throws ClassNotFoundException
 * @throws IllegalAccessException
 * @throws InstantiationException
 */
private static TranslationRule[] parseRules(final XMLStreamReader parser, final ClassLoader loader)
        throws XMLStreamException, ClassNotFoundException {
    final List<TranslationRule> rules = new LinkedList<TranslationRule>();
    while (parser.nextTag() == XMLStreamConstants.START_ELEMENT) {
        if (parser.getLocalName().equals("rule")) {
            rules.add(parseRule(parser, loader));
        } else {
            throw new IllegalArgumentException("Syntax error nothing allowed between rule sections.");
        }
    }
    return rules.toArray(new TranslationRule[rules.size()]);
}

From source file:net.xy.jcms.controller.configurations.parser.TranslationParser.java

/**
 * checks for parameters <parameter name="contentgroup" group="1"
 * convert="de.jamba.ContentGroupConverter"/>
 * /*from   www . j  av  a2 s. co  m*/
 * @param parser
 * @return value
 * @throws XMLStreamException
 * @throws ClassNotFoundException
 * @throws IllegalAccessException
 * @throws InstantiationException
 */
private static List<RuleParameter> parseParameter(final XMLStreamReader parser, final ClassLoader loader)
        throws XMLStreamException, ClassNotFoundException {
    final List<RuleParameter> params = new ArrayList<RuleParameter>();
    while (parser.nextTag() == XMLStreamConstants.START_ELEMENT) {
        String parameterName = null, converter = null;
        Integer aplicatesToGroup = null;
        if (parser.getAttributeCount() != 3 && parser.getAttributeCount() != 2) {
            throw new IllegalArgumentException("There are to much or few attributes specified for parameter.");
        }
        for (int i = 0; i < parser.getAttributeCount(); i++) {
            if (parser.getAttributeLocalName(i).equals("name")) {
                parameterName = parser.getAttributeValue(i);
            } else if (parser.getAttributeLocalName(i).equals("convert")) {
                converter = parser.getAttributeValue(i);
            } else if (parser.getAttributeLocalName(i).equals("group")) {
                aplicatesToGroup = new Integer(parser.getAttributeValue(i));
            }
        }

        boolean goEnd = true;
        Map<String, String> mappings = null;
        if (parser.next() == XMLStreamConstants.CHARACTERS) {
            final String mappingStr = parser.getText();
            // get integrated mapping body
            if (StringUtils.isNotBlank(mappingStr)) {
                mappings = new HashMap<String, String>();
                final String[] lines = mappingStr.split("\n");
                for (String line : lines) {
                    line = line.trim();
                    if (StringUtils.isBlank(line) || line.startsWith("#")) {
                        continue;
                    }
                    final String[] pair = line.split("=", 2);
                    mappings.put(pair[0].trim(), pair[1].trim());
                }
            }
        } else {
            goEnd = false; // allready on end
        }

        if (StringUtils.isBlank(parameterName)) {
            throw new IllegalArgumentException("Parameter name has to be set");
        } else if (StringUtils.isBlank(converter)) {
            throw new IllegalArgumentException("Parameter Converter has to be set");
        } else if (aplicatesToGroup == null) {
            throw new IllegalArgumentException("Applicates to regex group has to be set");
        } else {
            if (mappings != null) {
                // get special mapping converter
                params.add(new RuleParameter(parameterName, aplicatesToGroup,
                        ((InitializableController<?>) ConverterPool.get(converter, loader))
                                .initialize(mappings)));
            } else {
                // get normal converter
                params.add(new RuleParameter(parameterName, aplicatesToGroup,
                        ConverterPool.get(converter, loader)));
            }
        }
        if (goEnd) {
            parser.nextTag(); // gets endtag
        }
    }
    return params;
}

From source file:net.xy.jcms.controller.configurations.parser.UsecaseParser.java

/**
 * parses usecases out from an xml file/*from  w  ww . jav  a 2 s.com*/
 * 
 * @param in
 * @param loader
 *            used for retrieving configuration included resources and also
 *            for retrieving the controllers
 * @return value
 * @throws XMLStreamException
 * @throws ClassNotFoundException
 */
public static Usecase[] parse(final InputStream in, final ClassLoader loader)
        throws XMLStreamException, ClassNotFoundException {
    final XMLInputFactory factory = XMLInputFactory.newInstance();
    factory.setProperty("javax.xml.stream.isCoalescing", true);
    // not supported by the reference implementation
    // factory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.TRUE);
    final XMLStreamReader parser = factory.createXMLStreamReader(in);
    while (parser.hasNext()) {
        final int event = parser.next();
        if (event == XMLStreamConstants.START_ELEMENT && parser.getName().getLocalPart().equals("usecases")) {
            return parseUsecases(parser, loader);
        }
    }
    throw new IllegalArgumentException("No usecases section found. [" + parser.getLocation() + "]");
}

From source file:net.xy.jcms.controller.configurations.parser.UsecaseParser.java

/**
 * method for parsing single usecase xml files. one per file.
 * //from w w w .j av a 2 s . com
 * @param in
 * @param loader
 * @return parsed usecase
 * @throws XMLStreamException
 * @throws ClassNotFoundException
 */
public static Usecase parseSingle(final InputStream in, final ClassLoader loader)
        throws XMLStreamException, ClassNotFoundException {
    final XMLInputFactory factory = XMLInputFactory.newInstance();
    factory.setProperty("javax.xml.stream.isCoalescing", true);
    // not supported by the reference implementation
    // factory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.TRUE);
    final XMLStreamReader parser = factory.createXMLStreamReader(in);
    while (parser.hasNext()) {
        final int event = parser.next();
        if (event == XMLStreamConstants.START_ELEMENT && parser.getName().getLocalPart().equals("usecase")) {
            return parseUsecase(parser, loader);
        }
    }
    throw new IllegalArgumentException("No usecases section found. [" + parser.getLocation() + "]");
}

From source file:net.xy.jcms.controller.configurations.parser.UsecaseParser.java

/**
 * parses usecase section/*from   w w w.j av  a 2s. c o  m*/
 * 
 * @param parser
 * @return
 * @throws XMLStreamException
 * @throws ClassNotFoundException
 */
private static Usecase[] parseUsecases(final XMLStreamReader parser, final ClassLoader loader)
        throws XMLStreamException, ClassNotFoundException {
    final List<Usecase> cases = new ArrayList<Usecase>();
    while (parser.nextTag() == XMLStreamConstants.START_ELEMENT) {
        if (parser.getLocalName().equals("usecase")) {
            cases.add(parseUsecase(parser, loader));
        } else {
            throw new IllegalArgumentException(
                    "Syntax error nothing allowed between Usecase sections. [" + parser.getLocation() + "]");
        }
    }
    return cases.toArray(new Usecase[cases.size()]);
}

From source file:net.xy.jcms.controller.configurations.parser.UsecaseParser.java

/**
 * parse the usecase subsections//w w w  . j  ava2s.c  o  m
 * 
 * @param parser
 * @return
 * @throws XMLStreamException
 * @throws ClassNotFoundException
 */
private static Usecase parseUsecase(final XMLStreamReader parser, final ClassLoader loader)
        throws XMLStreamException, ClassNotFoundException {
    if (parser.getAttributeCount() != 1) {
        throw new IllegalArgumentException(
                "There are to much or few attributes specified for usecase. [" + parser.getLocation() + "]");
    }
    final String id = parser.getAttributeValue(0);
    String description = null;
    Parameter[] parameterList = {};
    Controller[] controllerList = {};
    Configuration<?>[] configurationList = {};
    while (parser.nextTag() == XMLStreamConstants.START_ELEMENT) {
        if (parser.getLocalName().equals("description")) {
            description = parseDescription(parser);
        } else if (parser.getLocalName().equals("parameter")) {
            parameterList = parseParameters(parser);
        } else if (parser.getLocalName().equals("controller")) {
            controllerList = parseControllers(parser, loader);
        } else if (parser.getLocalName().equals("configurations")) {
            configurationList = parseConfigurations(parser, loader);
        } else {
            throw new IllegalArgumentException(
                    "Syntax error nothing allowed between Usecase sections. [" + parser.getLocation() + "]");
        }
    }
    if (StringUtils.isBlank(description) || description.length() < 32) {
        throw new IllegalArgumentException(
                "Description is empty or insufficient please add more details. [" + parser.getLocation() + "]");
    }
    return new Usecase(id, description, parameterList, controllerList, configurationList);
}

From source file:net.xy.jcms.controller.configurations.parser.UsecaseParser.java

/**
 * parses parameter entries//w ww .  j a v a  2  s.co  m
 * 
 * @param parser
 * @return
 * @throws XMLStreamException
 */
private static Parameter[] parseParameters(final XMLStreamReader parser) throws XMLStreamException {
    final List<Parameter> params = new ArrayList<Parameter>();
    while (parser.nextTag() == XMLStreamConstants.START_ELEMENT) {
        if (parser.getLocalName().equals("param")) {
            params.add(parseParameter(parser));
        } else {
            throw new IllegalArgumentException(
                    "Syntax error nothing allowed between param deffinitions. [" + parser.getLocation() + "]");
        }
    }
    return params.toArray(new Parameter[params.size()]);
}

From source file:net.xy.jcms.controller.configurations.parser.UsecaseParser.java

/**
 * parses an controller section//ww  w . ja  v a2  s  .c o  m
 * 
 * @param parser
 * @return
 * @throws XMLStreamException
 * @throws ClassNotFoundException
 */
private static Controller[] parseControllers(final XMLStreamReader parser, final ClassLoader loader)
        throws XMLStreamException, ClassNotFoundException {
    final List<Controller> controller = new LinkedList<Controller>();
    while (parser.nextTag() == XMLStreamConstants.START_ELEMENT) {
        if (parser.getLocalName().equals("class")) {
            controller.add(parseController(parser, loader));
        } else {
            throw new IllegalArgumentException("Syntax error nothing allowed between controller deffinitions. ["
                    + parser.getLocation() + "]");
        }
    }
    return controller.toArray(new Controller[controller.size()]);
}

From source file:net.xy.jcms.controller.configurations.parser.UsecaseParser.java

/**
 * parses the configuration section//www.j  a va  2s  . c o m
 * 
 * @param parser
 * @return
 * @throws XMLStreamException
 */
private static Configuration<?>[] parseConfigurations(final XMLStreamReader parser, final ClassLoader loader)
        throws XMLStreamException {
    final List<Configuration<?>> configs = new ArrayList<Configuration<?>>();
    while (parser.nextTag() == XMLStreamConstants.START_ELEMENT) {
        if (parser.getLocalName().equals("configuration")) {
            final Configuration<?> config = parseConfiguration(parser, loader);
            if (config != null) {
                configs.add(config);
                if (config instanceof TemplateConfiguration) {
                    try {
                        configs.addAll(loadFragmentDependencies((TemplateConfiguration) config, loader));
                    } catch (final IOException e) {
                        throw new IllegalArgumentException(
                                "An implicite referenced config couldn't be loaded pls have a look.", e);
                    } catch (final Exception e) {
                        throw new IllegalArgumentException(
                                "An implicite referenced config has caused some error please verify format and parsing",
                                e);
                    }
                }
            }
        } else {
            throw new IllegalArgumentException(
                    "Syntax error nothing allowed between configuration deffinitions. [" + parser.getLocation()
                            + "]");
        }
    }
    return configs.toArray(new Configuration[configs.size()]);
}

From source file:nl.knaw.huygens.tei.xpath.XPathUtil.java

public static Map<String, String> getNamespaceInfo(String xml) {
    Map<String, String> namespaces = Maps.newIdentityHashMap();
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    try {/*from   w ww  . j a  v a  2 s. c o  m*/
        XMLStreamReader xreader = inputFactory.createXMLStreamReader(IOUtils.toInputStream(xml, "UTF-8"));
        while (xreader.hasNext()) {
            if (xreader.next() == XMLStreamConstants.START_ELEMENT) {
                QName qName = xreader.getName();
                if (qName != null) {
                    addNamespace(namespaces, qName.getPrefix(), qName.getNamespaceURI());
                    for (int i = 0; i < xreader.getAttributeCount(); i++) {
                        addNamespace(namespaces, xreader.getAttributePrefix(i),
                                xreader.getAttributeNamespace(i));
                    }
                }
            }
        }
    } catch (XMLStreamException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return namespaces;
}