use of com.ibm.commons.xml.XMLException in project org.openntf.nsfodp by OpenNTF.
the class AbstractSchemaServlet method findDefinitionElement.
/**
* Finds the XML element for the given property in its original .xsp-config file.
*
* @param def the containing {@link FacesDefinition} for the property
* @param prop the {@link FacesProperty} to search for
* @return an {@link Element} describing the definition, or {@code null} if it cannot be found
*/
private Element findDefinitionElement(FacesDefinition def, FacesProperty prop) {
try {
Element parent = findDefinitionElement(def);
if (parent != null) {
// Check for the property directly
Element propElement = Stream.of(DOMUtil.nodes(parent, "property/property-name[normalize-space(text())='" + prop.getName() + "']")).filter(Element.class::isInstance).map(Element.class::cast).map(el -> el.getParentNode()).map(Element.class::cast).findFirst().orElse(null);
if (propElement != null) {
// Great!
return propElement;
}
// Check its parent
if (def.getParent() != null) {
propElement = findDefinitionElement(def.getParent(), prop);
if (propElement != null) {
return propElement;
}
}
Collection<String> groups = def.getGroupTypeRefs();
if (groups != null && !groups.isEmpty()) {
String propFile = prop.getFile().getFilePath();
Document propDoc = getXspConfig(propFile);
if (propDoc != null) {
return findInGroups(prop, propDoc, groups);
}
}
}
return null;
} catch (XMLException e) {
throw new RuntimeException(e);
}
}
use of com.ibm.commons.xml.XMLException in project org.openntf.nsfodp by OpenNTF.
the class AbstractSchemaServlet method findDefinitionElement.
/**
* Finds the XML element for the given definition in its original .xsp-config file.
*
* @param def the {@link FacesDefinition} to search for
* @return an {@link Element} describing the definition, or {@code null} if it cannot be found
*/
private Element findDefinitionElement(FacesDefinition def) {
String filePath = def.getFile().getFilePath();
Document xspConfig = getXspConfig(filePath);
if (xspConfig != null) {
try {
String elementName;
if (def instanceof FacesComplexDefinition) {
elementName = "complex-class";
} else if (def instanceof FacesComponentDefinition) {
elementName = "component-class";
} else {
return null;
}
Object[] elements = DOMUtil.nodes(xspConfig, "/faces-config/*/" + elementName + "[normalize-space(text())='" + def.getJavaClass().getName() + "']");
return elements.length == 0 ? null : (Element) ((Element) elements[0]).getParentNode();
} catch (XMLException e) {
throw new RuntimeException(e);
}
}
return null;
}
use of com.ibm.commons.xml.XMLException in project org.openntf.nsfodp by OpenNTF.
the class AbstractSchemaServlet method getXspConfig.
private Document getXspConfig(String filePath) {
return xspConfigs.computeIfAbsent(PathUtil.concat("/", filePath, '/'), key -> {
try (InputStream is = getClass().getResourceAsStream(key)) {
if (is != null) {
Document result = DOMUtil.createDocument(is);
// Find a translation file
if (key.endsWith(".xsp-config")) {
String propFilePath = key.substring(0, key.length() - ".xsp-config".length()) + ".properties";
try (InputStream propIs = getClass().getResourceAsStream(propFilePath)) {
if (propIs != null) {
Properties props = new Properties();
props.load(propIs);
Stream.of(DOMUtil.nodes(result, "//*[starts-with(text(),'%')]")).filter(Element.class::isInstance).map(Element.class::cast).forEach(el -> {
String text = el.getTextContent();
if (text.endsWith("%")) {
String prop = text.substring(1, text.length() - 1);
String translated = props.getProperty(prop, text);
el.setTextContent(translated);
}
});
}
}
}
return result;
} else {
return null;
}
} catch (IOException | XMLException e) {
e.printStackTrace();
// Ignore
return null;
}
});
}
use of com.ibm.commons.xml.XMLException in project org.openntf.nsfodp by OpenNTF.
the class AbstractSchemaServlet method findInGroups.
private Element findInGroups(FacesProperty prop, Document propDoc, Collection<String> groups) throws XMLException {
if (groups == null || groups.isEmpty()) {
return null;
}
Element groupElement = groups.stream().map(group -> {
try {
return DOMUtil.nodes(propDoc, "/faces-config/group/group-type[normalize-space(text())='" + group + "']");
} catch (XMLException e) {
throw new RuntimeException(e);
}
}).filter(nodes -> nodes.length > 0).map(nodes -> nodes[0]).map(Element.class::cast).map(Element::getParentNode).map(Element.class::cast).findFirst().orElse(null);
if (groupElement == null) {
// No dice
return null;
}
// Check the group for a property element
Object[] nodes = DOMUtil.nodes(groupElement, "property/property-name[normalize-space(text())='" + prop.getName() + "']");
if (nodes.length > 0) {
// Then we found it
return (Element) ((Element) nodes[0]).getParentNode();
} else {
// Otherwise, search the group's group refs
List<String> groupRefs = Stream.of(DOMUtil.nodes(groupElement, "group-type-ref")).map(Element.class::cast).map(el -> el.getTextContent()).collect(Collectors.toList());
return findInGroups(prop, propDoc, groupRefs);
}
}
use of com.ibm.commons.xml.XMLException in project org.openntf.nsfodp by OpenNTF.
the class AbstractSchemaServlet method outAnnotations.
private void outAnnotations(FacesDefinition def, FacesProperty prop, Element element) {
Document doc = element.getOwnerDocument();
Element annotation = doc.createElement("xs:annotation");
if (element.getFirstChild() != null) {
element.insertBefore(annotation, element.getFirstChild());
} else {
element.appendChild(annotation);
}
Element propElement = findDefinitionElement(def, prop);
{
Element appinfo = DOMUtil.createElement(doc, annotation, "xs:appinfo");
if (propElement != null) {
appinfo.setTextContent(propElement.getElementsByTagName("display-name").item(0).getTextContent());
} else {
appinfo.setTextContent(prop.getName());
}
}
{
Element documentation = DOMUtil.createElement(doc, annotation, "xs:documentation");
documentation.setAttribute("source", "version");
String since = prop.getSince();
if (StringUtil.isEmpty(since)) {
since = def.getSince();
if (StringUtil.isEmpty(since)) {
since = "8.5.0";
}
}
documentation.setTextContent(since + "+");
}
{
// This appears to be treated as HTML
Element documentation = DOMUtil.createElement(doc, annotation, "xs:documentation");
documentation.setAttribute("source", "description");
StringBuilder description = new StringBuilder();
if (propElement != null) {
p(description, propElement.getElementsByTagName("description").item(0).getTextContent());
description.append("<dl>");
try {
Object[] catNodes = DOMUtil.nodes(propElement, "property-extension/designer-extension/category/text()");
if (catNodes.length > 0) {
dt(description, "Property Group", ((Node) catNodes[0]).getTextContent());
}
} catch (XMLException e) {
throw new RuntimeException(e);
}
} else {
description.append("<dl>");
}
String className = prop.getJavaClass().getName();
if (prop instanceof FacesContainerProperty) {
FacesContainerProperty fcp = (FacesContainerProperty) prop;
if (fcp.isCollection()) {
className += "<" + fcp.getItemProperty().getJavaClass().getName() + ">";
}
}
dt(description, "Class", className);
// Expected to be opened in every if condition
description.append("</dl>");
documentation.setTextContent(description.toString());
}
}
Aggregations