Example usage for org.w3c.dom Element getOwnerDocument

List of usage examples for org.w3c.dom Element getOwnerDocument

Introduction

In this page you can find the example usage for org.w3c.dom Element getOwnerDocument.

Prototype

public Document getOwnerDocument();

Source Link

Document

The Document object associated with this node.

Usage

From source file:com.enonic.vertical.engine.handlers.MenuHandler.java

/**
 * Builds and appends the xml for a given menuitem into a given menuitems element.
 *
 * @param user           The current user
 * @param sqlSelectByKey The sql string (that selects the one menuitem)
 * @param menuItemKey    The key to the menuitem to build
 * @param menuItems      The element to append the menuitem Element into
 * @return The menuitem element//from   ww  w. j a  v a  2 s .  co m
 * @throws SQLException When the menu item cannot be found or a more serious database error happens.
 */
private Element buildMenuItem(User user, String sqlSelectByKey, int menuItemKey, Element menuItems)
        throws SQLException {

    PreparedStatement statement = null;
    ResultSet resultSet = null;
    Connection con = null;
    try {
        con = getConnection();
        statement = con.prepareStatement(sqlSelectByKey);
        statement.setInt(1, menuItemKey);
        resultSet = statement.executeQuery();
        resultSet.next();

        Document document = menuItems.getOwnerDocument();
        Element menuItem = buildMenuItemXML(document, menuItems, resultSet, -1, false, false, true, true, true,
                1);
        Element accessRights = XMLTool.createElement(document, menuItem, "accessrights");
        getSecurityHandler().appendAccessRightsOnMenuItem(user, menuItemKey, accessRights, true);

        return menuItem;
    } finally {
        close(resultSet);
        close(statement);
        close(con);
    }
}

From source file:com.enonic.vertical.engine.handlers.MenuHandler.java

private void buildPageTypeXML(ResultSet result, Document doc, Element menuItemElement,
        boolean includePageConfig) throws SQLException {

    int pKey = result.getInt("mei_pag_lKey");

    if (includePageConfig) {
        Document pageDoc = getPageHandler().getPage(pKey, includePageConfig).getAsDOMDocument();
        Element rootElem = pageDoc.getDocumentElement();
        Element pageElem = XMLTool.getElement(rootElem, "page");
        menuItemElement.appendChild(menuItemElement.getOwnerDocument().importNode(pageElem, true));
    } else {/*from w w  w .  j  a v  a 2  s  .c om*/
        Element pageElem = XMLTool.createElement(doc, menuItemElement, "page");
        pageElem.setAttribute("key", String.valueOf(pKey));

        // extract pagetemplate key
        PageTemplateKey ptKey = getPageHandler().getPageTemplateKey(pKey);
        pageElem.setAttribute("pagetemplatekey", ptKey.toString());
        PageTemplateType pageTemplateType = getPageTemplateHandler().getPageTemplateType(ptKey);
        pageElem.setAttribute("pagetemplatetype", String.valueOf(pageTemplateType.getKey()));
    }
}

From source file:com.enonic.vertical.engine.handlers.MenuHandler.java

private void buildDocumentTypeXML(Element menuitemElem, Element documentElem) {

    if (documentElem != null) {
        if (verticalProperties.isStoreXHTMLOn()) {
            Node n = documentElem.getFirstChild();
            if (n != null && n.getNodeType() == Node.CDATA_SECTION_NODE) {
                int menuItemKey = Integer.parseInt(menuitemElem.getAttribute("key"));
                String menuItemName = XMLTool.getElementText(XMLTool.getElement(menuitemElem, "name"));
                Document doc = menuitemElem.getOwnerDocument();
                String docString = XMLTool.getElementText(documentElem);
                documentElem.removeChild(n);
                XMLTool.createXHTMLNodes(doc, documentElem, docString, true);
                String menuKey = menuitemElem.getAttribute("menukey");
                VerticalAdminLogger.error(this.getClass(), 0,
                        "Received invalid XML from database, menukey=" + menuKey + ", menuitem key="
                                + menuItemKey + ", name=" + menuItemName + ". Running Tidy..",
                        null);//from  w w  w .  j  av a2  s.co m
            }
            documentElem.setAttribute("mode", "xhtml");
        } else {
            Node n = documentElem.getFirstChild();
            if (n == null) {
                Document doc = menuitemElem.getOwnerDocument();
                XMLTool.createCDATASection(doc, menuitemElem, "Scratch document.");
            } else if (n.getNodeType() != Node.CDATA_SECTION_NODE) {
                Document doc = menuitemElem.getOwnerDocument();
                String docString = XMLTool.serialize(documentElem);
                XMLTool.createCDATASection(doc, documentElem, docString);
                VerticalEngineLogger.debug(this.getClass(), 0, "Expected CDATA, found XML. Serialized it.",
                        null);
            }
        }
    }
}

From source file:com.codename1.android.AndroidLayoutImporter.java

private String get(Element el, String att, String defaultValue) {
    if (el.getOwnerDocument() == inputDOM && !el.hasAttributeNS(NS_ANDROID, att)) {
        return defaultValue;
    } else if (el.getOwnerDocument() != inputDOM && !el.hasAttribute(att)) {
        return defaultValue;
    }// w  ww . ja v  a  2  s  . c om

    String val = el.getOwnerDocument() == inputDOM ? el.getAttributeNS(NS_ANDROID, att) : el.getAttribute(att);
    if (val == null) {
        return defaultValue;
    }
    return val;
}

From source file:com.codename1.android.AndroidLayoutImporter.java

private int get(Element el, String att, int defaultValue) {
    String val = el.getOwnerDocument() == inputDOM ? el.getAttributeNS(NS_ANDROID, att) : el.getAttribute(att);
    if (val == null) {
        return defaultValue;
    }//from  www  . ja v  a2 s . c  o  m
    int out = defaultValue;
    try {
        out = Integer.parseInt(val);
    } catch (Exception ex) {
    }
    return out;
}

From source file:com.codename1.android.AndroidLayoutImporter.java

private double get(Element el, String att, double defaultValue) {
    String val = el.getOwnerDocument() == inputDOM ? el.getAttributeNS(NS_ANDROID, att) : el.getAttribute(att);
    if (val == null) {
        return defaultValue;
    }//from   ww w .j a v  a 2 s  .com
    double out = defaultValue;
    try {
        out = Double.parseDouble(val);
    } catch (Exception ex) {
    }
    return out;
}

From source file:com.codename1.android.AndroidLayoutImporter.java

protected void convertElement(Element inputSrcElement, Element out) throws UnsupportedElementException {
    String id = inputSrcElement.getAttributeNS(NS_ANDROID, "id");
    if (id != null) {
        id = id.substring(id.indexOf("/") + 1);
        out.setAttribute("name", id.replaceAll("[^a-zA-Z0-9]", ""));
    }/*from   www  .  j  a  v a  2 s .  c  o  m*/

    //out.setAttribute("uiid", "android."+inputSrcElement.getTagName());
    out.setAttribute("type", "Container");
    out.setAttribute("layout", "FlowLayout");

    Node inputParentNode = inputSrcElement.getParentNode();
    if (inputParentNode != null && inputParentNode instanceof Element) {
        Element inputParent = (Element) inputParentNode;
        if ("FrameLayout".equals(inputParent.getTagName())) {
            out.setAttribute("layout", "BorderLayout");
            String gravity = inputSrcElement.getAttributeNS(NS_ANDROID, "layout_gravity");
            String layoutConstraint = "CENTER";
            switch (gravity) {
            case "top":
                layoutConstraint = "NORTH";
                break;
            case "left":
                layoutConstraint = "WEST";
                break;
            case "right":
                layoutConstraint = "EAST";
                break;
            case "bottom":
                layoutConstraint = "SOUTH";
                break;
            case "fill_vertical":
            case "center_vertical":
            case "center_horizontal":
            case "fill_horizontal":
            case "center":
            case "fill":

                layoutConstraint = "CENTER";
                break;
            default:
                layoutConstraint = "CENTER";
            }
            Element layoutConstraintEl = out.getOwnerDocument().createElement("layoutConstraint");
            layoutConstraintEl.setAttribute("value", layoutConstraint);
            out.appendChild(layoutConstraintEl);
        }
    }

    switch (inputSrcElement.getTagName()) {
    case "LinearLayout":
        convertLinearLayout(inputSrcElement, out);
        break;

    case "android.support.v7.widget.RecyclerView":
        convertRecyclerView(inputSrcElement, out);
        break;

    case "FrameLayout":
        convertFrameLayout(inputSrcElement, out);
        break;

    case "android.support.design.widget.CoordinatorLayout":
        convertCoordinatorLayout(inputSrcElement, out);
        break;

    case "android.support.design.widget.CollapsingToolbarLayout":
        convertCollapsingToolbarLayout(inputSrcElement, out);
        break;

    case "android.support.design.widget.AppBarLayout":
        convertAppBarLayout(inputSrcElement, out);
        break;

    case "android.support.v7.widget.Toolbar":
        convertToolbar(inputSrcElement, out);
        break;

    case "android.support.design.widget.FloatingActionButton":
        convertFloatingActionButton(inputSrcElement, out);
        break;

    case "include": {
        try {
            convertInclude(inputSrcElement, out);
        } catch (IOException ex) {
            Logger.getLogger(AndroidLayoutImporter.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(AndroidLayoutImporter.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(AndroidLayoutImporter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
        break;

    case "android.support.v4.widget.NestedScrollView":
        convertNestedScrollView(inputSrcElement, out);
        break;

    case "ListView":
        convertListView(inputSrcElement, out);
        break;

    case "ProgressBar":
        convertProgressBar(inputSrcElement, out);
        break;

    case "TextView":
        convertTextView(inputSrcElement, out);
        break;

    case "Button":
        convertButton(inputSrcElement, out);
        break;

    case "ScrollView":
        convertScrollView(inputSrcElement, out);
        break;

    case "EditText":
        convertEditText(inputSrcElement, out);
        break;

    case "CheckBox":
        convertCheckBox(inputSrcElement, out);
        break;

    case "View":
        convertView(inputSrcElement, out);
        break;

    case "merge":
        convertMerge(inputSrcElement, out);
        break;

    case "RadioGroup":
        convertRadioGroup(inputSrcElement, out);
        break;

    case "RadioButton":
        convertRadioButton(inputSrcElement, out);
        break;

    case "Spinner":
        convertSpinner(inputSrcElement, out);
        break;

    case "RelativeLayout":
        convertRelativeLayout(inputSrcElement, out);
        break;

    case "ImageButton":
        convertImageButton(inputSrcElement, out);
        break;

    case "ImageView":
        convertImageView(inputSrcElement, out);
        break;

    case "SeekBar":
        convertSeekBar(inputSrcElement, out);
        break;

    case "QuickContactBadge":
        convertQuickContactBadge(inputSrcElement, out);
        break;

    case "ViewAnimator":
        convertViewAnimator(inputSrcElement, out);
        break;

    case "ViewStub":
        convertViewStub(inputSrcElement, out);
        break;

    case "Space":
        convertSpace(inputSrcElement, out);
        break;

    default:
        if (suppressUnsupportedElementExceptions) {
            this.convertOther(inputSrcElement, out);
        } else {
            throw new UnsupportedElementException(inputSrcElement);
        }

    }

    convertChildren(inputSrcElement, out);

    if (out.hasAttribute("layout") && "BorderLayout".equals(out.getAttribute("layout"))) {
        // Make sure that border layout children have appropriate constraint
        Set<String> allowable = new HashSet<String>(
                Arrays.asList(new String[] { "NORTH", "SOUTH", "EAST", "WEST", "CENTER" }));
        Map<String, Element> childMap = new HashMap<String, Element>();
        List<ElementConstraint> childList = getBorderLayoutChildren(out);
        Set<ElementConstraint> used = new HashSet<ElementConstraint>();
        for (ElementConstraint ec : childList) {
            if (ec.constraint != null && allowable.contains(ec.constraint)
                    && !childMap.containsKey(ec.constraint)) {
                childMap.put(ec.constraint, ec.el);
                used.add(ec);
            }
        }

        childList.removeAll(used);

        if (!childList.isEmpty()) {
            // We weren't able to place all of the elements in the border layout
            // this is either because we filled all of the slots, or there were two
            // in some slots

            // Let's simplify things for now by just adding all of the extras in a 
            // BoxLayout.Y inside the CENTER
            Element center = out.getOwnerDocument().createElement("component");
            center.setAttribute("type", "Container");
            applyBoxLayoutX(center);
            Element centerConstraint = out.getOwnerDocument().createElement("layoutConstraint");
            centerConstraint.setAttribute("value", "CENTER");
            center.appendChild(centerConstraint);
            if (childMap.containsKey("CENTER")) {
                //System.out.println("Removing center from "+childMap);
                out.removeChild(childMap.get("CENTER"));
                center.appendChild(childMap.get("CENTER"));
            }

            for (ElementConstraint ec : childList) {
                removeLayoutConstraint(ec.el);

                out.removeChild(ec.el);
                center.appendChild(ec.el);
            }
            out.appendChild(center);

        }
    } else if (out.hasAttribute("layout")) {
        // Not a boxlayout.
        NodeList children = out.getChildNodes();
        int len = children.getLength();
        for (int i = 0; i < len; i++) {
            Node n = children.item(i);
            if (n instanceof Element) {
                removeLayoutConstraint((Element) n);
            }
        }
    }

    applyStyles(inputSrcElement, out);
}

From source file:com.codename1.android.AndroidLayoutImporter.java

protected void convertChildren(Element inputEl, Element outputEl) throws UnsupportedElementException {
    NodeList childNodes = inputEl.getChildNodes();
    int len = childNodes.getLength();
    for (int i = 0; i < len; i++) {
        Node child = childNodes.item(i);
        if (child instanceof Element) {
            Element converted = convertElement(inputEl.getOwnerDocument(), outputEl.getOwnerDocument(),
                    (Element) child);
            if (converted != null) {
                if (!appendChild(inputEl, outputEl, (Element) child, converted)) {
                    outputEl.appendChild(converted);
                }//from   w w  w  .j a  va 2 s .com
            }
        } else {
            Node n = convertNode(inputEl, outputEl);
            if (n != null) {
                outputEl.appendChild(n);
            }
        }
    }
}

From source file:net.wastl.webmail.xml.XMLCommon.java

/**
 * Set the node value of the node selected by the given xpath expression.
 *//*from w  ww  .ja  va  2 s .  com*/
public static void setValueXPath(Element root, String path, String value) {
    root.normalize();
    try {
        Node n = XPathAPI.selectSingleNode(root, path);
        if (n != null) {
            n.setNodeValue(value);
        } else {
            addNodeXPath(root, getParentXPath(path), root.getOwnerDocument().createTextNode(value));
        }
    } catch (TransformerException ex) {
        addNodeXPath(root, getParentXPath(path), root.getOwnerDocument().createTextNode(value));
    } catch (Exception ex) {
        log.error("Failed to set value '" + value + "' for path '" + path + "'.  Continuing, but should not.",
                ex);
        // TODO:  Throw here.
    }
}

From source file:net.wastl.webmail.xml.XMLCommon.java

public static void setTagValue(Element e, String tagname, String text, boolean unique, String errormsg,
        boolean cdata) throws Exception {
    if (text == null || tagname == null)
        throw new NullPointerException("Text or Tagname may not be null!");

    Document root = e.getOwnerDocument();

    if (unique) {
        // Check for double entries!
        NodeList nl = ((Element) e.getParentNode()).getElementsByTagName(tagname);
        for (int i = 0; i < nl.getLength(); i++) {
            if (getElementTextValue((Element) nl.item(0)).equals(text)) {
                throw new Exception(errormsg);
            }/*from  w ww.j  av  a 2s .c  o m*/
        }
    }
    NodeList namel = e.getElementsByTagName(tagname);
    Element elem;
    if (namel.getLength() <= 0) {
        log.debug("Creating Element " + tagname + "; will set to " + text);
        elem = root.createElement(tagname);
        e.appendChild(elem);
    } else {
        elem = (Element) namel.item(0);
    }
    //debugXML(root);
    setElementTextValue(elem, text, cdata);
}