Search in sources :

Example 1 with NodeType

use of org.loboevolution.type.NodeType in project LoboEvolution by LoboEvolution.

the class XPathExpressionImpl method evaluate.

/**
 * {@inheritDoc}
 */
@Override
public Object evaluate(Node contextNode, short type, Object result) throws XPathException, DOMException {
    // If the XPathEvaluator was determined by "casting" the document
    if (m_doc != null) {
        // Check that the context node is owned by the same document
        if (!Objects.equals(contextNode, m_doc) && !contextNode.getOwnerDocument().equals(m_doc)) {
            String fmsg = XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_WRONG_DOCUMENT, null);
            throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, fmsg);
        }
        // Check that the context node is an acceptable node type
        NodeType nodeType = contextNode.getNodeType();
        switch(nodeType) {
            case DOCUMENT_NODE:
            case ELEMENT_NODE:
            case ATTRIBUTE_NODE:
            case TEXT_NODE:
            case CDATA_SECTION_NODE:
            case COMMENT_NODE:
            case PROCESSING_INSTRUCTION_NODE:
                break;
            default:
                String fmsg = XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_WRONG_NODETYPE, null);
                throw new UnsupportedOperationException(fmsg);
        }
    }
    if (!XPathResultImpl.isValidType(type)) {
        String fmsg = XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_INVALID_XPATH_TYPE, new Object[] { (int) type });
        throw new XPathException(XPathException.TYPE_ERR, fmsg);
    }
    XPathContext xpathSupport = new XPathContext(false);
    XObject xobj = null;
    return new XPathResultImpl(type, xobj, contextNode, m_xpath);
}
Also used : DOMException(com.gargoylesoftware.css.dom.DOMException) XPathException(org.loboevolution.html.xpath.XPathException) NodeType(org.loboevolution.type.NodeType) XPathContext(org.apache.xpath.XPathContext) XObject(org.apache.xpath.objects.XObject)

Example 2 with NodeType

use of org.loboevolution.type.NodeType in project LoboEvolution by LoboEvolution.

the class RBlockViewport method layoutChildren.

private void layoutChildren(NodeImpl node) {
    final NodeListImpl nodeList = node.getNodeList();
    if (nodeList != null) {
        nodeList.forEach(nd -> {
            final NodeImpl child = (NodeImpl) nd;
            final NodeType nodeType = child.getNodeType();
            switch(nodeType) {
                case TEXT_NODE:
                    layoutText(child);
                    break;
                case ELEMENT_NODE:
                    this.currentLine.addStyleChanger(new RStyleChanger(child));
                    final String nodeName = child.getNodeName().toUpperCase();
                    MarkupLayout ml = RLayout.elementLayout.get(HTMLTag.get(nodeName));
                    if (ml == null) {
                        ml = miscLayout;
                    }
                    ml.layoutMarkup(this, (HTMLElementImpl) child);
                    this.currentLine.addStyleChanger(new RStyleChanger(node));
                    break;
                case DOCUMENT_FRAGMENT_NODE:
                    final DocumentFragmentImpl fragment = (DocumentFragmentImpl) child;
                    fragment.getNodeList().forEach(fragNode -> {
                        final NodeImpl fragChild = (NodeImpl) fragNode;
                        layoutChildren(fragChild);
                    });
                    break;
                case COMMENT_NODE:
                case PROCESSING_INSTRUCTION_NODE:
                default:
                    break;
            }
        });
    }
}
Also used : DocumentFragmentImpl(org.loboevolution.html.dom.domimpl.DocumentFragmentImpl) NodeListImpl(org.loboevolution.html.dom.nodeimpl.NodeListImpl) NodeImpl(org.loboevolution.html.dom.nodeimpl.NodeImpl) NodeType(org.loboevolution.type.NodeType)

Example 3 with NodeType

use of org.loboevolution.type.NodeType in project LoboEvolution by LoboEvolution.

the class NodeImpl method getTextContent.

/**
 * {@inheritDoc}
 *
 * Gets the text content of this node and its descendents.
 */
@Override
public String getTextContent() {
    final StringBuilder sb = new StringBuilder();
    nodeList.forEach(child -> {
        final NodeType type = child.getNodeType();
        switch(type) {
            case CDATA_SECTION_NODE:
            case TEXT_NODE:
            case ELEMENT_NODE:
                final String textContent = child.getTextContent();
                if (textContent != null) {
                    sb.append(textContent);
                }
                break;
            default:
                break;
        }
    });
    return sb.toString();
}
Also used : NodeType(org.loboevolution.type.NodeType)

Example 4 with NodeType

use of org.loboevolution.type.NodeType in project LoboEvolution by LoboEvolution.

the class XPathNSResolverImpl method lookupNamespaceURI.

/**
 * {@inheritDoc}
 */
@Override
public String lookupNamespaceURI(String prefix) {
    String namespace = null;
    if (prefix.equals("xml")) {
        namespace = Constants.S_XMLNAMESPACEURI;
    } else {
        NodeType type;
        while ((null != parent) && (null == namespace) && (((type = parent.getNodeType()) == NodeType.ELEMENT_NODE) || ((type = parent.getNodeType()) == NodeType.DOCUMENT_NODE) || (type == NodeType.ENTITY_REFERENCE_NODE))) {
            if (type == NodeType.DOCUMENT_NODE) {
                Document document = (Document) parent;
                Element docelm = document.getDocumentElement();
                if (docelm != null && docelm.getNodeName().indexOf(prefix.toUpperCase() + ":") == 0) {
                    return docelm.getNamespaceURI();
                }
            }
            if (type == NodeType.ELEMENT_NODE) {
                if (parent.getNodeName().indexOf(prefix.toUpperCase() + ":") == 0) {
                    return parent.getNamespaceURI();
                }
                NamedNodeMap nnm = ((Element) parent).getAttributes();
                for (int i = 0; i < nnm.getLength(); i++) {
                    Node attr = nnm.item(i);
                    String aname = attr.getNodeName();
                    boolean isPrefix = aname.startsWith("xmlns:");
                    if (isPrefix || aname.equals("xmlns")) {
                        int index = aname.indexOf(':');
                        String p = isPrefix ? aname.substring(index + 1) : "";
                        if (p.equals(prefix)) {
                            namespace = attr.getNodeValue();
                            break;
                        }
                    }
                }
            }
            parent = parent.getParentNode();
        }
    }
    return namespace;
}
Also used : NamedNodeMap(org.loboevolution.html.node.NamedNodeMap) NodeType(org.loboevolution.type.NodeType) Element(org.loboevolution.html.node.Element) Node(org.loboevolution.html.node.Node) Document(org.loboevolution.html.node.Document)

Example 5 with NodeType

use of org.loboevolution.type.NodeType in project LoboEvolution by LoboEvolution.

the class XMLSerializerImpl method getXMLString.

public static void getXMLString(Element node, boolean withoutNamespaces, StringBuffer buff, boolean endTag) {
    try {
        buff.append("<").append(namespace(node.getNodeName(), withoutNamespaces));
        if (node.hasAttributes()) {
            buff.append(" ");
            NamedNodeMap attributes = node.getAttributes();
            for (Attr attrItem : Nodes.iterable(attributes)) {
                String name = namespace(attrItem.getNodeName(), withoutNamespaces);
                String value = attrItem.getNodeValue();
                buff.append(name).append("=").append("\"").append(value).append("\"");
            }
        }
        if (node.hasChildNodes()) {
            buff.append(">");
            NodeList children = node.getChildNodes();
            int childrenCount = children.getLength();
            if (childrenCount == 1) {
                Node item = children.item(0);
                NodeType itemType = item.getNodeType();
                if (itemType == NodeType.TEXT_NODE) {
                    if (item.getNodeValue() == null) {
                        buff.append("/>");
                    } else {
                        buff.append(item.getNodeValue());
                        buff.append("</").append(namespace(node.getNodeName(), withoutNamespaces)).append(">");
                    }
                    endTag = false;
                }
            }
            NodeListImpl child = (NodeListImpl) children;
            AtomicBoolean tag = new AtomicBoolean(endTag);
            child.forEach(item -> {
                NodeType itemType = item.getNodeType();
                if (itemType == NodeType.DOCUMENT_NODE || itemType == NodeType.ELEMENT_NODE) {
                    getXMLString((Element) item, withoutNamespaces, buff, tag.get());
                }
            });
        } else {
            if (node.getNodeValue() == null) {
                buff.append("/>");
            } else {
                buff.append(node.getNodeValue());
                buff.append("</").append(namespace(node.getNodeName(), withoutNamespaces)).append(">");
            }
            endTag = false;
        }
        if (endTag) {
            buff.append("</").append(namespace(node.getNodeName(), withoutNamespaces)).append(">");
        }
    } catch (Exception e) {
        logger.severe(e.getMessage());
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NodeListImpl(org.loboevolution.html.dom.nodeimpl.NodeListImpl) NodeType(org.loboevolution.type.NodeType)

Aggregations

NodeType (org.loboevolution.type.NodeType)5 NodeListImpl (org.loboevolution.html.dom.nodeimpl.NodeListImpl)2 DOMException (com.gargoylesoftware.css.dom.DOMException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 XPathContext (org.apache.xpath.XPathContext)1 XObject (org.apache.xpath.objects.XObject)1 DocumentFragmentImpl (org.loboevolution.html.dom.domimpl.DocumentFragmentImpl)1 NodeImpl (org.loboevolution.html.dom.nodeimpl.NodeImpl)1 Document (org.loboevolution.html.node.Document)1 Element (org.loboevolution.html.node.Element)1 NamedNodeMap (org.loboevolution.html.node.NamedNodeMap)1 Node (org.loboevolution.html.node.Node)1 XPathException (org.loboevolution.html.xpath.XPathException)1