use of javax.xml.xpath.XPathExpressionException in project lucene-solr by apache.
the class Config method getNodeList.
public NodeList getNodeList(String path, boolean errIfMissing) {
XPath xpath = xpathFactory.newXPath();
String xstr = normalize(path);
try {
NodeList nodeList = (NodeList) xpath.evaluate(xstr, doc, XPathConstants.NODESET);
if (null == nodeList) {
if (errIfMissing) {
throw new RuntimeException(name + " missing " + path);
} else {
log.debug(name + " missing optional " + path);
return null;
}
}
log.trace(name + ":" + path + "=" + nodeList);
return nodeList;
} catch (XPathExpressionException e) {
SolrException.log(log, "Error in xpath", e);
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Error in xpath:" + xstr + " for " + name, e);
} catch (SolrException e) {
throw (e);
} catch (Exception e) {
SolrException.log(log, "Error in xpath", e);
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Error in xpath:" + xstr + " for " + name, e);
}
}
use of javax.xml.xpath.XPathExpressionException in project lucene-solr by apache.
the class EnumField method init.
/**
* {@inheritDoc}
*/
@Override
protected void init(IndexSchema schema, Map<String, String> args) {
super.init(schema, args);
enumsConfigFile = args.get(PARAM_ENUMS_CONFIG);
if (enumsConfigFile == null) {
throw new SolrException(SolrException.ErrorCode.NOT_FOUND, "No enums config file was configured.");
}
enumName = args.get(PARAM_ENUM_NAME);
if (enumName == null) {
throw new SolrException(SolrException.ErrorCode.NOT_FOUND, "No enum name was configured.");
}
InputStream is = null;
try {
is = schema.getResourceLoader().openResource(enumsConfigFile);
final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
final Document doc = dbf.newDocumentBuilder().parse(is);
final XPathFactory xpathFactory = XPathFactory.newInstance();
final XPath xpath = xpathFactory.newXPath();
final String xpathStr = String.format(Locale.ROOT, "/enumsConfig/enum[@name='%s']", enumName);
final NodeList nodes = (NodeList) xpath.evaluate(xpathStr, doc, XPathConstants.NODESET);
final int nodesLength = nodes.getLength();
if (nodesLength == 0) {
String exceptionMessage = String.format(Locale.ENGLISH, "No enum configuration found for enum '%s' in %s.", enumName, enumsConfigFile);
throw new SolrException(SolrException.ErrorCode.NOT_FOUND, exceptionMessage);
}
if (nodesLength > 1) {
if (log.isWarnEnabled())
log.warn("More than one enum configuration found for enum '{}' in {}. The last one was taken.", enumName, enumsConfigFile);
}
final Node enumNode = nodes.item(nodesLength - 1);
final NodeList valueNodes = (NodeList) xpath.evaluate("value", enumNode, XPathConstants.NODESET);
for (int i = 0; i < valueNodes.getLength(); i++) {
final Node valueNode = valueNodes.item(i);
final String valueStr = valueNode.getTextContent();
if ((valueStr == null) || (valueStr.length() == 0)) {
final String exceptionMessage = String.format(Locale.ENGLISH, "A value was defined with an no value in enum '%s' in %s.", enumName, enumsConfigFile);
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, exceptionMessage);
}
if (enumStringToIntMap.containsKey(valueStr)) {
final String exceptionMessage = String.format(Locale.ENGLISH, "A duplicated definition was found for value '%s' in enum '%s' in %s.", valueStr, enumName, enumsConfigFile);
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, exceptionMessage);
}
enumIntToStringMap.put(i, valueStr);
enumStringToIntMap.put(valueStr, i);
}
} catch (ParserConfigurationException | XPathExpressionException | SAXException e) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Error parsing enums config.", e);
}
} catch (IOException e) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Error while opening enums config.", e);
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
if ((enumStringToIntMap.size() == 0) || (enumIntToStringMap.size() == 0)) {
String exceptionMessage = String.format(Locale.ENGLISH, "Invalid configuration was defined for enum '%s' in %s.", enumName, enumsConfigFile);
throw new SolrException(SolrException.ErrorCode.NOT_FOUND, exceptionMessage);
}
args.remove(PARAM_ENUMS_CONFIG);
args.remove(PARAM_ENUM_NAME);
}
use of javax.xml.xpath.XPathExpressionException in project lucene-solr by apache.
the class FieldTypePluginLoader method readAnalyzer.
//
// <analyzer><tokenizer class="...."/><tokenizer class="...." arg="....">
//
//
private Analyzer readAnalyzer(Node node) throws XPathExpressionException {
final SolrResourceLoader loader = schema.getResourceLoader();
if (node == null)
return null;
NamedNodeMap attrs = node.getAttributes();
String analyzerName = DOMUtil.getAttr(attrs, "class");
// check for all of these up front, so we can error if used in
// conjunction with an explicit analyzer class.
NodeList charFilterNodes = (NodeList) xpath.evaluate("./charFilter", node, XPathConstants.NODESET);
NodeList tokenizerNodes = (NodeList) xpath.evaluate("./tokenizer", node, XPathConstants.NODESET);
NodeList tokenFilterNodes = (NodeList) xpath.evaluate("./filter", node, XPathConstants.NODESET);
if (analyzerName != null) {
// own custom nodes (ie: <description> or something like that)
if (0 != charFilterNodes.getLength() || 0 != tokenizerNodes.getLength() || 0 != tokenFilterNodes.getLength()) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Configuration Error: Analyzer class='" + analyzerName + "' can not be combined with nested analysis factories");
}
try {
// No need to be core-aware as Analyzers are not in the core-aware list
final Class<? extends Analyzer> clazz = loader.findClass(analyzerName, Analyzer.class);
Analyzer analyzer = clazz.newInstance();
final String matchVersionStr = DOMUtil.getAttr(attrs, LUCENE_MATCH_VERSION_PARAM);
final Version luceneMatchVersion = (matchVersionStr == null) ? schema.getDefaultLuceneMatchVersion() : Config.parseLuceneVersionString(matchVersionStr);
if (luceneMatchVersion == null) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Configuration Error: Analyzer '" + clazz.getName() + "' needs a 'luceneMatchVersion' parameter");
}
analyzer.setVersion(luceneMatchVersion);
return analyzer;
} catch (Exception e) {
log.error("Cannot load analyzer: " + analyzerName, e);
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Cannot load analyzer: " + analyzerName, e);
}
}
// Load the CharFilters
final ArrayList<CharFilterFactory> charFilters = new ArrayList<>();
AbstractPluginLoader<CharFilterFactory> charFilterLoader = new AbstractPluginLoader<CharFilterFactory>("[schema.xml] analyzer/charFilter", CharFilterFactory.class, false, false) {
@Override
protected CharFilterFactory create(SolrResourceLoader loader, String name, String className, Node node) throws Exception {
final Map<String, String> params = DOMUtil.toMap(node.getAttributes());
String configuredVersion = params.remove(LUCENE_MATCH_VERSION_PARAM);
params.put(LUCENE_MATCH_VERSION_PARAM, parseConfiguredVersion(configuredVersion, CharFilterFactory.class.getSimpleName()).toString());
CharFilterFactory factory = loader.newInstance(className, CharFilterFactory.class, getDefaultPackages(), new Class[] { Map.class }, new Object[] { params });
factory.setExplicitLuceneMatchVersion(null != configuredVersion);
return factory;
}
@Override
protected void init(CharFilterFactory plugin, Node node) throws Exception {
if (plugin != null) {
charFilters.add(plugin);
}
}
@Override
protected CharFilterFactory register(String name, CharFilterFactory plugin) {
// used for map registration
return null;
}
};
charFilterLoader.load(loader, charFilterNodes);
// Load the Tokenizer
// Although an analyzer only allows a single Tokenizer, we load a list to make sure
// the configuration is ok
final ArrayList<TokenizerFactory> tokenizers = new ArrayList<>(1);
AbstractPluginLoader<TokenizerFactory> tokenizerLoader = new AbstractPluginLoader<TokenizerFactory>("[schema.xml] analyzer/tokenizer", TokenizerFactory.class, false, false) {
@Override
protected TokenizerFactory create(SolrResourceLoader loader, String name, String className, Node node) throws Exception {
final Map<String, String> params = DOMUtil.toMap(node.getAttributes());
String configuredVersion = params.remove(LUCENE_MATCH_VERSION_PARAM);
params.put(LUCENE_MATCH_VERSION_PARAM, parseConfiguredVersion(configuredVersion, TokenizerFactory.class.getSimpleName()).toString());
TokenizerFactory factory = loader.newInstance(className, TokenizerFactory.class, getDefaultPackages(), new Class[] { Map.class }, new Object[] { params });
factory.setExplicitLuceneMatchVersion(null != configuredVersion);
return factory;
}
@Override
protected void init(TokenizerFactory plugin, Node node) throws Exception {
if (!tokenizers.isEmpty()) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "The schema defines multiple tokenizers for: " + node);
}
tokenizers.add(plugin);
}
@Override
protected TokenizerFactory register(String name, TokenizerFactory plugin) {
// used for map registration
return null;
}
};
tokenizerLoader.load(loader, tokenizerNodes);
// Make sure something was loaded
if (tokenizers.isEmpty()) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "analyzer without class or tokenizer");
}
// Load the Filters
final ArrayList<TokenFilterFactory> filters = new ArrayList<>();
AbstractPluginLoader<TokenFilterFactory> filterLoader = new AbstractPluginLoader<TokenFilterFactory>("[schema.xml] analyzer/filter", TokenFilterFactory.class, false, false) {
@Override
protected TokenFilterFactory create(SolrResourceLoader loader, String name, String className, Node node) throws Exception {
final Map<String, String> params = DOMUtil.toMap(node.getAttributes());
String configuredVersion = params.remove(LUCENE_MATCH_VERSION_PARAM);
params.put(LUCENE_MATCH_VERSION_PARAM, parseConfiguredVersion(configuredVersion, TokenFilterFactory.class.getSimpleName()).toString());
TokenFilterFactory factory = loader.newInstance(className, TokenFilterFactory.class, getDefaultPackages(), new Class[] { Map.class }, new Object[] { params });
factory.setExplicitLuceneMatchVersion(null != configuredVersion);
return factory;
}
@Override
protected void init(TokenFilterFactory plugin, Node node) throws Exception {
if (plugin != null) {
filters.add(plugin);
}
}
@Override
protected TokenFilterFactory register(String name, TokenFilterFactory plugin) throws Exception {
// used for map registration
return null;
}
};
filterLoader.load(loader, tokenFilterNodes);
return new TokenizerChain(charFilters.toArray(new CharFilterFactory[charFilters.size()]), tokenizers.get(0), filters.toArray(new TokenFilterFactory[filters.size()]));
}
use of javax.xml.xpath.XPathExpressionException in project cloudstack by apache.
the class OvmObject method xmlToList.
public List<String> xmlToList(String path, Document xmlDocument) throws Ovm3ResourceException {
List<String> list = new ArrayList<String>();
XPathFactory factory = javax.xml.xpath.XPathFactory.newInstance();
XPath xPath = factory.newXPath();
try {
XPathExpression xPathExpression = xPath.compile(path);
NodeList nodeList = (NodeList) xPathExpression.evaluate(xmlDocument, XPathConstants.NODESET);
for (int ind = 0; ind < nodeList.getLength(); ind++) {
if (!nodeList.item(ind).getTextContent().isEmpty()) {
list.add("" + nodeList.item(ind).getTextContent());
} else {
list.add("" + nodeList.item(ind).getNodeValue());
}
}
return list;
} catch (XPathExpressionException e) {
throw new Ovm3ResourceException("Problem parsing XML to List: ", e);
}
}
use of javax.xml.xpath.XPathExpressionException in project cloudstack by apache.
the class PaloAltoResource method requestWithCommit.
/* Runs a sequence of commands and attempts to commit at the end. */
/* Uses the Command pattern to enable overriding of the response handling if needed. */
private synchronized boolean requestWithCommit(ArrayList<IPaloAltoCommand> commandList) throws ExecutionException {
boolean result = true;
if (commandList.size() > 0) {
// CHECK IF THERE IS PENDING CHANGES THAT HAVE NOT BEEN COMMITTED...
String pending_changes;
Map<String, String> check_params = new HashMap<String, String>();
check_params.put("type", "op");
check_params.put("cmd", "<check><pending-changes></pending-changes></check>");
String check_response = request(PaloAltoMethod.GET, check_params);
Document check_doc = getDocument(check_response);
XPath check_xpath = XPathFactory.newInstance().newXPath();
try {
XPathExpression expr = check_xpath.compile("/response[@status='success']/result/text()");
pending_changes = (String) expr.evaluate(check_doc, XPathConstants.STRING);
} catch (XPathExpressionException e) {
throw new ExecutionException(e.getCause().getMessage());
}
if (pending_changes.equals("yes")) {
throw new ExecutionException("The Palo Alto has uncommited changes, so no changes can be made. Try again later or contact your administrator.");
} else {
// ADD A CONFIG LOCK TO CAPTURE THE PALO ALTO RESOURCE
String add_lock_status;
Map<String, String> add_lock_params = new HashMap<String, String>();
add_lock_params.put("type", "op");
add_lock_params.put("cmd", "<request><config-lock><add></add></config-lock></request>");
String add_lock_response = request(PaloAltoMethod.GET, add_lock_params);
Document add_lock_doc = getDocument(add_lock_response);
XPath add_lock_xpath = XPathFactory.newInstance().newXPath();
try {
XPathExpression expr = add_lock_xpath.compile("/response[@status='success']/result/text()");
add_lock_status = (String) expr.evaluate(add_lock_doc, XPathConstants.STRING);
} catch (XPathExpressionException e) {
throw new ExecutionException(e.getCause().getMessage());
}
if (add_lock_status.length() == 0) {
throw new ExecutionException("The Palo Alto is locked, no changes can be made at this time.");
}
try {
// RUN THE SEQUENCE OF COMMANDS
for (IPaloAltoCommand command : commandList) {
// run commands and modify result boolean
result = (result && command.execute());
}
// COMMIT THE CHANGES (ALSO REMOVES CONFIG LOCK)
String commit_job_id;
Map<String, String> commit_params = new HashMap<String, String>();
commit_params.put("type", "commit");
commit_params.put("cmd", "<commit></commit>");
String commit_response = requestWithPolling(PaloAltoMethod.GET, commit_params);
Document commit_doc = getDocument(commit_response);
XPath commit_xpath = XPathFactory.newInstance().newXPath();
try {
XPathExpression expr = commit_xpath.compile("/response[@status='success']/result/job/id/text()");
commit_job_id = (String) expr.evaluate(commit_doc, XPathConstants.STRING);
} catch (XPathExpressionException e) {
throw new ExecutionException(e.getCause().getMessage());
}
if (commit_job_id.length() == 0) {
// no commit was done, so release the lock...
// REMOVE THE CONFIG LOCK TO RELEASE THE PALO ALTO RESOURCE
String remove_lock_status;
Map<String, String> remove_lock_params = new HashMap<String, String>();
remove_lock_params.put("type", "op");
remove_lock_params.put("cmd", "<request><config-lock><remove></remove></config-lock></request>");
String remove_lock_response = request(PaloAltoMethod.GET, remove_lock_params);
Document remove_lock_doc = getDocument(remove_lock_response);
XPath remove_lock_xpath = XPathFactory.newInstance().newXPath();
try {
XPathExpression expr = remove_lock_xpath.compile("/response[@status='success']/result/text()");
remove_lock_status = (String) expr.evaluate(remove_lock_doc, XPathConstants.STRING);
} catch (XPathExpressionException e) {
throw new ExecutionException(e.getCause().getMessage());
}
if (remove_lock_status.length() == 0) {
throw new ExecutionException("Could not release the Palo Alto device. Please notify an administrator!");
}
}
} catch (ExecutionException ex) {
// REVERT TO RUNNING
String revert_job_id;
Map<String, String> revert_params = new HashMap<String, String>();
revert_params.put("type", "op");
revert_params.put("cmd", "<load><config><from>running-config.xml</from></config></load>");
requestWithPolling(PaloAltoMethod.GET, revert_params);
// REMOVE THE CONFIG LOCK TO RELEASE THE PALO ALTO RESOURCE
String remove_lock_status;
Map<String, String> remove_lock_params = new HashMap<String, String>();
remove_lock_params.put("type", "op");
remove_lock_params.put("cmd", "<request><config-lock><remove></remove></config-lock></request>");
String remove_lock_response = request(PaloAltoMethod.GET, remove_lock_params);
Document remove_lock_doc = getDocument(remove_lock_response);
XPath remove_lock_xpath = XPathFactory.newInstance().newXPath();
try {
XPathExpression expr = remove_lock_xpath.compile("/response[@status='success']/result/text()");
remove_lock_status = (String) expr.evaluate(remove_lock_doc, XPathConstants.STRING);
} catch (XPathExpressionException e) {
throw new ExecutionException(e.getCause().getMessage());
}
if (remove_lock_status.length() == 0) {
throw new ExecutionException("Could not release the Palo Alto device. Please notify an administrator!");
}
// Bubble up the reason we reverted...
throw ex;
}
return result;
}
} else {
// nothing to do
return true;
}
}
Aggregations