Search in sources :

Example 1 with XMLException

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);
    }
}
Also used : FacesListener(javax.faces.event.FacesListener) FacesSimpleProperty(com.ibm.xsp.registry.FacesSimpleProperty) ServletException(javax.servlet.ServletException) Date(java.util.Date) ExtensionManager(com.ibm.commons.extension.ExtensionManager) SimpleRegistryProvider(com.ibm.xsp.registry.config.SimpleRegistryProvider) HashMap(java.util.HashMap) XspLibrary(com.ibm.xsp.library.XspLibrary) TemporalAccessor(java.time.temporal.TemporalAccessor) FacesComplexDefinition(com.ibm.xsp.registry.FacesComplexDefinition) HttpServletRequest(javax.servlet.http.HttpServletRequest) FacesComponentDefinition(com.ibm.xsp.registry.FacesComponentDefinition) Document(org.w3c.dom.Document) ActionGroup(com.ibm.xsp.actions.ActionGroup) Map(java.util.Map) Node(org.w3c.dom.Node) LocalTime(java.time.LocalTime) AbstractContainerProperty(com.ibm.xsp.registry.AbstractContainerProperty) MethodBinding(javax.faces.el.MethodBinding) LibraryWrapper(com.ibm.xsp.library.LibraryWrapper) PrintWriter(java.io.PrintWriter) PathUtil(com.ibm.commons.util.PathUtil) DOMUtil(com.ibm.commons.xml.DOMUtil) FacesValidatorDefinition(com.ibm.xsp.registry.FacesValidatorDefinition) SharableRegistryImpl(com.ibm.xsp.registry.SharableRegistryImpl) Properties(java.util.Properties) HttpServlet(javax.servlet.http.HttpServlet) FacesContainerProperty(com.ibm.xsp.registry.FacesContainerProperty) Collection(java.util.Collection) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) FacesDefinition(com.ibm.xsp.registry.FacesDefinition) Collectors(java.util.stream.Collectors) ValueBinding(javax.faces.el.ValueBinding) FacesProperty(com.ibm.xsp.registry.FacesProperty) UIComponent(javax.faces.component.UIComponent) LibraryServiceLoader(com.ibm.xsp.library.LibraryServiceLoader) List(java.util.List) Stream(java.util.stream.Stream) Element(org.w3c.dom.Element) StringUtil(com.ibm.commons.util.StringUtil) XspRegistryProvider(com.ibm.xsp.registry.config.XspRegistryProvider) Format(com.ibm.commons.xml.Format) XMLException(com.ibm.commons.xml.XMLException) InputStream(java.io.InputStream) ComplexContainerTreeNode(com.ibm.xsp.extlib.tree.complex.ComplexContainerTreeNode) XMLException(com.ibm.commons.xml.XMLException) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document)

Example 2 with XMLException

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;
}
Also used : FacesComponentDefinition(com.ibm.xsp.registry.FacesComponentDefinition) XMLException(com.ibm.commons.xml.XMLException) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) FacesComplexDefinition(com.ibm.xsp.registry.FacesComplexDefinition)

Example 3 with XMLException

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;
        }
    });
}
Also used : XMLException(com.ibm.commons.xml.XMLException) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) IOException(java.io.IOException) Document(org.w3c.dom.Document) Properties(java.util.Properties)

Example 4 with XMLException

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);
    }
}
Also used : FacesListener(javax.faces.event.FacesListener) FacesSimpleProperty(com.ibm.xsp.registry.FacesSimpleProperty) ServletException(javax.servlet.ServletException) Date(java.util.Date) ExtensionManager(com.ibm.commons.extension.ExtensionManager) SimpleRegistryProvider(com.ibm.xsp.registry.config.SimpleRegistryProvider) HashMap(java.util.HashMap) XspLibrary(com.ibm.xsp.library.XspLibrary) TemporalAccessor(java.time.temporal.TemporalAccessor) FacesComplexDefinition(com.ibm.xsp.registry.FacesComplexDefinition) HttpServletRequest(javax.servlet.http.HttpServletRequest) FacesComponentDefinition(com.ibm.xsp.registry.FacesComponentDefinition) Document(org.w3c.dom.Document) ActionGroup(com.ibm.xsp.actions.ActionGroup) Map(java.util.Map) Node(org.w3c.dom.Node) LocalTime(java.time.LocalTime) AbstractContainerProperty(com.ibm.xsp.registry.AbstractContainerProperty) MethodBinding(javax.faces.el.MethodBinding) LibraryWrapper(com.ibm.xsp.library.LibraryWrapper) PrintWriter(java.io.PrintWriter) PathUtil(com.ibm.commons.util.PathUtil) DOMUtil(com.ibm.commons.xml.DOMUtil) FacesValidatorDefinition(com.ibm.xsp.registry.FacesValidatorDefinition) SharableRegistryImpl(com.ibm.xsp.registry.SharableRegistryImpl) Properties(java.util.Properties) HttpServlet(javax.servlet.http.HttpServlet) FacesContainerProperty(com.ibm.xsp.registry.FacesContainerProperty) Collection(java.util.Collection) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) FacesDefinition(com.ibm.xsp.registry.FacesDefinition) Collectors(java.util.stream.Collectors) ValueBinding(javax.faces.el.ValueBinding) FacesProperty(com.ibm.xsp.registry.FacesProperty) UIComponent(javax.faces.component.UIComponent) LibraryServiceLoader(com.ibm.xsp.library.LibraryServiceLoader) List(java.util.List) Stream(java.util.stream.Stream) Element(org.w3c.dom.Element) StringUtil(com.ibm.commons.util.StringUtil) XspRegistryProvider(com.ibm.xsp.registry.config.XspRegistryProvider) Format(com.ibm.commons.xml.Format) XMLException(com.ibm.commons.xml.XMLException) InputStream(java.io.InputStream) ComplexContainerTreeNode(com.ibm.xsp.extlib.tree.complex.ComplexContainerTreeNode) XMLException(com.ibm.commons.xml.XMLException) Element(org.w3c.dom.Element)

Example 5 with XMLException

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 += "&lt;" + fcp.getItemProperty().getJavaClass().getName() + "&gt;";
            }
        }
        dt(description, "Class", className);
        // Expected to be opened in every if condition
        description.append("</dl>");
        documentation.setTextContent(description.toString());
    }
}
Also used : XMLException(com.ibm.commons.xml.XMLException) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) ComplexContainerTreeNode(com.ibm.xsp.extlib.tree.complex.ComplexContainerTreeNode) FacesContainerProperty(com.ibm.xsp.registry.FacesContainerProperty) Document(org.w3c.dom.Document)

Aggregations

XMLException (com.ibm.commons.xml.XMLException)7 Document (org.w3c.dom.Document)7 Element (org.w3c.dom.Element)7 ComplexContainerTreeNode (com.ibm.xsp.extlib.tree.complex.ComplexContainerTreeNode)4 FacesComplexDefinition (com.ibm.xsp.registry.FacesComplexDefinition)4 FacesComponentDefinition (com.ibm.xsp.registry.FacesComponentDefinition)4 FacesContainerProperty (com.ibm.xsp.registry.FacesContainerProperty)3 FacesValidatorDefinition (com.ibm.xsp.registry.FacesValidatorDefinition)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 Collection (java.util.Collection)3 Properties (java.util.Properties)3 ValueBinding (javax.faces.el.ValueBinding)3 ExtensionManager (com.ibm.commons.extension.ExtensionManager)2 PathUtil (com.ibm.commons.util.PathUtil)2 StringUtil (com.ibm.commons.util.StringUtil)2 DOMUtil (com.ibm.commons.xml.DOMUtil)2 Format (com.ibm.commons.xml.Format)2 ActionGroup (com.ibm.xsp.actions.ActionGroup)2 LibraryServiceLoader (com.ibm.xsp.library.LibraryServiceLoader)2