List of usage examples for org.xml.sax SAXParseException SAXParseException
public SAXParseException(String message, Locator locator)
From source file:com.jkoolcloud.tnt4j.streams.configure.sax.ConfigParserHandler.java
/** * Processes a {@code <parser>} element. * * @param attrs// w ww . j a v a 2 s . com * List of element attributes * * @throws SAXException * if error occurs parsing element */ private void processParser(Attributes attrs) throws SAXException { if (currParser != null) { throw new SAXParseException(StreamsResources.getStringFormatted(StreamsResources.RESOURCE_BUNDLE_NAME, "ConfigParserHandler.malformed.configuration", PARSER_ELMT), currParseLocation); } String name = null; String className = null; String tags = null; for (int i = 0; i < attrs.getLength(); i++) { String attName = attrs.getQName(i); String attValue = attrs.getValue(i); if (NAME_ATTR.equals(attName)) { name = attValue; } else if (CLASS_ATTR.equals(attName)) { className = attValue; } else if (TAGS_ATTR.equals(attName)) { tags = attValue; } } notEmpty(name, PARSER_ELMT, NAME_ATTR); notEmpty(className, PARSER_ELMT, CLASS_ATTR); if (streamsConfigData.getParser(name) != null) { throw new SAXParseException(StreamsResources.getStringFormatted(StreamsResources.RESOURCE_BUNDLE_NAME, "ConfigParserHandler.duplicate.parser.definition", name), currParseLocation); } try { Object newParser = Utils.createInstance(className); if (!(newParser instanceof ActivityParser)) { throw new SAXNotSupportedException(StreamsResources.getStringFormatted( StreamsResources.RESOURCE_BUNDLE_NAME, "ConfigParserHandler.not.implement.interface", PARSER_ELMT, CLASS_ATTR, className, ActivityParser.class.getName(), getLocationInfo())); } currParser = (ActivityParser) newParser; } catch (Exception exc) { throw new SAXException(StreamsResources.getStringFormatted(StreamsResources.RESOURCE_BUNDLE_NAME, "ConfigParserHandler.failed.to.load", PARSER_ELMT, CLASS_ATTR, className, getLocationInfo()), exc); } if (currParser != null) { currParser.setName(name); currParser.setTags(tags); streamsConfigData.addParser(currParser); } }
From source file:com.jkoolcloud.jesl.simulator.TNT4JSimulatorParserHandler.java
private void recordProperty(Attributes attributes) throws SAXException { if (curElement == null) { throw new SAXParseException("<" + SIM_XML_PROP + ">: Must have <" + SIM_XML_ACTIVITY + ">, <" + SIM_XML_EVENT + ">, or <" + SIM_XML_SNAPSHOT + "> as parent element", saxLocator); }//from w w w . j a v a 2 s . c o m String name = null; String type = null; String value = null; String valType = null; try { for (int i = 0; i < attributes.getLength(); i++) { String attName = attributes.getQName(i); String attValue = expandEnvVars(attributes.getValue(i)); if (attName.equals(SIM_XML_ATTR_NAME)) name = attValue; else if (attName.equals(SIM_XML_ATTR_TYPE)) type = attValue; else if (attName.equals(SIM_XML_ATTR_VALUE)) value = attValue; else if (attName.equals(SIM_XML_ATTR_VALTYPE)) valType = attValue; else throw new SAXParseException("Unknown <" + SIM_XML_PROP + "> attribute '" + attName + "'", saxLocator); } if (StringUtils.isEmpty(name)) throw new SAXParseException("<" + SIM_XML_PROP + ">: must specify '" + SIM_XML_ATTR_NAME + "'", saxLocator); TNT4JSimulator.trace(simCurrTime, "Recording " + curElement + " property: " + name + " ..."); Property prop = processPropertyValue(name, type, value, valType); if (SIM_XML_SNAPSHOT.equals(curElement)) curSnapshot.add(prop); else if (SIM_XML_EVENT.equals(curElement)) curEvent.getOperation().addProperty(prop); else if (SIM_XML_ACTIVITY.equals(curElement)) curActivity.addProperty(prop); } catch (Exception e) { if (e instanceof SAXException) throw (SAXException) e; throw new SAXException("Failed processing definition for property '" + name + "': " + e, e); } }
From source file:com.jkoolcloud.tnt4j.streams.configure.sax.ConfigParserHandler.java
/** * Processes a {@code <field>} element. * * @param attrs/*from w w w . j a va2 s .c o m*/ * List of element attributes * * @throws SAXException * if error occurs parsing element */ private void processField(Attributes attrs) throws SAXException { if (currField != null) { throw new SAXParseException(StreamsResources.getStringFormatted(StreamsResources.RESOURCE_BUNDLE_NAME, "ConfigParserHandler.malformed.configuration", FIELD_ELMT), currParseLocation); } if (currParser == null) { throw new SAXParseException( StreamsResources.getStringFormatted(StreamsResources.RESOURCE_BUNDLE_NAME, "ConfigParserHandler.malformed.configuration2", FIELD_ELMT, PARSER_ELMT), currParseLocation); } currFieldHasLocValAttr = false; // currFieldHasLocElmt = false; // currFieldHasMapElmt = false; String field = null; ActivityFieldDataType dataType = null; String locatorType = null; String locator = null; String valueType = null; String separator = null; String pattern = null; String units = null; String format = null; String locale = null; String timeZone = null; String value = null; int radix = 10; String reqVal = ""; boolean transparent = false; boolean split = false; String id = null; for (int i = 0; i < attrs.getLength(); i++) { String attName = attrs.getQName(i); String attValue = attrs.getValue(i); if (NAME_ATTR.equals(attName)) { field = attValue; } else if (DATA_TYPE_ATTR.equals(attName)) { dataType = ActivityFieldDataType.valueOf(attValue); } else if (LOC_TYPE_ATTR.equals(attName)) { locatorType = attValue; } else if (LOCATOR_ATTR.equals(attName)) { locator = attValue; } else if (VALUE_TYPE_ATTR.equals(attName)) { valueType = attValue; } else if (SEPARATOR_ATTR.equals(attName)) { separator = attValue; } else if (FORMATTING_PATTERN_ATTR.equals(attName)) pattern = attValue; else if (RADIX_ATTR.equals(attName)) { radix = Integer.parseInt(attValue); } else if (UNITS_ATTR.equals(attName)) { units = attValue; } else if (FORMAT_ATTR.equals(attName)) { format = attValue; } else if (LOCALE_ATTR.equals(attName)) { locale = attValue; } else if (TIMEZONE_ATTR.equals(attName)) { timeZone = attValue; } else if (VALUE_ATTR.equals(attName)) { value = attValue; } else if (REQUIRED_ATTR.equals(attName)) { reqVal = attValue; } else if (TRANSPARENT_ATTR.equals(attName)) { transparent = Boolean.parseBoolean(attValue); } else if (SPLIT_ATTR.equals(attName)) { split = Boolean.parseBoolean(attValue); } else if (ID_ATTR.equals(attName)) { id = attValue; } } // make sure required fields are present notEmpty(field, FIELD_ELMT, NAME_ATTR); if (separator != null && pattern != null) { throw new SAXParseException(StreamsResources.getStringFormatted(StreamsResources.RESOURCE_BUNDLE_NAME, "ConfigParserHandler.cannot.contain", FIELD_ELMT, SEPARATOR_ATTR, FORMATTING_PATTERN_ATTR), currParseLocation); } ActivityFieldLocator afl; ActivityField af = new ActivityField(field); if (value != null) { currFieldHasLocValAttr = true; afl = new ActivityFieldLocator(value); afl.setRadix(radix); afl.setRequired(reqVal); if (dataType != null) { afl.setDataType(dataType); } if (StringUtils.isNotEmpty(units)) { afl.setUnits(units); } if (StringUtils.isNotEmpty(format)) { afl.setFormat(format, locale); } if (StringUtils.isNotEmpty(timeZone)) { afl.setTimeZone(timeZone); } if (StringUtils.isNotEmpty(id)) { afl.setId(id); } af.addLocator(afl); } else if (StringUtils.isNotEmpty(locator)) { currFieldHasLocValAttr = true; String[] locators = currParser.canHaveDelimitedLocators() ? locator.split(Pattern.quote(LOC_DELIM)) : new String[] { locator }; for (String loc : locators) { if (StringUtils.isNotEmpty(loc)) { afl = new ActivityFieldLocator(locatorType, loc); afl.setRadix(radix); afl.setRequired(reqVal); if (dataType != null) { afl.setDataType(dataType); } if (StringUtils.isNotEmpty(units)) { afl.setUnits(units); } if (StringUtils.isNotEmpty(format)) { afl.setFormat(format, locale); } if (StringUtils.isNotEmpty(timeZone)) { afl.setTimeZone(timeZone); } if (StringUtils.isNotEmpty(id)) { afl.setId(id); } af.addLocator(afl); } } } else if (StringUtils.isEmpty(locator)) { af.setGroupLocator(radix, reqVal, dataType, units, format, locale, timeZone); } if (separator != null) { af.setSeparator(separator); } if (StringUtils.isNotEmpty(pattern)) { af.setFormattingPattern(pattern); } if (StringUtils.isNotEmpty(valueType)) { af.setValueType(valueType); } af.setRequired(reqVal); af.setTransparent(transparent); af.setSplitCollection(split); currField = af; }
From source file:com.jkoolcloud.jesl.simulator.TNT4JSimulatorParserHandler.java
private void recordMessage(Attributes attributes) throws SAXException { if (TNT4JSimulator.getIteration() > 1L) return;/* w w w . j a v a 2s . c om*/ if (!SIM_XML_ROOT.equals(curElement)) throw new SAXParseException("<" + SIM_XML_MSG + ">: must have <" + SIM_XML_ROOT + "> as parent element", saxLocator); int id = 0; String mimeType = null; String encoding = null; String charset = null; String fileName = null; boolean isBinary = false; try { for (int i = 0; i < attributes.getLength(); i++) { String attName = attributes.getQName(i); String attValue = expandEnvVars(attributes.getValue(i)); if (attName.equals(SIM_XML_ATTR_ID)) { id = Integer.parseInt(attValue); TNT4JSimulator.trace(simCurrTime, "Recording Message: " + attValue + " ..."); } else if (attName.equals(SIM_XML_ATTR_MIME)) { mimeType = attValue; } else if (attName.equals(SIM_XML_ATTR_ENC)) { encoding = attValue; } else if (attName.equals(SIM_XML_ATTR_CHARSET)) { charset = attValue; } else if (attName.equals(SIM_XML_ATTR_FILE)) { fileName = attValue; } else if (attName.equals(SIM_XML_ATTR_BINFILE)) { isBinary = Boolean.parseBoolean(attValue); } else { throw new SAXParseException("Unknown <" + SIM_XML_MSG + "> attribute '" + attName + "'", saxLocator); } } if (id == 0) throw new SAXParseException("<" + SIM_XML_MSG + ">: missing or invalid '" + SIM_XML_ATTR_ID + "'", saxLocator); curMsg = messageIds.get(id); if (curMsg == null) { curMsg = new Message(TNT4JSimulator.newUUID()); messageIds.put(id, curMsg); } if (fileName != null) { if (isBinary) { BufferedInputStream fileReader = null; try { File f = new File(fileName); fileReader = new BufferedInputStream(new FileInputStream(f)); byte[] binData = new byte[(int) f.length()]; int totalBytesRead = 0; while (totalBytesRead < binData.length) { int bytesRemaining = binData.length - totalBytesRead; int bytesRead = fileReader.read(binData, totalBytesRead, bytesRemaining); if (bytesRead > 0) totalBytesRead += bytesRead; curMsg.setMessage(binData); } } catch (Exception e) { throw new SAXParseException("Failed loading message data from " + fileName, saxLocator, e); } finally { if (fileReader != null) try { fileReader.close(); } catch (Exception e1) { } } } else { FileReader fileReader = null; try { fileReader = new FileReader(fileName); StringBuffer msgData = new StringBuffer(); char[] text = new char[2048]; int numRead = 0; while ((numRead = fileReader.read(text, 0, text.length)) > 0) msgData.append(text, 0, numRead); curMsg.setMessage(msgData.toString()); } catch (Exception e) { throw new SAXParseException("Failed loading message data from " + fileName, saxLocator, e); } finally { if (fileReader != null) try { fileReader.close(); } catch (Exception e1) { } } } curMsg = null; // to ignore msg element value } if (!StringUtils.isEmpty(mimeType)) curMsg.setMimeType(mimeType); if (!StringUtils.isEmpty(encoding)) curMsg.setEncoding(encoding); if (!StringUtils.isEmpty(charset)) curMsg.setCharset(charset); } catch (Exception e) { if (e instanceof SAXException) throw (SAXException) e; throw new SAXException("Failed processing definition for message '" + id + "': " + e, e); } }
From source file:org.esco.grouper.parsing.SGSParsingUtil.java
/** * Handles an attribute error.//from w w w. j ava 2s. c o m * @param tag The current tag. * @param attribute The attribute. * @param legalValues The string that represents the legal values for the attribute. * @param actualValue The actual value of the attribute. * @throws SAXParseException The thrown exception. */ private void handleAttributeError(final String tag, final String attribute, final String legalValues, final String actualValue) throws SAXParseException { String msg = "Tag: " + tag + " - Path of the tag: " + current.getPath() + " - Invalid value for the attribute {" + attribute + "}" + " (line " + locator.getLineNumber() + ")"; if (legalValues != null && !"".equals(legalValues)) { msg += " - Legal values are: " + legalValues; } if (actualValue != null) { msg += " - Actual value is: " + actualValue; } msg += "."; LOGGER.error(msg); throw new SAXParseException(msg, locator); }
From source file:com.jkoolcloud.tnt4j.streams.configure.sax.ConfigParserHandler.java
/** * Checks if attribute resolved {@link String} value is not empty. * * @param attrValue// w w w . j a va2s . co m * attribute resolved value * @param elemName * element name * @param attrName * attribute name * @throws SAXParseException * if attribute resolved {@link String} value is {@code null} or {@code ""} */ protected void notEmpty(String attrValue, String elemName, String attrName) throws SAXParseException { if (StringUtils.isEmpty(attrValue)) { throw new SAXParseException(StreamsResources.getStringFormatted(StreamsResources.RESOURCE_BUNDLE_NAME, "ConfigParserHandler.missing.attribute", elemName, attrName), currParseLocation); } }
From source file:com.jkoolcloud.tnt4j.streams.configure.sax.ConfigParserHandler.java
/** * Checks if element resolved {@link String} value is not empty. * * @param attrValue//from www. ja va2 s. c o m * attribute resolved value * @param elemName * element name * @throws SAXParseException * if attribute resolved {@link String} value is {@code null} or {@code ""} */ protected void notEmpty(String attrValue, String elemName) throws SAXParseException { if (StringUtils.isEmpty(attrValue)) { throw new SAXParseException(StreamsResources.getStringFormatted(StreamsResources.RESOURCE_BUNDLE_NAME, "ConfigParserHandler.missing.element", elemName), currParseLocation); } }
From source file:eu.elf.license.LicenseParser.java
/** * Creates DOM Document object from XML string * * @param xmlString//from w ww.ja v a 2s.c o m * @return xml document * @throws Exception */ public static Document createXMLDocumentFromString(String xmlString) throws Exception { Document document = null; try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder builder = dbf.newDocumentBuilder(); InputStream is = new ByteArrayInputStream(xmlString.getBytes("UTF-8")); document = builder.parse(is); } catch (SAXParseException spe) { Locator2Impl locator = new Locator2Impl(); spe.printStackTrace(); throw new SAXParseException("", locator); } catch (Exception e) { e.printStackTrace(); } return document; }
From source file:com.jkoolcloud.tnt4j.streams.configure.sax.ConfigParserHandler.java
/** * Checks if attribute resolved value is not {@code null}. * * @param attrValue//from w w w . j a va2 s.c o m * attribute resolved value * @param elemName * element name * @param attrName * attribute name * * @throws SAXParseException * if attribute resolved value is {@code null} */ protected void notNull(Object attrValue, String elemName, String attrName) throws SAXParseException { if (attrValue == null) { throw new SAXParseException(StreamsResources.getStringFormatted(StreamsResources.RESOURCE_BUNDLE_NAME, "ConfigParserHandler.missing.attribute", elemName, attrName), currParseLocation); } }
From source file:com.jkoolcloud.tnt4j.streams.configure.sax.ConfigParserHandler.java
/** * Processes a {@code <embedded-activity>} element. * * @param attrs/*from www. j a v a 2s . c o m*/ * List of element attributes * * @throws SAXException * if error occurs parsing element */ private void processEmbeddedActivity(Attributes attrs) throws SAXException { if (currField != null) { throw new SAXParseException( StreamsResources.getStringFormatted(StreamsResources.RESOURCE_BUNDLE_NAME, "ConfigParserHandler.malformed.configuration5", EMBEDDED_ACTIVITY_ELMT, FIELD_ELMT), currParseLocation); } if (currParser == null) { throw new SAXParseException( StreamsResources.getStringFormatted(StreamsResources.RESOURCE_BUNDLE_NAME, "ConfigParserHandler.malformed.configuration2", EMBEDDED_ACTIVITY_ELMT, PARSER_ELMT), currParseLocation); } String field = null; String locatorType = null; String locator = null; String reqVal = ""; boolean transparent = true; boolean split = true; String id = null; for (int i = 0; i < attrs.getLength(); i++) { String attName = attrs.getQName(i); String attValue = attrs.getValue(i); if (NAME_ATTR.equals(attName)) { field = attValue; } else if (LOC_TYPE_ATTR.equals(attName)) { locatorType = attValue; } else if (LOCATOR_ATTR.equals(attName)) { locator = attValue; } else if (REQUIRED_ATTR.equals(attName)) { reqVal = attValue; } else if (ID_ATTR.equals(attName)) { id = attValue; } } // make sure required fields are present notEmpty(field, EMBEDDED_ACTIVITY_ELMT, NAME_ATTR); notEmpty(locator, EMBEDDED_ACTIVITY_ELMT, LOCATOR_ATTR); notEmpty(locatorType, EMBEDDED_ACTIVITY_ELMT, LOC_TYPE_ATTR); ActivityFieldLocator afl; ActivityField af = new ActivityField(field); String[] locators = currParser.canHaveDelimitedLocators() ? locator.split(Pattern.quote(LOC_DELIM)) : new String[] { locator }; for (String loc : locators) { if (StringUtils.isNotEmpty(loc)) { afl = new ActivityFieldLocator(locatorType, loc); afl.setRequired(reqVal); if (StringUtils.isNotEmpty(id)) { afl.setId(id); } af.addLocator(afl); } } af.setRequired(reqVal); af.setTransparent(transparent); af.setSplitCollection(split); currField = af; }