private static Document parse(InputStream is) throws IndexerConfException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); try { return factory.newDocumentBuilder().parse(is); } catch (ParserConfigurationException e) { throw new IndexerConfException("Failed to parse configuration", e); } catch (IOException e) { throw new IndexerConfException("Failed to parse configuration", e); } catch (SAXException e) { throw new IndexerConfException("Failed to parse configuration", e); } }
private static Document parse(InputStream is) throws IndexerConfException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); try { return factory.newDocumentBuilder().parse(is); } catch (ParserConfigurationException e) { throw new IndexerConfException("Failed to parse configuration", e); } catch (IOException e) { throw new IndexerConfException("Failed to parse configuration", e); } catch (SAXException e) { throw new IndexerConfException("Failed to parse configuration", e); } }
public static String getAttribute(Element element, String name, boolean required) { if (!element.hasAttribute(name)) { if (required) throw new IndexerConfException("Missing attribute " + name + " on element " + element.getLocalName()); else return null; } return element.getAttribute(name); }
public static String getAttribute(Element element, String name, boolean required) { if (!element.hasAttribute(name)) { if (required) throw new IndexerConfException("Missing attribute " + name + " on element " + element.getLocalName()); else return null; } return element.getAttribute(name); }
private <T> Class<T> loadClass(String className, Class<T> baseType) { try { Class<T> clazz = (Class<T>)getClass().getClassLoader().loadClass(className); if (!baseType.isAssignableFrom(clazz)) { throw new IndexerConfException("Expected a class which inherits from " + baseType.getName() + ", which the following does not: " + clazz.getName()); } return clazz; } catch (ClassNotFoundException e) { throw new IndexerConfException("Could not load class: '" + className + "'", e); } } }
private <T> Class<T> loadClass(String className, Class<T> baseType) { try { Class<T> clazz = (Class<T>)getClass().getClassLoader().loadClass(className); if (!baseType.isAssignableFrom(clazz)) { throw new IndexerConfException("Expected a class which inherits from " + baseType.getName() + ", which the following does not: " + clazz.getName()); } return clazz; } catch (ClassNotFoundException e) { throw new IndexerConfException("Could not load class: '" + className + "'", e); } } }
private <T extends Enum> T getEnumAttribute(Class<T> enumClass, Element element, String attribute, T defaultValue) { if (!element.hasAttribute(attribute)) { return defaultValue; } String value = element.getAttribute(attribute); try { return (T)Enum.valueOf(enumClass, value.toUpperCase()); } catch (IllegalArgumentException e) { throw new IndexerConfException("Illegal value in attribute " + attribute + " on element " + element.getLocalName() + ": '" + value); } }
private <T extends Enum> T getEnumAttribute(Class<T> enumClass, Element element, String attribute, T defaultValue) { if (!element.hasAttribute(attribute)) { return defaultValue; } String value = element.getAttribute(attribute); try { return (T)Enum.valueOf(enumClass, value.toUpperCase()); } catch (IllegalArgumentException e) { throw new IndexerConfException("Illegal value in attribute " + attribute + " on element " + element.getLocalName() + ": '" + value); } }
private void validate(Document document) { try { SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); URL url = getClass().getResource("indexerconf.xsd"); Schema schema = factory.newSchema(url); Validator validator = schema.newValidator(); validator.validate(new DOMSource(document)); } catch (Exception e) { throw new IndexerConfException("Error validating index configuration against XML Schema.", e); } }
private void validate(Document document) { try { SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); URL url = getClass().getResource("indexerconf.xsd"); Schema schema = factory.newSchema(url); Validator validator = schema.newValidator(); validator.validate(new DOMSource(document)); } catch (Exception e) { throw new IndexerConfException("Error validating index configuration against XML Schema.", e); } }
private List<Element> evalXPathAsElementList(String expression, Node node) { try { XPathExpression expr = XPathFactory.newInstance().newXPath().compile(expression); NodeList list = (NodeList)expr.evaluate(node, XPathConstants.NODESET); List<Element> newList = new ArrayList<Element>(list.getLength()); for (int i = 0; i < list.getLength(); i++) { newList.add((Element)list.item(i)); } return newList; } catch (XPathExpressionException e) { throw new IndexerConfException("Error evaluating XPath expression '" + expression + "'.", e); } }
private List<Element> evalXPathAsElementList(String expression, Node node) { try { XPathExpression expr = XPathFactory.newInstance().newXPath().compile(expression); NodeList list = (NodeList)expr.evaluate(node, XPathConstants.NODESET); List<Element> newList = new ArrayList<Element>(list.getLength()); for (int i = 0; i < list.getLength(); i++) { newList.add((Element)list.item(i)); } return newList; } catch (XPathExpressionException e) { throw new IndexerConfException("Error evaluating XPath expression '" + expression + "'.", e); } }