List of usage examples for javax.xml.stream XMLStreamReader getLocation
public Location getLocation();
From source file:net.xy.jcms.controller.configurations.parser.UsecaseParser.java
/** * method for parsing single usecase xml files. one per file. * /* www.j a v a 2s . c om*/ * @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/*w ww .j a v a 2 s . c om*/ * * @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/*from w ww . j av a2s .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//from www .j a v a 2 s .c o 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 parameter deffinition/*w ww . j a v a2s . c o m*/ * * @param parser * @return * @throws XMLStreamException */ private static Parameter parseParameter(final XMLStreamReader parser) throws XMLStreamException { if (parser.getAttributeCount() != 2) { throw new IllegalArgumentException( "There are to much or few attributes specified for param. [" + parser.getLocation() + "]"); } String key = null, valueType = null; for (int i = 0; i < 2; i++) { if (parser.getAttributeLocalName(i).equals("key")) { key = parser.getAttributeValue(i); } else if (parser.getAttributeLocalName(i).equals("valueType")) { valueType = parser.getAttributeValue(i); } } if (StringUtils.isBlank(key)) { throw new IllegalArgumentException("Param key name is missing [" + parser.getLocation() + "]"); } else if (StringUtils.isBlank(valueType)) { throw new IllegalArgumentException("Param value type is missing [" + parser.getLocation() + "]"); } parser.nextTag(); return new Parameter(key, valueType); }
From source file:net.xy.jcms.controller.configurations.parser.UsecaseParser.java
/** * parses an controller section/* w w w .j a v a 2 s. co 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 controller itself/*from w ww . j a v a 2 s. com*/ * * @param parser * @return * @throws XMLStreamException * @throws ClassNotFoundException */ private static Controller parseController(final XMLStreamReader parser, final ClassLoader loader) throws XMLStreamException, ClassNotFoundException { if (parser.getAttributeCount() != 2 && parser.getAttributeCount() != 1) { throw new IllegalArgumentException( "There are to much or few attributes specified for class. [" + parser.getLocation() + "]"); } String path = null; final EnumSet<ConfigurationType> obmitedConfigurations = EnumSet.noneOf(ConfigurationType.class); for (int i = 0; i < parser.getAttributeCount(); i++) { if (parser.getAttributeLocalName(i).equals("path")) { path = parser.getAttributeValue(i); } else if (parser.getAttributeLocalName(i).equals("obmitConfig")) { final String[] configs = parser.getAttributeValue(i).split(","); for (final String config : configs) { obmitedConfigurations.add(ConfigurationType.valueOf(config.trim())); } } } if (StringUtils.isBlank(path)) { throw new IllegalArgumentException("Classpath must be set [" + parser.getLocation() + "]"); } parser.nextTag(); return new Controller(ControllerPool.get(path, loader), obmitedConfigurations); }
From source file:net.xy.jcms.controller.configurations.parser.UsecaseParser.java
/** * parses the configuration section//from w w w . j a v a2 s . co 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:net.xy.jcms.controller.configurations.parser.UsecaseParser.java
/** * parses the configuration itself// w w w . j a va2s . c o m * * @param parser * @return * @throws XMLStreamException */ private static Configuration<?> parseConfiguration(final XMLStreamReader parser, final ClassLoader loader) throws XMLStreamException { Configuration<?> config = null; if (parser.getAttributeCount() != 1 && parser.getAttributeCount() != 2) { throw new IllegalArgumentException("There are to much or few attributes specified for configuration. [" + parser.getLocation() + "]"); } ConfigurationType type = null; String include = null; for (int i = 0; i < parser.getAttributeCount(); i++) { if (parser.getAttributeLocalName(i).equals("type")) { type = ConfigurationType.valueOf(parser.getAttributeValue(i).trim()); } else if (parser.getAttributeLocalName(i).equals("include")) { include = parser.getAttributeValue(i); } } if (type == null) { throw new IllegalArgumentException("Configuration type must be set [" + parser.getLocation() + "]"); } if (include != null) { config = Configuration.initConfigurationByInclude(type, include, loader); } else { config = getConfigurationByBody(type, parser, loader); } if (config == null) { throw new IllegalArgumentException( "Configuration could not be retrieved [" + parser.getLocation() + "]"); } parser.nextTag(); return config; }
From source file:nl.ordina.bag.etl.util.XMLStreamReaderUtils.java
public static Tag readTag(XMLStreamReader reader, Tag... tags) throws XMLStreamException { reader.nextTag();/* w ww. java 2s. co m*/ for (Tag tag : tags) if (reader.getEventType() == tag.getElementType()) if (reader.getLocalName().equals(tag.getLocalName())) return tag; throw new XMLStreamException("Encountered element [" + reader.getLocalName() + "] while one of the following elements [" + StringUtils.join(tags, ",") + "] was expected", reader.getLocation()); }