下面列出了javax.xml.xpath.XPathExpressionException#printStackTrace ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static Set<String> getExcludeSet() throws XPathExpressionException {
final String testExcludeList = System.getProperty(TEST_JS_EXCLUDE_LIST);
String[] testExcludeArray = {};
if (testExcludeList != null) {
testExcludeArray = testExcludeList.split(" ");
}
final Set<String> testExcludeSet = new HashSet<>(testExcludeArray.length);
for (final String test : testExcludeArray) {
testExcludeSet.add(test);
}
final String testExcludesFile = System.getProperty(TEST_JS_EXCLUDES_FILE);
if (testExcludesFile != null && !testExcludesFile.isEmpty()) {
try {
loadExcludesFile(testExcludesFile, testExcludeSet);
} catch (final XPathExpressionException e) {
System.err.println("Error: unable to load test excludes from " + testExcludesFile);
e.printStackTrace();
throw e;
}
}
return testExcludeSet;
}
private static Set<String> getExcludeSet() throws XPathExpressionException {
final String testExcludeList = System.getProperty(TEST_JS_EXCLUDE_LIST);
String[] testExcludeArray = {};
if (testExcludeList != null) {
testExcludeArray = testExcludeList.split(" ");
}
final Set<String> testExcludeSet = new HashSet<>(testExcludeArray.length);
for (final String test : testExcludeArray) {
testExcludeSet.add(test);
}
final String testExcludesFile = System.getProperty(TEST_JS_EXCLUDES_FILE);
if (testExcludesFile != null && !testExcludesFile.isEmpty()) {
try {
loadExcludesFile(testExcludesFile, testExcludeSet);
} catch (final XPathExpressionException e) {
System.err.println("Error: unable to load test excludes from " + testExcludesFile);
e.printStackTrace();
throw e;
}
}
return testExcludeSet;
}
public Map<String, String> getActionsWithReceivers() {
Map<String, String> list = new TreeMap<>();
try {
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
XPathExpression expr =
xpath.compile("/manifest/application/receiver/intent-filter/action");
NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++) {
Node currentNode = nodes.item(i);
Node actionNameAttribute = currentNode.getAttributes().getNamedItem("name");
String receiverName =
currentNode.getParentNode().getParentNode().getAttributes().getNamedItem("name").getTextContent();
list.put(actionNameAttribute.getTextContent(), receiverName);
}
} catch (XPathExpressionException e) {
e.printStackTrace();
}
return list;
}
public List<String> getServices() {
List<String> list = new ArrayList<>();
try {
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
XPathExpression expr =
xpath.compile("/manifest/application/service");
NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++) {
Node serviceNode = nodes.item(i);
Node actionName = serviceNode.getAttributes().getNamedItem("name");
list.add(actionName.getTextContent());
}
} catch (XPathExpressionException e) {
e.printStackTrace();
}
return list;
}
private static Set<String> getExcludeSet() throws XPathExpressionException {
final String testExcludeList = System.getProperty(TEST_JS_EXCLUDE_LIST);
String[] testExcludeArray = {};
if (testExcludeList != null) {
testExcludeArray = testExcludeList.split(" ");
}
final Set<String> testExcludeSet = new HashSet<>(testExcludeArray.length);
for (final String test : testExcludeArray) {
testExcludeSet.add(test);
}
final String testExcludesFile = System.getProperty(TEST_JS_EXCLUDES_FILE);
if (testExcludesFile != null && !testExcludesFile.isEmpty()) {
try {
loadExcludesFile(testExcludesFile, testExcludeSet);
} catch (final XPathExpressionException e) {
System.err.println("Error: unable to load test excludes from " + testExcludesFile);
e.printStackTrace();
throw e;
}
}
return testExcludeSet;
}
private static Set<String> getExcludeSet() throws XPathExpressionException {
final String testExcludeList = System.getProperty(TEST_JS_EXCLUDE_LIST);
String[] testExcludeArray = {};
if (testExcludeList != null) {
testExcludeArray = testExcludeList.split(" ");
}
final Set<String> testExcludeSet = new HashSet<>(testExcludeArray.length);
for (final String test : testExcludeArray) {
testExcludeSet.add(test);
}
final String testExcludesFile = System.getProperty(TEST_JS_EXCLUDES_FILE);
if (testExcludesFile != null && !testExcludesFile.isEmpty()) {
try {
loadExcludesFile(testExcludesFile, testExcludeSet);
} catch (final XPathExpressionException e) {
System.err.println("Error: unable to load test excludes from " + testExcludesFile);
e.printStackTrace();
throw e;
}
}
return testExcludeSet;
}
private static Set<String> getExcludeSet() throws XPathExpressionException {
final String testExcludeList = System.getProperty(TEST_JS_EXCLUDE_LIST);
String[] testExcludeArray = {};
if (testExcludeList != null) {
testExcludeArray = testExcludeList.split(" ");
}
final Set<String> testExcludeSet = new HashSet<>(testExcludeArray.length);
for (final String test : testExcludeArray) {
testExcludeSet.add(test);
}
final String testExcludesFile = System.getProperty(TEST_JS_EXCLUDES_FILE);
if (testExcludesFile != null && !testExcludesFile.isEmpty()) {
try {
loadExcludesFile(testExcludesFile, testExcludeSet);
} catch (final XPathExpressionException e) {
System.err.println("Error: unable to load test excludes from " + testExcludesFile);
e.printStackTrace();
throw e;
}
}
return testExcludeSet;
}
private static Set<String> getExcludeSet() throws XPathExpressionException {
final String testExcludeList = System.getProperty(TEST_JS_EXCLUDE_LIST);
String[] testExcludeArray = {};
if (testExcludeList != null) {
testExcludeArray = testExcludeList.split(" ");
}
final Set<String> testExcludeSet = new HashSet<>(testExcludeArray.length);
for (final String test : testExcludeArray) {
testExcludeSet.add(test);
}
final String testExcludesFile = System.getProperty(TEST_JS_EXCLUDES_FILE);
if (testExcludesFile != null && !testExcludesFile.isEmpty()) {
try {
loadExcludesFile(testExcludesFile, testExcludeSet);
} catch (final XPathExpressionException e) {
System.err.println("Error: unable to load test excludes from " + testExcludesFile);
e.printStackTrace();
throw e;
}
}
return testExcludeSet;
}
private static Set<String> getExcludeSet() throws XPathExpressionException {
final String testExcludeList = System.getProperty(TEST_JS_EXCLUDE_LIST);
String[] testExcludeArray = {};
if (testExcludeList != null) {
testExcludeArray = testExcludeList.split(" ");
}
final Set<String> testExcludeSet = new HashSet<>(testExcludeArray.length);
for (final String test : testExcludeArray) {
testExcludeSet.add(test);
}
final String testExcludesFile = System.getProperty(TEST_JS_EXCLUDES_FILE);
if (testExcludesFile != null && !testExcludesFile.isEmpty()) {
try {
loadExcludesFile(testExcludesFile, testExcludeSet);
} catch (final XPathExpressionException e) {
System.err.println("Error: unable to load test excludes from " + testExcludesFile);
e.printStackTrace();
throw e;
}
}
return testExcludeSet;
}
private static Set<String> getExcludeSet() throws XPathExpressionException {
final String testExcludeList = System.getProperty(TEST_JS_EXCLUDE_LIST);
String[] testExcludeArray = {};
if (testExcludeList != null) {
testExcludeArray = testExcludeList.split(" ");
}
final Set<String> testExcludeSet = new HashSet<>(testExcludeArray.length);
for (final String test : testExcludeArray) {
testExcludeSet.add(test);
}
final String testExcludesFile = System.getProperty(TEST_JS_EXCLUDES_FILE);
if (testExcludesFile != null && !testExcludesFile.isEmpty()) {
try {
loadExcludesFile(testExcludesFile, testExcludeSet);
} catch (final XPathExpressionException e) {
System.err.println("Error: unable to load test excludes from " + testExcludesFile);
e.printStackTrace();
throw e;
}
}
return testExcludeSet;
}
/**
* Removes empty tags, spaces between XML tags
*
* @param document
* document in which the empty tags should be removed
*/
public void removeEmptyTags(Document document) {
NodeList nl = null;
try {
if(Thread.currentThread().getContextClassLoader() == null){
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
}
XPath xPath = XPathFactory.newInstance().newXPath();
nl = (NodeList) xPath.evaluate("//text()[normalize-space(.)='']", document, XPathConstants.NODESET);
for (int i = 0; i < nl.getLength(); ++i) {
Node node = nl.item(i);
node.getParentNode().removeChild(node);
}
} catch (XPathExpressionException e) {
e.printStackTrace();
}
}
/**
* Removes a signature in a given XML document
*
* @param document
* document in which the signature should be removed
* @return number of removed signatures
*/
public int removeOnlyMessageSignature(Document document) {
try {
if(Thread.currentThread().getContextClassLoader() == null){
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
}
setIDAttribute(document);
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xpath.compile("//*[local-name()='Response']/*[local-name()='Signature']");
NodeList nl = (NodeList) expr.evaluate(document, XPathConstants.NODESET);
int nrSig = nl.getLength();
for (int i = 0; i < nrSig; i++) {
Node parent = nl.item(0).getParentNode();
parent.removeChild(nl.item(0));
}
removeEmptyTags(document);
document.normalize();
return nrSig;
} catch (XPathExpressionException e) {
e.printStackTrace();
}
return 0;
}
/**
* Returns SOAP Body as an Element
*
* @param document
* document with SOAP body
* @return Element SOAP Body Element or null if no body found
*/
public Element getSOAPBody(Document document) {
try {
if(Thread.currentThread().getContextClassLoader() == null){
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
}
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xpath.compile("//*[local-name()='Envelope']/*[local-name()='Body']");
NodeList elements = (NodeList) expr.evaluate(document, XPathConstants.NODESET);
if(elements.getLength()>0){
return (Element) elements.item(0);
}
} catch (XPathExpressionException e) {
e.printStackTrace();
}
return null;
}
/**
* Set the ID Attribute in an XML Document so that java recognises the ID
* Attribute as a real id
*
* @param document
* Document to set the ids
*/
public void setIDAttribute(Document document) {
try {
if(Thread.currentThread().getContextClassLoader() == null){
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
}
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xpath.compile("//*[@ID]");
NodeList nodeList = (NodeList) expr.evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
Element elem = (Element) nodeList.item(i);
Attr attr = (Attr) elem.getAttributes().getNamedItem("ID");
elem.setIdAttributeNode(attr, true);
}
} catch (XPathExpressionException e) {
e.printStackTrace();
}
}
public static NodeList selectNodes(String express, Object source) {// 查找节点,返回符合条件的节点集。
NodeList result = null;
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
try {
result = (NodeList) xpath.evaluate(express, source,
XPathConstants.NODESET);
} catch (XPathExpressionException e) {
e.printStackTrace();
}
return result;
}
public void collect(DOMNode node, Consumer<DOMNode> collector) {
DOMDocument document = node.getOwnerDocument();
for (XMLReferences references : referencesCache) {
if (references.canApply(document)) {
try {
references.collectNodes(node, collector);
} catch (XPathExpressionException e) {
// TODO!!!
e.printStackTrace();
}
}
}
}
@Override
public FileReader replacePropertyValuesWith( String propertyName, String replacedValue )
{
XPath xPath = XPathFactory.newInstance().newXPath();
try
{
NodeList nodes = (NodeList) xPath.evaluate( "//*[@" + propertyName + "]", document, XPathConstants.NODESET );
for ( int i = 0; i < nodes.getLength(); i++ )
{
Node node = nodes.item( i ).getAttributes().getNamedItem( propertyName );
if ( replacedValue.equalsIgnoreCase( "uniqueid" ) )
{
node.setNodeValue( new IdGenerator().generateUniqueId() );
continue;
}
node.setNodeValue( replacedValue );
}
}
catch ( XPathExpressionException e )
{
e.printStackTrace();
}
return this;
}
/**
* Sign assertions in SAML message
*
* @param document
* Document in assertions should be signed
* @param signAlgorithm
* Signature algorithm in uri form, default if an unknown
* algorithm is provided:
* http://www.w3.org/2001/04/xmldsig-more#rsa-sha256
* @param digestAlgorithm
* Digest algorithm in uri form, default if an unknown algorithm
* is provided: http://www.w3.org/2001/04/xmlenc#sha256
*/
public void signAssertion(Document document, String signAlgorithm, String digestAlgorithm, X509Certificate cert, PrivateKey key)
throws CertificateException, FileNotFoundException, NoSuchAlgorithmException, InvalidKeySpecException,
MarshalException, XMLSignatureException, IOException {
try {
if(Thread.currentThread().getContextClassLoader() == null){
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
}
setIDAttribute(document);
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xpath.compile("//*[local-name()='Assertion']/@ID");
NodeList nlURIs = (NodeList) expr.evaluate(document, XPathConstants.NODESET);
String[] sigIDs = new String[nlURIs.getLength()];
for (int i = 0; i < nlURIs.getLength(); i++) {
sigIDs[i] = nlURIs.item(i).getNodeValue();
}
Init.init();
for (String id : sigIDs) {
signElement(document, id, cert, key, signAlgorithm, digestAlgorithm);
}
} catch (XPathExpressionException e) {
e.printStackTrace();
}
}
/**
* Sign whole SAML Message
*
* @param document
* Document with the response to sign
* @param signAlgorithm
* Signature algorithm in uri form, default if an unknown
* algorithm is provided:
* http://www.w3.org/2001/04/xmldsig-more#rsa-sha256
* @param digestAlgorithm
* Digest algorithm in uri form, default if an unknown algorithm
* is provided: http://www.w3.org/2001/04/xmlenc#sha256
*/
public void signMessage(Document document, String signAlgorithm, String digestAlgorithm, X509Certificate cert, PrivateKey key)
throws CertificateException, FileNotFoundException, NoSuchAlgorithmException, InvalidKeySpecException,
MarshalException, XMLSignatureException, IOException {
try {
if(Thread.currentThread().getContextClassLoader() == null){
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
}
setIDAttribute(document);
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xpath.compile("//*[local-name()='Response']/@ID");
NodeList nlURIs = (NodeList) expr.evaluate(document, XPathConstants.NODESET);
String[] sigIDs = new String[nlURIs.getLength()];
for (int i = 0; i < nlURIs.getLength(); i++) {
sigIDs[i] = nlURIs.item(i).getNodeValue();
}
Init.init();
for (String id : sigIDs) {
signElement(document, id, cert, key, signAlgorithm, digestAlgorithm);
}
} catch (XPathExpressionException e) {
e.printStackTrace();
}
}
/**
* Gets the string.
*
* @param sldContentString the sld content string
* @param selectionData the selection data
* @param field the field
* @param suffix the suffix
* @return the string
*/
public String getString(
String sldContentString,
TreeSelectionData selectionData,
FieldIdEnum field,
String suffix) {
Document doc = getXMLDocument(sldContentString);
String extractedString = null;
XPath xpath = xPathfactory.newXPath();
XPathExpression expr;
try {
String xPathString = null;
SelectedTreeItemEnum selection = selectionData.getSelection();
String prefix = prefixMap.get(selection);
Map<FieldIdEnum, String> fieldMap = xPathMap.get(selection);
if (fieldMap == null) {
System.err.println("Unknown selected tree item : " + selection);
} else {
String configXPathString = fieldMap.get(field);
if (configXPathString == null) {
System.err.println("Unknown XPath : " + field);
}
StringBuilder sb = new StringBuilder();
sb.append(prefix);
sb.append("/");
sb.append(configXPathString);
if (suffix != null) {
sb.append(suffix);
}
String fieldString = sb.toString();
switch (selection) {
case LAYER:
xPathString = getLayerString(selectionData, fieldString);
break;
case STYLE:
xPathString = getStyleString(selectionData, fieldString);
break;
case POINT_SYMBOLIZER:
case LINE_SYMBOLIZER:
case POLYGON_SYMBOLIZER:
case TEXT_SYMBOLIZER:
case RASTER_SYMBOLIZER:
xPathString = getSymbolizerString(selectionData, fieldString);
break;
case RULE:
xPathString = getRuleString(selectionData, fieldString);
break;
case POINT_FILL:
case POLYGON_FILL:
xPathString = getFillString(selectionData, fieldString);
break;
case STROKE:
xPathString = getStrokeString(selectionData, fieldString);
break;
default:
break;
}
if (xPathString != null) {
expr = xpath.compile(xPathString);
extractedString = expr.evaluate(doc);
if ((extractedString == null) || extractedString.isEmpty()) {
System.out.println("SLD : " + sldContentString);
System.out.println("XPath : " + xPathString);
}
} else {
System.out.println("No XPath string");
}
}
} catch (XPathExpressionException e) {
e.printStackTrace();
}
return extractedString;
}