/** * Add an element at the end of the path */ protected void pushPath(String thePathElement, boolean theResource) { assert isNotBlank(thePathElement); assert !thePathElement.contains("."); assert theResource ^ Character.isLowerCase(thePathElement.charAt(0)); EncodeContextPathElement element = new EncodeContextPathElement(thePathElement, theResource); getPath().add(element); if (theResource) { myResourcePath.add(element); } }
@Override public void setEncodeElements(Set<String> theEncodeElements) { if (theEncodeElements == null || theEncodeElements.isEmpty()) { myEncodeElements = null; myEncodeElementsAppliesToResourceTypes = null; } else { myEncodeElements = theEncodeElements .stream() .map(ElementsPath::new) .collect(Collectors.toList()); myEncodeElementsAppliesToResourceTypes = new HashSet<>(); for (String next : myEncodeElements.stream().map(t -> t.getPath().get(0).getName()).collect(Collectors.toList())) { if (next.startsWith("*")) { myEncodeElementsAppliesToResourceTypes = null; break; } int dotIdx = next.indexOf('.'); if (dotIdx == -1) { myEncodeElementsAppliesToResourceTypes.add(next); } else { myEncodeElementsAppliesToResourceTypes.add(next.substring(0, dotIdx)); } } } }
/** * Remove the element at the end of the path */ public void popPath() { EncodeContextPathElement removed = getPath().remove(getPath().size() - 1); if (removed.isResource()) { myResourcePath.remove(myResourcePath.size() - 1); } }
if (next.getPath().get(next.getPath().size() - 1).getName().equals("(mandatory)")) { if (myDef.getMin() > 0) { retVal = true;
private boolean checkIfParentShouldBeEncodedAndBuildPath() { List<ElementsPath> encodeElements = myEncodeElements; String currentResourceName = myEncodeContext.getResourcePath().get(myEncodeContext.getResourcePath().size() - 1).getName(); if (myEncodeElementsAppliesToResourceTypes != null && !myEncodeElementsAppliesToResourceTypes.contains(currentResourceName)) { encodeElements = null; } boolean retVal = checkIfPathMatchesForEncoding(encodeElements, true); /* * We force the meta tag to be encoded even if it's not specified as an element in the * elements filter, specifically because we'll need it in order to automatically add * the SUBSETTED tag */ if (!retVal) { if ("meta".equals(myEncodeContext.getLeafResourcePathFirstField()) && shouldAddSubsettedTag(myEncodeContext)) { // The next element is a child of the <meta> element retVal = true; } else if ("meta".equals(myDef.getElementName()) && shouldAddSubsettedTag(myEncodeContext)) { // The next element is the <meta> element retVal = true; } } return retVal; }
protected boolean shouldAddSubsettedTag(EncodeContext theEncodeContext) { if (isSummaryMode()) { return true; } if (isSuppressNarratives()) { return true; } if (myEncodeElements != null) { if (isEncodeElementsAppliesToChildResourcesOnly() && theEncodeContext.getResourcePath().size() < 2) { return false; } String currentResourceName = theEncodeContext.getResourcePath().get(theEncodeContext.getResourcePath().size() - 1).getName(); if (myEncodeElementsAppliesToResourceTypes == null || myEncodeElementsAppliesToResourceTypes.contains(currentResourceName)) { return true; } } return false; }
protected ElementsPath(String thePath) { StringTokenizer tok = new StringTokenizer(thePath, "."); boolean first = true; while (tok.hasMoreTokens()) { String next = tok.nextToken(); if (first && next.equals("*")) { getPath().add(new EncodeContextPathElement("*", true)); } else if (isNotBlank(next)) { getPath().add(new EncodeContextPathElement(next, Character.isUpperCase(next.charAt(0)))); } first = false; } }
public String getLeafResourcePathFirstField() { String retVal = null; for (int i = getPath().size() - 1; i >= 0; i--) { if (getPath().get(i).isResource()) { break; } else { retVal = getPath().get(i).getName(); } } return retVal; }
public EncodeContextPath getCurrentResourcePath() { EncodeContextPath retVal = null; for (int i = myPath.size() - 1; i >= 0; i--) { if (myPath.get(i).isResource()) { retVal = new EncodeContextPath(myPath.subList(i, myPath.size())); break; } } Validate.isTrue(retVal != null); return retVal; } }
public boolean matches(EncodeContextPathElement theOther) { if (myResource != theOther.isResource()) { return false; } if (myName.equals(theOther.getName())) { return true; } if (myName.equals("*")) { return true; } return false; }
public boolean startsWith(EncodeContextPath theCurrentResourcePath) { for (int i = 0; i < getPath().size(); i++) { if (theCurrentResourcePath.getPath().size() == i) { return true; } EncodeContextPathElement expected = getPath().get(i); EncodeContextPathElement actual = theCurrentResourcePath.getPath().get(i); if (!expected.matches(actual)) { return false; } } return true; }