private DataDrivenTest(Element e) { super(e.getAttributeValue("desc")); Filter prologFilter = new AbstractFilter() { @Override public Object filter(Object o) { if (o instanceof Element || o instanceof Comment || o instanceof ProcessingInstruction) { return o; } return null; } }; target.addContent(XmlHelper.clone(e.getChild("target").getContent(prologFilter))); diff.setRootElement((Element) e.getChild("diff").clone()); expectedResult.addContent(XmlHelper.clone(e.getChild("result").getContent(prologFilter))); String errorName = e.getChild("result").getAttributeValue("error"); expectedError = errorName == null ? null : ErrorCondition.valueOf(errorName); }
private ImageResource getFirstImageSource(Resource CoverPageResource, Resources resources) { try { Document titlePageDocument = ResourceUtil.getAsDocument(CoverPageResource); Filter<Element> imgFilter = new ElementFilter("img"); List<Element> imageElements = titlePageDocument.getRootElement().getContent(imgFilter); for (Element imageElement : imageElements) { String relativeImageHref = imageElement.getAttributeValue("src"); String absoluteImageHref = calculateAbsoluteImageHref(relativeImageHref, CoverPageResource.getHref()); ImageResource imageResource = (ImageResource)resources.getByHref(absoluteImageHref); if (imageResource != null) { return imageResource; } } } catch (Exception e) { log.error(e.getMessage(), e); } return null; }
/** * Executes an Xpath for an element and executes the consumer * * @param element Element the xpath is executed against * @param expressionStr Xpath * @param consumer Consumer to execute if the xpath matches * @param filter Filter to apply for the xpath * @param selectOne Whether to execute for the first match or multiple matches */ @SuppressWarnings({ "rawtypes", "unchecked" }) void setOnNode(Element element, String expressionStr, Consumer consumer, Filter<?> filter, boolean selectOne) { XPathExpression<?> expression = XPathFactory.instance().compile(expressionStr, filter, null, xpathNs); if (selectOne) { Optional.ofNullable(expression.evaluateFirst(element)).ifPresent(consumer); } else { List<?> elems = expression.evaluate(element); Optional.ofNullable(elems) .ifPresent(notNullElems -> notNullElems.forEach(consumer)); } }
@SuppressWarnings({ "rawtypes", "unchecked" }) public static List<Object> evaluate(String expr, Object parent, Filter filter) throws FatalIndexerException { XPathBuilder<Object> builder = new XPathBuilder<>(expr.trim().replace("\n", ""), filter); // Add all namespaces for (String key : Configuration.getInstance().getNamespaces().keySet()) { Namespace value = Configuration.getInstance().getNamespaces().get(key); builder.setNamespace(key, value.getURI()); } XPathExpression<Object> xpath = builder.compileWith(XPathFactory.instance()); return xpath.evaluate(parent); }
@Override public <T> XPathExpression<T> compile(String expression, Filter<T> filter, Map<String, Object> variables, Namespace... namespaces) { XPathExpression<T> jaxenCompiled = super.compile(expression, filter, variables, namespaces); if (functions.stream().anyMatch(function -> function.isCalledIn(expression))) { addExtensionFunctions(jaxenCompiled, namespaces); } return jaxenCompiled; }
/** * Execute an XPath * * @param document * the Document * @param xpathStr * the Xpath String * @param namespaceStr * the namespace str * @param filter * the filter * @return the list<? extends content> */ static public List<? extends Content> executeXPath(final Object document, final String xpathStr, final String namespaceStr, final Filter<? extends Content> filter) { final XPathFactory xpathFactory = XPathFactory.instance(); // XPathExpression<Object> expr = xpathFactory.compile(xpathStr); XPathExpression<? extends Content> expr = null; if (namespaceStr != null) expr = xpathFactory.compile(xpathStr, filter, null, Namespace.getNamespace("x", namespaceStr)); else expr = xpathFactory.compile(xpathStr, filter); List<? extends Content> xPathSearchedNodes = null; try { xPathSearchedNodes = expr.evaluate(document); } // TODO: Add better handling for these kinds of exceptions catch (final Exception e) { throw new CsfRuntimeException("Error in querying the message", e); } return xPathSearchedNodes; /* * for (int i = 0; i < xPathSearchedNodes.size(); i++) { Content content = * xPathSearchedNodes.get(i); System.out.println("content: " + i + ": " + * content.getValue()); } */ }
private List<Element> getAllElementsByFilter(Document document, Filter<Element> filter) { List<Element> allElems = new ArrayList<>(); Iterator<Element> it = document.getRootElement().getDescendants(filter); while (it.hasNext()) { allElems.add(it.next()); } return allElems; }
void decodeGSEControlBlock(String appID_name) throws IEC61850_GOOSE_Exception { boolean found_GSEControlBlock = false; // Retrieves all elements named "GSEControl" within IED node Filter<Element> elementFilter = new ElementFilter("GSEControl"); // Search for a GSEControl block with a matching appID for (Iterator<Element> GSEControl_IT = IED_section.getDescendants(elementFilter); GSEControl_IT.hasNext();) { Element current_element = GSEControl_IT.next(); if(current_element.getAttributeValue("type").equals("GOOSE") && current_element.getAttributeValue("appID").equals(appID_name)) { // We found the right control block GSEControl_node = current_element; found_GSEControlBlock = true; } } if (found_GSEControlBlock == false) throw new IEC61850_GOOSE_Exception("<GSEControl> Block with corresponding appID_name: " + appID_name + " name not found in <IED>"); gseControlBlockAppIDName = GSEControl_node.getAttributeValue("appID"); gseControlBlockName = GSEControl_node.getAttributeValue("name"); gseControlBlockConfRev = GSEControl_node.getAttributeValue("confRev"); gseControlBlockDatSet = GSEControl_node.getAttributeValue("datSet"); ln0ClassName = GSEControl_node.getParentElement().getAttributeValue("lnClass"); return; }
@SuppressWarnings("unchecked") private <T> T evaluateXpath(String xPath, Filter filter) throws IOException, XmlException { XPathExpression<Attribute> xpath = xpf.compile(xPath, filter); return (T) xpath.evaluateFirst(XmlUtils.parseXmlStream(XmlUtils.fileToStream(path))); }
private Object evaluateXpath(String xPath, Filter filter) throws IOException, XmlException { XPathExpression<Object> xpath = XPF.compile(xPath, filter); return xpath.evaluateFirst(XmlUtils.parseXmlStream(XmlUtils.fileToStream(PATH))); }
void decodeGSEBlock(String GSEControlBlock_name) throws IEC61850_GOOSE_Exception { boolean found_GSEBlock = false; boolean found_APPID = false; boolean found_MACAddress = false; // Retrieves all elements named "GSE" within ConnectedAP in Communication section Filter<Element> elementFilter = new ElementFilter("GSE"); // Search for a GSE with a matching cbName for (Iterator<Element> GSE_IT = ConnectedAP_in_Comm_section.getDescendants(elementFilter); GSE_IT.hasNext();) { Element current_element = GSE_IT.next(); if(current_element.getAttributeValue("cbName").equals(GSEControlBlock_name)) { GSE_node = current_element; found_GSEBlock = true; } } if (found_GSEBlock == false) throw new IEC61850_GOOSE_Exception("<GSE> Block with cbName: " + GSEControlBlock_name + " not found in <ConnectedAP> block"); gseldInst = GSE_node.getAttributeValue("ldInst"); // walks to the "Address" children Element GSE_Address = GSE_node.getChild("Address", root_namespace); List<Element> p_nodes_LIST = GSE_Address.getChildren("P", root_namespace); // Walks all P nodes to retrieve addressing data for ( int position = 0; position < p_nodes_LIST.size(); position++) { if(p_nodes_LIST.get(position).getAttributeValue("type").contentEquals("APPID")) { gseAPPID = Integer.parseInt(p_nodes_LIST.get(position).getValue()); found_APPID = true; } if (p_nodes_LIST.get(position).getAttributeValue("type").contentEquals("MAC-Address")) { gseMACAddress = p_nodes_LIST.get(position).getValue(); found_MACAddress = true; } } if (found_APPID == false) throw new IEC61850_GOOSE_Exception("<APPID> type not found in <GSE> Block with cbName:" + GSEControlBlock_name); if(found_MACAddress == false) throw new IEC61850_GOOSE_Exception("<MAC-Address> type not found in <GSE> Block with cbName:" + GSEControlBlock_name); // Retrieves the maxtime Element GSE_Maxtime = GSE_node.getChild("MaxTime", root_namespace); if (GSE_Maxtime == null) //throw new IEC61850_GOOSE_Exception("<MaxTime> not found in <GSE> Block with cbName:" + GSEControlBlock_name); // If the value is not set in the ICD file, we set it to 0. gseMaxTime = 0; else gseMaxTime = Integer.parseInt(GSE_Maxtime.getValue()); // Retrieves the mintime Element GSE_Mintime = GSE_node.getChild("MinTime", root_namespace); if (GSE_Mintime == null) //throw new IEC61850_GOOSE_Exception("<MinTime> not found in <GSE> Block with cbName:" + GSEControlBlock_name); // If the value is not set in the ICD file, we set it to 0. gseMinTime = 0; else gseMinTime = Integer.parseInt(GSE_Mintime.getValue()); return; }
public WithChildFilter( Filter<Element> childFilter ) { this( null, childFilter ); }
public WithChildFilter( String type, Filter<Element> childFilter ) { this.type = type; this.childFilter = childFilter; }