List of usage examples for org.dom4j DocumentException DocumentException
public DocumentException(Throwable cause)
From source file:com.zimbra.perf.chart.XMLChartConfig.java
License:Open Source License
private static String getAttr(Element elem, String name) throws DocumentException { String val = elem.attributeValue(name, null); if (val != null) return val; else/*from ww w. ja va 2 s. c om*/ throw new DocumentException("Missing required attribute " + name + " in element " + elem.getName()); }
From source file:com.zimbra.perf.chart.XMLChartConfig.java
License:Open Source License
private static int getInheritedAttrInt(Element elem, String name, int defaultValue) throws DocumentException { String val = getInheritedAttr(elem, name, null); if (val != null) { try {//from ww w .j a va 2 s . c om int i = Integer.parseInt(val); return i; } catch (NumberFormatException ex) { throw new DocumentException("Invalid integer value " + val + " for attribute " + name + " in element " + elem.getName()); } } else return defaultValue; }
From source file:com.zimbra.perf.chart.XMLChartConfig.java
License:Open Source License
private static double getInheritedAttrDouble(Element elem, String name, double defaultValue) throws DocumentException { String val = getInheritedAttr(elem, name, null); if (val != null) { int len = val.length(); if (len == 0) return defaultValue; char unit = val.toLowerCase().charAt(len - 1); int multiplier = 1; if (unit == 'k') multiplier = 1024;/* w w w. ja va 2s .c o m*/ else if (unit == 'm') multiplier = 1024 * 1024; else if (unit == 'g') multiplier = 1024 * 1024 * 1024; String digits; if (multiplier == 1) digits = val; else digits = val.substring(0, len - 1); try { double d = Double.parseDouble(digits); return d * multiplier; } catch (NumberFormatException ex) { throw new DocumentException( "Invalid double value " + val + " for attribute " + name + " in element " + elem.getName()); } } else return defaultValue; }
From source file:com.zimbra.perf.chart.XMLChartConfig.java
License:Open Source License
/** * Loads chart settings from the specified XML file. * * @throws IOException/*from w ww.j av a 2 s.c o m*/ * if there was an error reading the file * @throws DocumentException * if there was an error parsing the file * @throws XmlParseException * @throws IllegalArgumentException * if any attribute has invalid value */ public static List<ChartSettings> load(File xmlFile) throws IOException, DocumentException, XmlParseException { List<ChartSettings> charts = new ArrayList<ChartSettings>(); Document document; try (FileInputStream fis = new FileInputStream(xmlFile)) { document = W3cDomUtil.parseXMLToDom4jDocUsingSecureProcessing(fis); } Element chartsElem = document.getRootElement(); if (!chartsElem.getName().equals(E_CHARTS)) { throw new DocumentException("Missing <" + E_CHARTS + "> root element"); } for (Iterator iter = chartsElem.elementIterator(E_CHART); iter.hasNext();) { Element chartElem = (Element) iter.next(); String chartTitle = getAttr(chartElem, A_CHART_TITLE); String category = getAttr(chartElem, A_CHART_CATEGORY, "unknown"); String outfile = getAttr(chartElem, A_CHART_OUTFILE); // inheritable attributes String xAxis = getInheritedAttr(chartElem, A_CHART_XAXIS, ChartSettings.DEFAULT_CHART_XAXIS); String yAxis = getInheritedAttr(chartElem, A_CHART_YAXIS, ""); boolean allowLogScale = getInheritedAttrBoolean(chartElem, A_CHART_ALLOW_LOG_SCALE, ChartSettings.DEFAULT_CHART_ALLOW_LOG_SCALE); boolean plotZero = getInheritedAttrBoolean(chartElem, A_CHART_PLOT_ZERO, ChartSettings.DEFAULT_CHART_PLOT_ZERO); if (!allowLogScale) plotZero = true; int width = getInheritedAttrInt(chartElem, A_CHART_WIDTH, ChartSettings.DEFAULT_CHART_WIDTH); int height = getInheritedAttrInt(chartElem, A_CHART_HEIGHT, ChartSettings.DEFAULT_CHART_HEIGHT); String outDoc = getAttr(chartElem, A_CHART_DOCUMENT, null); String topPlotStr = getAttr(chartElem, A_CHART_TOP_PLOTS, null); int topPlots = -1; if (topPlotStr != null) topPlots = Integer.parseInt(topPlotStr); topPlotStr = getAttr(chartElem, A_CHART_TOP_PLOTS_TYPE, "max"); ChartSettings.TopPlotsType topPlotsType = ChartSettings.TopPlotsType.valueOf(topPlotStr.toUpperCase()); ChartSettings chart = new ChartSettings(chartTitle, category, outfile, xAxis, yAxis, allowLogScale, plotZero, width, height, outDoc, topPlots, topPlotsType); for (Iterator plotIter = chartElem.elementIterator(E_PLOT); plotIter.hasNext();) { Element plotElem = (Element) plotIter.next(); String dataCol = getAttr(plotElem, A_PLOT_DATA_COLUMN, null); // inheritable attributes String legend = getInheritedAttr(plotElem, A_PLOT_LEGEND, null); String infile = getInheritedAttr(plotElem, A_PLOT_INFILE, null); boolean showRaw = getInheritedAttrBoolean(plotElem, A_PLOT_SHOW_RAW, PlotSettings.DEFAULT_PLOT_SHOW_RAW); boolean showMovingAvg = getInheritedAttrBoolean(plotElem, A_PLOT_SHOW_MOVING_AVG, PlotSettings.DEFAULT_PLOT_SHOW_MOVING_AVG); int movingAvgPoints = getInheritedAttrInt(plotElem, A_PLOT_MOVING_AVG_POINTS, PlotSettings.DEFAULT_PLOT_MOVING_AVG_POINTS); double multiplier = getInheritedAttrDouble(plotElem, A_PLOT_MULTIPLIER, PlotSettings.DEFAULT_PLOT_MULTIPLIER); double divisor = getInheritedAttrDouble(plotElem, A_PLOT_DIVISOR, PlotSettings.DEFAULT_PLOT_DIVISOR); boolean nonNegative = getInheritedAttrBoolean(plotElem, A_PLOT_NON_NEGATIVE, PlotSettings.DEFAULT_PLOT_NON_NEGATIVE); boolean percentTime = getInheritedAttrBoolean(plotElem, A_PLOT_PERCENT_TIME, PlotSettings.DEFAULT_PLOT_PERCENT_TIME); String dataFunction = getAttr(plotElem, A_PLOT_DATA_FUNCTION, PlotSettings.DEFAULT_PLOT_DATA_FUNCTION); String aggFunction = getAttr(plotElem, A_PLOT_AGGREGATE_FUNCTION, PlotSettings.DEFAULT_PLOT_AGGREGATE_FUNCTION); String ratioTop = getAttr(plotElem, A_PLOT_RATIO_TOP, null); String ratioBottom = getAttr(plotElem, A_PLOT_RATIO_BOTTOM, null); if ((ratioTop == null && ratioBottom != null) || (ratioTop != null && ratioBottom == null)) { throw new DocumentException("Both ratioTop/ratioBottom need to be specified"); } if ((ratioTop == null && dataCol == null) || (ratioTop != null && dataCol != null)) { throw new DocumentException("Specify either ratio or data"); } boolean optional = getInheritedAttrBoolean(plotElem, A_PLOT_OPTIONAL, PlotSettings.DEFAULT_PLOT_OPTIONAL); PlotSettings plot = new PlotSettings(legend, infile, dataCol, showRaw, showMovingAvg, movingAvgPoints, multiplier, divisor, nonNegative, percentTime, dataFunction, aggFunction, optional, ratioTop, ratioBottom); chart.addPlot(plot); } for (Iterator plotIter = chartElem.elementIterator(E_GROUP_PLOT); plotIter.hasNext();) { Element plotElem = (Element) plotIter.next(); String dataCol = getAttr(plotElem, A_PLOT_DATA_COLUMN); // inheritable attributes String infile = getInheritedAttr(plotElem, A_PLOT_INFILE, null); boolean showRaw = getInheritedAttrBoolean(plotElem, A_PLOT_SHOW_RAW, PlotSettings.DEFAULT_PLOT_SHOW_RAW); boolean showMovingAvg = getInheritedAttrBoolean(plotElem, A_PLOT_SHOW_MOVING_AVG, PlotSettings.DEFAULT_PLOT_SHOW_MOVING_AVG); int movingAvgPoints = getInheritedAttrInt(plotElem, A_PLOT_MOVING_AVG_POINTS, PlotSettings.DEFAULT_PLOT_MOVING_AVG_POINTS); double multiplier = getInheritedAttrDouble(plotElem, A_PLOT_MULTIPLIER, PlotSettings.DEFAULT_PLOT_MULTIPLIER); double divisor = getInheritedAttrDouble(plotElem, A_PLOT_DIVISOR, PlotSettings.DEFAULT_PLOT_DIVISOR); boolean nonNegative = getInheritedAttrBoolean(plotElem, A_PLOT_NON_NEGATIVE, PlotSettings.DEFAULT_PLOT_NON_NEGATIVE); boolean percentTime = getInheritedAttrBoolean(plotElem, A_PLOT_PERCENT_TIME, PlotSettings.DEFAULT_PLOT_PERCENT_TIME); String dataFunction = getAttr(plotElem, A_PLOT_DATA_FUNCTION, PlotSettings.DEFAULT_PLOT_DATA_FUNCTION); String aggFunction = getAttr(plotElem, A_PLOT_AGGREGATE_FUNCTION, PlotSettings.DEFAULT_PLOT_AGGREGATE_FUNCTION); String groupBy = getAttr(plotElem, A_PLOT_GROUP_BY); String ignore = getAttr(plotElem, A_PLOT_IGNORE, null); boolean optional = getInheritedAttrBoolean(plotElem, A_PLOT_OPTIONAL, PlotSettings.DEFAULT_PLOT_OPTIONAL); GroupPlotSettings plot = new GroupPlotSettings(groupBy, ignore, infile, dataCol, showRaw, showMovingAvg, movingAvgPoints, multiplier, divisor, nonNegative, percentTime, dataFunction, aggFunction, optional); chart.addPlot(plot); } charts.add(chart); } return charts; }
From source file:de.ailis.xadrian.data.Complex.java
License:Open Source License
/** * Loads a complex from the specified XML document and returns it. * * @param document/*from ww w . ja va 2 s . c o m*/ * The XML document * @return The complex * @throws DocumentException * If XML file could not be read */ public static Complex fromXML(final Document document) throws DocumentException { final Element root = document.getRootElement(); // Check the version final String versionStr = root.attributeValue("version"); int version = 1; if (versionStr != null) version = Integer.parseInt(versionStr); if (version > 4) throw new DocumentException(I18N.getString("error.fileFormatTooNew")); // Determine the game for this complex. String gameId = "x3tc"; if (version == 4) gameId = root.attributeValue("game"); final Game game = GameFactory.getInstance().getGame(gameId); final Complex complex = new Complex(game); final FactoryFactory factoryFactory = game.getFactoryFactory(); final SectorFactory sectorFactory = game.getSectorFactory(); final WareFactory wareFactory = game.getWareFactory(); final SunFactory sunsFactory = game.getSunFactory(); complex.setSuns(sunsFactory.getSun(Integer.parseInt(root.attributeValue("suns")))); complex.setSector(sectorFactory.getSector(root.attributeValue("sector"))); complex.setAddBaseComplex(Boolean.parseBoolean(root.attributeValue("addBaseComplex", "false"))); complex.showingProductionStats = Boolean .parseBoolean(root.attributeValue("showingProductionStats", "false")); complex.showingShoppingList = Boolean.parseBoolean(root.attributeValue("showingShoppingList", "false")); complex.showingStorageCapacities = Boolean .parseBoolean(root.attributeValue("showingStorageCapacities", "false")); complex.showingComplexSetup = Boolean.parseBoolean(root.attributeValue("showingComplexSetup", "true")); // Get the factories parent element (In older version this was the root // node) Element factoriesE = root.element("complexFactories"); if (factoriesE == null) factoriesE = root; // Read the complex factories for (final Object item : factoriesE.elements("complexFactory")) { final Element element = (Element) item; final Factory factory = factoryFactory.getFactory(element.attributeValue("factory")); final ComplexFactory complexFactory; final Element yieldsE = element.element("yields"); if (yieldsE == null) { final int yield = Integer.parseInt(element.attributeValue("yield", "0")); final int quantity = Integer.parseInt(element.attributeValue("quantity")); complexFactory = new ComplexFactory(game, factory, quantity, yield); } else { final List<Integer> yields = new ArrayList<Integer>(); for (final Object yieldItem : yieldsE.elements("yield")) { final Element yieldE = (Element) yieldItem; yields.add(Integer.parseInt(yieldE.getText())); } complexFactory = new ComplexFactory(game, factory, yields); } if (Boolean.parseBoolean(element.attributeValue("disabled", "false"))) complexFactory.disable(); complex.addFactory(complexFactory); } // Read the complex wares final Element waresE = root.element("complexWares"); if (waresE != null) { complex.customPrices.clear(); for (final Object item : waresE.elements("complexWare")) { final Element element = (Element) item; final Ware ware = wareFactory.getWare(element.attributeValue("ware")); final boolean use = Boolean.parseBoolean(element.attributeValue("use")); final int price = Integer.parseInt(element.attributeValue("price")); complex.customPrices.put(ware, use ? price : -price); } } final Element builtE = root.element("built"); if (builtE != null) { complex.builtKits = Integer.parseInt(builtE.attributeValue("kits", "0")); for (final Object item : builtE.elements("factory")) { final Element element = (Element) item; final String id = element.attributeValue("id"); final int quantity = Integer.parseInt(element.attributeValue("quantity")); complex.builtFactories.put(id, quantity); } } complex.calculateBaseComplex(); return complex; }
From source file:de.fct.companian.analyze.mvn.helper.PomHelper.java
License:Apache License
public PomHelper(File pomFile) throws DocumentException { this.pomFile = pomFile; this.document = null; SAXReader reader = new SAXReader(); reader.setEncoding("ISO-8859-1"); reader.setIgnoreComments(true);// w w w. j a v a 2 s . c o m reader.setValidation(false); try { this.document = reader.read(this.pomFile); } catch (Throwable t) { t.printStackTrace(); } if (this.document != null) { Element projectElement = this.document.getRootElement(); Namespace defaultNS = projectElement.getNamespace(); if (logger.isDebugEnabled()) { logger.debug("extractPomInfo() using default namespace " + defaultNS.getURI()); } Map<String, String> nsMap = new HashMap<String, String>(); nsMap.put("mvn", defaultNS.getURI()); this.nsContext = new SimpleNamespaceContext(nsMap); } else { throw new DocumentException("Could not create document."); } }
From source file:edu.umd.cs.findbugs.workflow.Update.java
License:Open Source License
public void doit(String[] args) throws IOException, DocumentException { DetectorFactoryCollection.instance(); UpdateCommandLine commandLine = new UpdateCommandLine(); int argCount = commandLine.parse(args, 1, Integer.MAX_VALUE, USAGE); if (commandLine.outputFilename == null) { verbose = false;//from ww w. j a va 2 s .c o m } if (mostRecent > 0) { argCount = Math.max(argCount, args.length - mostRecent); } String[] firstPathParts = getFilePathParts(args[argCount]); int commonPrefix = firstPathParts.length; for (int i = argCount + 1; i <= (args.length - 1); i++) { commonPrefix = Math.min(commonPrefix, lengthCommonPrefix(firstPathParts, getFilePathParts(args[i]))); } String origFilename = args[argCount++]; BugCollection origCollection; origCollection = new SortedBugCollection(); if (verbose) { System.out.println("Starting with " + origFilename); } while (true) { try { while (true) { File f = new File(origFilename); if (f.length() > 0) { break; } if (verbose) { System.out.println("Empty input file: " + f); } origFilename = args[argCount++]; } origCollection.readXML(origFilename); break; } catch (Exception e) { if (verbose) { System.out.println("Error reading " + origFilename); e.printStackTrace(System.out); } origFilename = args[argCount++]; } } if (commandLine.overrideRevisionNames || origCollection.getReleaseName() == null || origCollection.getReleaseName().length() == 0) { if (commonPrefix >= firstPathParts.length) { // This should only happen if either // // (1) there is only one input file, or // (2) all of the input files have the same name // // In either case, make the release name the same // as the file part of the input file(s). commonPrefix = firstPathParts.length - 1; } origCollection.setReleaseName(firstPathParts[commonPrefix]); if (useAnalysisTimes) { origCollection.setTimestamp(origCollection.getAnalysisTimestamp()); } } for (BugInstance bug : origCollection.getCollection()) { if (bug.getLastVersion() >= 0 && bug.getFirstVersion() > bug.getLastVersion()) { throw new IllegalStateException( "Illegal Version range: " + bug.getFirstVersion() + ".." + bug.getLastVersion()); } } discardUnwantedBugs(origCollection); while (argCount <= (args.length - 1)) { BugCollection newCollection = new SortedBugCollection(); String newFilename = args[argCount++]; if (verbose) { System.out.println("Merging " + newFilename); } try { File f = new File(newFilename); if (f.length() == 0) { if (verbose) { System.out.println("Empty input file: " + f); } continue; } newCollection.readXML(newFilename); if (commandLine.overrideRevisionNames || newCollection.getReleaseName() == null || newCollection.getReleaseName().length() == 0) { newCollection.setReleaseName(getFilePathParts(newFilename)[commonPrefix]); } if (useAnalysisTimes) { newCollection.setTimestamp(newCollection.getAnalysisTimestamp()); } discardUnwantedBugs(newCollection); origCollection = mergeCollections(origCollection, newCollection, true, false); } catch (IOException e) { IOException e2 = new IOException("Error parsing " + newFilename); e2.initCause(e); if (verbose) { e2.printStackTrace(); } throw e2; } catch (DocumentException e) { DocumentException e2 = new DocumentException("Error parsing " + newFilename); e2.initCause(e); if (verbose) { e2.printStackTrace(); } throw e2; } } /* if (false) { for (Iterator<BugInstance> i = origCollection.iterator(); i.hasNext();) { if (!resurrected.contains(i.next().getInstanceKey())) { i.remove(); } } } */ origCollection.setWithMessages(commandLine.withMessages); if (commandLine.outputFilename != null) { if (verbose) { System.out.println("Writing " + commandLine.outputFilename); } origCollection.writeXML(commandLine.outputFilename); } else { origCollection.writeXML(System.out); } }
From source file:gr.omadak.leviathan.asp.objects.XmlObjectParser.java
License:Open Source License
public void configureMethods(Map placeIn, URL url) throws DocumentException { Document doc = loadDocument(url); Element root = doc.getRootElement(); List classes = doc.selectNodes("class"); Map classMap = classes.isEmpty() ? Collections.EMPTY_MAP : collectInnerClasses(classes); List methods = root.selectNodes("method"); StringBuffer errors = new StringBuffer(); for (Iterator it = methods.iterator(); it.hasNext();) { Element methodDef = (Element) it.next(); try {//from w w w . jav a2s.c o m Method method = configureMethod(methodDef, classMap); String name = method.getName().toUpperCase(); if (placeIn.containsKey(name)) { Object obj = placeIn.get(name); if (obj instanceof List) { ((List) obj).add(method); } else { List item = new ArrayList(); item.add(obj); item.add(method); placeIn.put(name, item); } } else { placeIn.put(name, method); } } catch (DocumentException docex) { errors.append(docex).append('\n'); } } if (errors.length() > 0) { throw new DocumentException(url + "\n" + errors.toString()); } }
From source file:gr.omadak.leviathan.asp.objects.XmlObjectParser.java
License:Open Source License
private Method configureMethod(Element methodEl, Map innerClasses) throws DocumentException { String methodName = methodEl.attributeValue("name"); assertValue(methodName, "Name not specified for method"); String type = methodEl.attributeValue("type"); assertValue(type, "Method type not specified"); String use = methodEl.attributeValue("use"); Method method = null;/*from w w w . ja va 2 s . co m*/ int dType; try { dType = getType(type); } catch (DocumentException de) { dType = objectType; } if (use != null) { ASPDependantClass aspClazz = (ASPDependantClass) getUseClass(use, innerClasses); if (aspClazz != null) { ASPMethodWrapper mWrapper = new ASPMethodWrapper(aspClazz); mWrapper.setName(methodName); mWrapper.setReturnType(dType); method = mWrapper; } } else { ASPClass retClass = null; if (!"OBJECT".equals(type)) { if (dType == objectType) { retClass = getUseClass(type, innerClasses); if (retClass == null) { throw new DocumentException("in method:" + methodName + " type:" + type + " is not recognized and is not a known class"); } } } List args = getArgs(methodEl); Element map = methodEl.element("map"); if (map != null) { method = new ASPMethodMap(methodName, dType, args); configureMethodMap((ASPMethodMap) method, map, innerClasses); } else { AST translation = translate(methodEl, null, false, 0); method = new GenericMethod(methodName, dType, args, translation); } String id = methodEl.attributeValue("id"); if (id != null) { if (idMethods == null) { idMethods = new HashMap(); idMethods.put(id, new Object[] { method, null, null }); } else { Object[] mem = (Object[]) idMethods.get(id); mem[0] = method; } } if (retClass != null) { method.setRetObjectClass(retClass); } } if (method != null) { configureInclude(method, methodEl); } return method; }
From source file:gr.omadak.leviathan.asp.objects.XmlObjectParser.java
License:Open Source License
private void configureMethod(GenericClass clazz, Element methodEl, Map innerClasses) throws DocumentException { Method method = configureMethod(methodEl, innerClasses); if (method != null) { boolean isDefault = Boolean.valueOf(methodEl.attributeValue("default")).booleanValue(); if (isDefault) { clazz.setDefaultMethod(method); } else {/*w w w . jav a2s . c o m*/ clazz.addMember(method); } } else { throw new DocumentException(url + ": Failed to create method:" + methodEl.getText()); } }