List of usage examples for org.xml.sax SAXParseException SAXParseException
public SAXParseException(String message, Locator locator)
From source file:com.netspective.commons.xml.AbstractContentHandler.java
public void includeInputSource(ContentHandlerNodeStackEntry activeEntry, Attributes attrs) throws ContentHandlerException, ParserConfigurationException, SAXException, IOException, TransformProcessingInstructionEncounteredException { String resourceName = attrs.getValue(NodeIdentifiers.ATTRNAME_INCLUDE_RESOURCE); String templateName = attrs.getValue(NodeIdentifiers.ATTRNAME_INCLUDE_TEMPLATE); if (resourceName != null && resourceName.length() > 0) { Resource resource = null; Object relativeTo = activeEntry.getResourceIncludeRelativeTo(); String relativeToExpr = attrs.getValue(NodeIdentifiers.ATTRNAME_INCLUDE_RESOURCE_RELATIVE_TO); if (relativeToExpr != null) relativeTo = evaluateHandlerExpression(relativeToExpr); log.trace("Including resource '" + resourceName + "' relative to '" + relativeTo + "'."); if (relativeTo instanceof Class) resource = new Resource((Class) relativeTo, resourceName); else if (relativeTo instanceof ClassLoader) resource = new Resource((ClassLoader) relativeTo, resourceName); else if (relativeTo instanceof String) { try { resource = new Resource(Class.forName((String) relativeTo), resourceName); } catch (ClassNotFoundException e) { throw new ContentHandlerException(parseContext, "The result of '" + NodeIdentifiers.ATTRNAME_INCLUDE_RESOURCE_RELATIVE_TO + "' attribute expression '" + relativeTo + "' in <" + nodeIdentifiers.getIncludeElementName() + "> (" + relativeToExpr + ") must be either a Class or a ClassLoader."); }/*w w w. ja v a 2s. co m*/ } else if (relativeToExpr != null) throw new ContentHandlerException(parseContext, "The result of '" + NodeIdentifiers.ATTRNAME_INCLUDE_RESOURCE_RELATIVE_TO + "' attribute expression '" + relativeTo + "' in <" + nodeIdentifiers.getIncludeElementName() + "> (" + relativeToExpr + ") must be either a Class or a ClassLoader."); else resource = new Resource(activeEntry.getClass(), resourceName); ParseContext includePC = resource.getFile() != null ? activeEntry.parseInclude(parseContext, resource.getFile()) : activeEntry.parseInclude(parseContext, resource); parseContext.getErrors().addAll(includePC.getErrors()); } else if (templateName != null && templateName.length() > 0) { log.trace("Including template '" + templateName + "'."); Template template = parseContext.getTemplateCatalog() .getTemplate(nodeIdentifiers.getGenericTemplateProducer(), templateName); if (template == null) throw new SAXParseException( "Generic template '" + templateName + "' was not found in the active document.", parseContext.getLocator()); template.applyChildren( template.createApplyContext(this, nodeIdentifiers.getIncludeElementName(), attrs)); } else { String fileName = attrs.getValue(NodeIdentifiers.ATTRNAME_INCLUDE_FILE); File includeFile = parseContext.resolveFile(fileName); log.trace("Including file '" + includeFile.getAbsolutePath() + "'."); ParseContext includePC = activeEntry.parseInclude(parseContext, includeFile); parseContext.getErrors().addAll(includePC.getErrors()); } }
From source file:com.jkoolcloud.tnt4j.streams.configure.sax.WsConfigParserHandler.java
@Override public void endElement(String uri, String localName, String qName) throws SAXException { super.endElement(uri, localName, qName); try {/* www .jav a 2 s . c o m*/ if (SCENARIO_ELMT.equals(qName)) { if (currScenario.isEmpty()) { throw new SAXException(StreamsResources.getStringFormatted( StreamsResources.RESOURCE_BUNDLE_NAME, "WsConfigParserHandler.element.must.have.one", SCENARIO_ELMT, STEP_ELMT, getLocationInfo())); } ((AbstractWsStream) currStream).addScenario(currScenario); currScenario = null; } else if (STREAM_ELMT.equals(qName)) { if (currStream instanceof AbstractWsStream) { if (CollectionUtils.isEmpty(((AbstractWsStream) currStream).getScenarios())) { throw new SAXException( StreamsResources.getStringFormatted(StreamsResources.RESOURCE_BUNDLE_NAME, "WsConfigParserHandler.element.must.have.one", STREAM_ELMT, SCENARIO_ELMT, getLocationInfo())); } } } else if (STEP_ELMT.equals(qName)) { if (StringUtils.isEmpty(currStep.getRequest()) && StringUtils.isEmpty(currStep.getUrlStr())) { throw new SAXParseException( StreamsResources.getStringFormatted(StreamsResources.RESOURCE_BUNDLE_NAME, "ConfigParserHandler.must.contain", STEP_ELMT, URL_ATTR, REQ_ELMT), currParseLocation); } currScenario.addStep(currStep); currStep = null; } else if (REQ_ELMT.equals(qName)) { if (elementData != null) { currStep.setRequest(getElementData()); elementData = null; } } } catch (SAXException exc) { throw exc; } catch (Exception e) { throw new SAXException(e.getLocalizedMessage() + getLocationInfo(), e); } }
From source file:com.jkoolcloud.jesl.simulator.TNT4JSimulatorParserHandler.java
/** * {@inheritDoc}//from w w w. j a v a 2 s . c o m */ @Override public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException { curElmtValue.setLength(0); if (name.equals(SIM_XML_ROOT)) TNT4JSimulator.trace(simCurrTime, "Starting next iteration..."); else if (name.equals(SIM_XML_SOURCE)) recordSource(attributes); else if (name.equals(SIM_XML_SNAPSHOT)) recordSnapshot(attributes); else if (name.equals(SIM_XML_PROP)) recordProperty(attributes); else if (name.equals(SIM_XML_MSG)) recordMessage(attributes); else if (name.equals(SIM_XML_ACTIVITY)) startActivity(attributes); else if (name.equals(SIM_XML_EVENT)) runEvent(attributes); else if (name.equals(SIM_XML_SLEEP)) pauseSimulator(attributes); else if (name.equals(SIM_XML_VAR)) defineVar(attributes); else if (name.equals(SIM_XML_OPTION)) defineOption(attributes); else throw new SAXParseException("Unrecognized element <" + name + ">", saxLocator); activeElements.push(curElement); curElement = name; }
From source file:com.jkoolcloud.jesl.simulator.TNT4JSimulatorParserHandler.java
protected String processVarValue(String value) throws SAXParseException { double valueNext = 0; double totalValue = 0; String symbol = null;//ww w .jav a2s. c o m // If addition or multiplication are specified, then do the math. // For now, only one or the other is permitted. if (value.indexOf("+") > 0 && (value.indexOf("*") > 0)) throw new SAXParseException("Either multiplicaton or addition but not both are allowed", saxLocator); else if (value.indexOf("bet") > 0) { if ((value.substring(value.indexOf("bet") + 4, value.length()).length() > 9)) { BigInteger min = new BigInteger(value.substring(0, value.indexOf("bet") - 1)); BigInteger max = new BigInteger(value.substring(value.indexOf("bet") + 4, value.length())); BigInteger diff = max.subtract(min); diff = diff.add(new BigInteger("1")); BigInteger rnd = new BigInteger(diff.bitLength(), random); BigInteger finalVal = rnd.add(min); value = "" + finalVal; } else { int min = Integer.parseInt(value.substring(0, value.indexOf("bet") - 1)); int max = Integer.parseInt(value.substring(value.indexOf("bet") + 4, value.length())); value = "" + (random.nextInt(max - min + 1) + min); } } else if ((value.indexOf("+") > 0 && (value.indexOf("*") < 0)) || (value.indexOf("*") > 0 && (value.indexOf("+") < 0))) { symbol = (value.indexOf("+") > 0) ? "+" : "*"; totalValue = symbol.equals("*") ? 1 : 0; while (value.indexOf(symbol) > 0) { valueNext = Double.parseDouble(vars.get(value.substring(0, value.indexOf(symbol) - 1))); if (symbol.equals("+")) totalValue = totalValue + valueNext; else totalValue = totalValue * valueNext; value = value.substring(value.indexOf(symbol) + 2, value.length()); } totalValue = symbol.equals("*") ? totalValue * Double.parseDouble(value) : totalValue + Double.parseDouble(vars.get(value.substring(0, value.length()))); value = "" + totalValue; } return value; }
From source file:com.textocat.textokit.commons.io.axml.AXMLContentHandler.java
private String toTypeName(final String aType) throws SAXParseException { String type = typeAliases.get(aType); if (type == null) { type = aType;//from www. j av a 2 s . com } if (typeSystem.getType(type) == null) { throw new SAXParseException(String.format("Unknown annotation type: %s", type), locator); } return type; }
From source file:fr.lip6.move.coloane.projects.its.io.ModelHandler.java
/** * Analyze a type declaration/*ww w . jav a 2s. c om*/ * @param attributes Les attributs attache la balise * @throws SAXException if the referenced model file is bad. */ private void startType(Attributes attributes) throws SAXException { TypeList types = (TypeList) stack.peek(); int id = Integer.parseInt(attributes.getValue("id")); //$NON-NLS-1$ String name = attributes.getValue("name"); //$NON-NLS-1$ String formalism = attributes.getValue("formalism"); //$NON-NLS-1$ String workDir = new File(workFile).getParentFile().toURI().getPath(); String filePath = workDir + "/" + attributes.getValue("path"); URI file = new File(filePath).toURI(); if (file == null || !new File(filePath).canRead()) { throw new SAXException("Could not open referenced file " + filePath, null); } ITypeDeclaration type; try { type = TypeDeclarationFactory.create(name, file, types); } catch (IOException e) { throw new SAXException("Could not open referenced file " + filePath, null); } if (!formalism.equals(type.getTypeType())) { String errmsg = "Model formalism type for file " + filePath + " does not match file contents.\n " + "Expected type " + formalism + " read type " + type.getTypeType(); logger.fine(errmsg); throw new SAXParseException(errmsg, null); } // store type in hash ids.put(id, type); types.addTypeDeclaration(type); }
From source file:com.textocat.textokit.commons.io.axml.AXMLContentHandler.java
private void onAnnotationEnd(final String aType) throws SAXParseException { final String type = toTypeName(aType); Optional<Annotation> annoOpt = Iterables.tryFind(openAnnotations, new Predicate<Annotation>() { @Override//from ww w.j a v a 2 s. c o m public boolean apply(Annotation input) { return Objects.equal(type, input.getType()); } }); if (annoOpt.isPresent()) { Annotation anno = annoOpt.get(); openAnnotations.remove(anno); anno.setEnd(textBuilder.length()); annotations.add(anno); } else { throw new SAXParseException("End of an element closes an unknown annotation", locator); } }
From source file:jcurl.core.io.SetupSaxDeSer.java
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { // log.debug("[" + localName + "] [" + qName + "] [" + uri + "]"); final String elem = qName; final String parent = elems.size() > 0 ? (String) elems.peek() : null; final String grandParent = elems.size() > 1 ? (String) elems.get(elems.size() - 2) : null; elems.push(elem);/*from w ww . j a v a 2 s .c om*/ if (log.isDebugEnabled()) log.debug(elems + " " + atts.getLength()); buf.setLength(0); try { // check switch (elems.size()) { case 1: if ("jcurl".equals(elem)) ; else break; return; case 2: if ("setup".equals(elem)) ; else break; return; case 3: if ("meta".equals(elem)) ; else if ("model".equals(elem)) { modelClass = Class.forName(atts.getValue("engine")); modelProps.clear(); } else if ("positions".equals(elem)) ; else if ("speeds".equals(elem)) ; else break; return; case 4: if ("event".equals(elem)) ; else if ("game".equals(elem)) ; else if ("description".equals(elem) && "model".equals(parent)) ; else if ("param".equals(elem) && "model".equals(parent)) { final String key = atts.getValue("name"); final String val = atts.getValue("val"); final String dim = atts.getValue("dim"); if (dim != null) { modelProps.put(key, new DimVal(Double.parseDouble(val), Dim.find(dim))); } else { modelProps.put(key, val); } } else if ("rock".equals(elem)) currRock = new RockIdx(atts); else break; return; case 5: if ("a".equals(elem)) if ("positions".equals(grandParent)) setup.setAngle(currRock.idx16, getDim(atts)); else if ("speeds".equals(grandParent)) setup.setSpin(currRock.idx16, getDim(atts)); else break; else if ("x".equals(elem)) if ("positions".equals(grandParent)) setup.setPosX(currRock.idx16, getDim(atts)); else if ("speeds".equals(grandParent)) setup.setSpeedX(currRock.idx16, getDim(atts)); else break; else if ("y".equals(elem)) if ("positions".equals(grandParent)) setup.setPosY(currRock.idx16, getDim(atts)); else if ("speeds".equals(grandParent)) setup.setSpeedY(currRock.idx16, getDim(atts)); else break; else if ("out".equals(elem)) if ("positions".equals(grandParent)) setup.setPosOut(currRock.idx16); else break; else if ("nearhog".equals(elem)) if ("positions".equals(grandParent)) setup.setPosNHog(currRock.idx16); else break; else if ("to_x".equals(elem)) if ("speeds".equals(grandParent)) setup.setToX(currRock.idx16, getDim(atts)); else break; else if ("to_y".equals(elem)) if ("speeds".equals(grandParent)) setup.setToY(currRock.idx16, getDim(atts)); else break; else if ("speed".equals(elem)) if ("speeds".equals(grandParent)) setup.setSpeed(currRock.idx16, getDim(atts)); else break; else if ("spin".equals(elem)) if ("speeds".equals(grandParent)) setup.setSpin(currRock.idx16, getDim(atts)); else break; else break; return; } error(new SAXParseException("unexpected element [" + elem + "]", locator)); } catch (RuntimeException e) { log.warn("error in [" + elems + "]", e); error(new SAXParseException("error in [" + elems + "]", locator, e)); } catch (ClassNotFoundException e) { log.warn("error in [" + elems + "]", e); error(new SAXParseException("error in [" + elems + "]", locator, e)); } }
From source file:com.textocat.textokit.commons.io.axml.AXMLContentHandler.java
private String requiredAttribute(Attributes attrs, String name) throws SAXParseException { String val = attrs.getValue(name); if (val == null) { throw new SAXParseException(String.format("Missing attribute '%s'", name), locator); }//from w w w . ja va 2s .c om return val; }
From source file:com.netspective.commons.xml.AbstractContentHandler.java
protected boolean defaultHandleTemplateStartElement(String url, String localName, String qName, Attributes attributes) throws SAXException { String elementName = qName.toLowerCase(); if (!templateDefnStack.isEmpty()) { // we're inside a template already so just grab the contents TemplateElement activeTemplate = (TemplateElement) templateDefnStack.peek(); if (nodeIdentifiers.getTemplateParamDecl().equals(elementName)) { if (activeTemplate instanceof Template) { ((Template) activeTemplate).declareParameter(this, url, localName, qName, attributes); templateDefnStack.push(activeTemplate); } else throw new SAXParseException( "<" + nodeIdentifiers.getTemplateParamDecl() + "> not allowed here.", parseContext.getLocator()); } else {/*from ww w.j a va 2s.com*/ TemplateElement childNode = new TemplateElement(this, url, localName, qName, attributes); activeTemplate.addChild(childNode); templateDefnStack.push(childNode); } return true; } else if (elementName.equals(nodeIdentifiers.getTemplateElementName())) { String templateName = attributes.getValue(NodeIdentifiers.ATTRNAME_GENERIC_TEMPLATE_NAME); if (templateName == null || templateName.length() == 0) throw new SAXParseException("Template must have a '" + NodeIdentifiers.ATTRNAME_GENERIC_TEMPLATE_NAME + "' attribute in <" + elementName + "> ", parseContext.getLocator()); final Locator locator = getParseContext().getLocator(); InputSourceLocator inputSourceLocator = new InputSourceLocator(getParseContext().getInputSrcTracker(), locator.getLineNumber(), locator.getColumnNumber()); Template template = new Template(templateName, this, inputSourceLocator, parseContext.getTemplateCatalog(), nodeIdentifiers.getGenericTemplateProducer(), url, localName, qName, attributes); parseContext.getTemplateCatalog().registerTemplate(nodeIdentifiers.getGenericTemplateProducer(), templateName, template); templateDefnStack.push(template); return true; } else return false; }