List of usage examples for org.w3c.dom Element removeAttribute
public void removeAttribute(String name) throws DOMException;
From source file:com.enonic.vertical.adminweb.MenuHandlerServlet.java
private void selectNewParent(HttpServletRequest request, HttpServletResponse response, HttpSession session, AdminService admin, ExtendedMap formItems) throws VerticalAdminException { User user = securityService.getLoggedInAdminConsoleUser(); try {/*from w w w . jav a2 s . c o m*/ DOMSource xmlSource; Map<String, Object> parameters = new HashMap<String, Object>(); int menuKey = formItems.getInt("menukey"); Document menuDoc = XMLTool.domparse(admin.getAdminMenu(user, menuKey)); int menuItemKey = formItems.getInt("key"); Element menuElem = XMLTool.selectElement(menuDoc.getDocumentElement(), "menu[@key = '" + menuKey + "']"); String menuItemName = XMLTool .selectElement(menuDoc.getDocumentElement(), "//menuitem[@key = " + menuItemKey + " ]") .getAttribute("name"); menuElem = XMLTool.renameElement(menuElem, "menutop"); menuElem.removeAttribute("key"); menuDoc = XMLTool.createDocument(menuElem); xmlSource = new DOMSource(menuDoc); addCommonParameters(admin, user, request, parameters, -1, menuKey); parameters.put("page", formItems.get("page")); parameters.put("key", menuItemKey); parameters.put("cur_parent_key", formItems.get("cur_parent_key")); parameters.put("menuitemname", menuItemName); // Stylesheet Source xslSource = AdminStore.getStylesheet(session, "menuitem_move.xsl"); transformXML(session, response.getWriter(), xmlSource, xslSource, parameters); } catch (IOException e) { VerticalAdminLogger.errorAdmin(this.getClass(), 10, "I/O error: %t", e); } catch (TransformerException e) { VerticalAdminLogger.errorAdmin(this.getClass(), 30, "XSLT error: %t", e); } }
From source file:lcmc.data.VMSXML.java
/** Modify xml of some device element. */ private void modifyXML(final Node domainNode, final String domainName, final Map<String, String> tagMap, final Map<String, String> attributeMap, final Map<String, String> parametersMap, final String path, final String elementName, final VirtualHardwareComparator vhc) { final String configName = namesConfigsMap.get(domainName); if (configName == null) { return;//from ww w. ja v a 2s . co m } //final Node domainNode = getDomainNode(domainName); if (domainNode == null) { return; } final XPath xpath = XPathFactory.newInstance().newXPath(); final Node devicesNode = getDevicesNode(xpath, domainNode); if (devicesNode == null) { return; } try { final NodeList nodes = (NodeList) xpath.evaluate(path, domainNode, XPathConstants.NODESET); Element hwNode = vhc.getElement(nodes, parametersMap); if (hwNode == null) { hwNode = (Element) devicesNode .appendChild(domainNode.getOwnerDocument().createElement(elementName)); } for (final String param : parametersMap.keySet()) { final String value = parametersMap.get(param); if (!tagMap.containsKey(param) && attributeMap.containsKey(param)) { /* attribute */ final Node attributeNode = hwNode.getAttributes().getNamedItem(attributeMap.get(param)); if (attributeNode == null) { if (value != null && !"".equals(value)) { hwNode.setAttribute(attributeMap.get(param), value); } } else if (value == null || "".equals(value)) { hwNode.removeAttribute(attributeMap.get(param)); } else { attributeNode.setNodeValue(value); } continue; } Element node = (Element) getChildNode(hwNode, tagMap.get(param)); if ((attributeMap.containsKey(param) || "True".equals(value)) && node == null) { node = (Element) hwNode .appendChild(domainNode.getOwnerDocument().createElement(tagMap.get(param))); } else if (node != null && !attributeMap.containsKey(param) && (value == null || "".equals(value))) { hwNode.removeChild(node); } if (attributeMap.containsKey(param)) { final Node attributeNode = node.getAttributes().getNamedItem(attributeMap.get(param)); if (attributeNode == null) { if (value != null && !"".equals(value)) { node.setAttribute(attributeMap.get(param), value); } } else { if (value == null || "".equals(value)) { node.removeAttribute(attributeMap.get(param)); } else { attributeNode.setNodeValue(value); } } } } final Element hwAddressNode = (Element) getChildNode(hwNode, HW_ADDRESS); if (hwAddressNode != null) { hwNode.removeChild(hwAddressNode); } } catch (final javax.xml.xpath.XPathExpressionException e) { Tools.appError("could not evaluate: ", e); return; } }
From source file:com.twinsoft.convertigo.engine.util.XMLUtils.java
private static Object getValue(Element elt, boolean ignoreStepIds, boolean useType) throws JSONException { Object value = null;//from w w w . j av a2 s .co m try { if (elt.hasAttribute("type")) { String type = elt.getAttribute("type"); if (type.equals("object")) { JSONObject jsonObject = new JSONObject(); NodeList nl = elt.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); if (node instanceof Element) { Element child = (Element) node; String childName = child.hasAttribute("originalKeyName") ? child.getAttribute("originalKeyName") : child.getTagName(); Object childValue = getValue(child, ignoreStepIds, useType); if (childValue != null) { jsonObject.put(childName, childValue); } else { handleElement(child, jsonObject, ignoreStepIds, useType); } } } value = jsonObject; } else if (type.equals("array")) { JSONArray array = new JSONArray(); NodeList nl = elt.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); if (node instanceof Element) { Element child = (Element) node; Object childValue = getValue(child, ignoreStepIds, useType); if (childValue != null) { array.put(childValue); } else { JSONObject obj = new JSONObject(); array.put(obj); handleElement(child, obj, ignoreStepIds, useType); } } } value = array; } else if (type.equals("string")) { value = elt.getTextContent(); } else if (type.equals("boolean")) { value = Boolean.parseBoolean(elt.getTextContent()); } else if (type.equals("null")) { value = JSONObject.NULL; } else if (type.equals("integer")) { value = Integer.parseInt(elt.getTextContent()); } else if (type.equals("long")) { value = Long.parseLong(elt.getTextContent()); } else if (type.equals("double")) { value = Double.parseDouble(elt.getTextContent()); } else if (type.equals("float")) { value = Float.parseFloat(elt.getTextContent()); } if (value != null) { elt.removeAttribute(type); } } } catch (Throwable t) { Engine.logEngine.debug("failed to convert the element " + elt.getTagName(), t); } return value; }
From source file:com.icesoft.faces.context.BridgeFacesContext.java
protected void applyBrowserFormChanges(Map parameters, Document document, Element form) { NodeList inputElements = form.getElementsByTagName("input"); int inputElementsLength = inputElements.getLength(); for (int i = 0; i < inputElementsLength; i++) { Element inputElement = (Element) inputElements.item(i); String id = inputElement.getAttribute("id"); if (!"".equals(id)) { String name = null;// w w w . ja v a2 s. co m if (parameters.containsKey(id)) { String value = ((String[]) parameters.get(id))[0]; //empty string is implied (default) when 'value' attribute is missing if (!"".equals(value)) { if (inputElement.hasAttribute("value")) { inputElement.setAttribute("value", value); } else if (inputElement.getAttribute("type").equals("checkbox")) { inputElement.setAttribute("checked", "checked"); } } else { inputElement.setAttribute("value", ""); } } else if (!"".equals(name = inputElement.getAttribute("name")) && parameters.containsKey(name)) { String type = inputElement.getAttribute("type"); if (type != null && type.equals("checkbox") || type.equals("radio")) { String currValue = inputElement.getAttribute("value"); if (!"".equals(currValue)) { boolean found = false; // For multiple checkboxes, values can have length > 1, // but for multiple radios, values would have at most length=1 String[] values = (String[]) parameters.get(name); if (values != null) { for (int v = 0; v < values.length; v++) { if (currValue.equals(values[v])) { found = true; break; } } } if (found) { // For some reason, our multiple checkbox // components use checked="true", while // our single checkbox components use // checked="checked". The latter complying // with the HTML specification. // Also, radios use checked="checked" if (type.equals("checkbox")) { inputElement.setAttribute("checked", "true"); } else if (type.equals("radio")) { inputElement.setAttribute("checked", "checked"); } } else { inputElement.removeAttribute("checked"); } } } } else { if (inputElement.getAttribute("type").equals("checkbox")) { ////inputElement.setAttribute("checked", ""); inputElement.removeAttribute("checked"); } } } } NodeList textareaElements = form.getElementsByTagName("textarea"); int textareaElementsLength = textareaElements.getLength(); for (int i = 0; i < textareaElementsLength; i++) { Element textareaElement = (Element) textareaElements.item(i); String id = textareaElement.getAttribute("id"); if (!"".equals(id) && parameters.containsKey(id)) { String value = ((String[]) parameters.get(id))[0]; Node firstChild = textareaElement.getFirstChild(); if (null != firstChild) { //set value on the Text node firstChild.setNodeValue(value); } else { //DOM brought back from compression may have no //child for empty TextArea if (value != null && value.length() > 0) { textareaElement.appendChild(document.createTextNode(value)); } } } } NodeList selectElements = form.getElementsByTagName("select"); int selectElementsLength = selectElements.getLength(); for (int i = 0; i < selectElementsLength; i++) { Element selectElement = (Element) selectElements.item(i); String id = selectElement.getAttribute("id"); if (!"".equals(id) && parameters.containsKey(id)) { List values = Arrays.asList((String[]) parameters.get(id)); NodeList optionElements = selectElement.getElementsByTagName("option"); int optionElementsLength = optionElements.getLength(); for (int j = 0; j < optionElementsLength; j++) { Element optionElement = (Element) optionElements.item(j); if (values.contains(optionElement.getAttribute("value"))) { optionElement.setAttribute("selected", "selected"); } else { optionElement.removeAttribute("selected"); } } } } }
From source file:lucee.runtime.config.XMLConfigWebFactory.java
private static void loadORM(ConfigServerImpl configServer, ConfigImpl config, Document doc, Log log) { boolean hasAccess = ConfigWebUtil.hasAccess(config, SecurityManagerImpl.TYPE_ORM); Element orm = hasAccess ? getChildByName(doc.getDocumentElement(), "orm") : null; boolean hasCS = configServer != null; // engine/*ww w. j a v a2s. co m*/ ClassDefinition cdDefault = new ClassDefinitionImpl(DummyORMEngine.class); ClassDefinition cd = null; if (orm != null) { // in the beginning we had attr class but only as default with dummy String cls = orm.getAttribute("class"); if (DummyORMEngine.class.getName().equals(cls)) orm.removeAttribute(cls); cd = getClassDefinition(orm, "engine-", config.getIdentification()); if (cd == null) cd = getClassDefinition(orm, "", config.getIdentification()); } if (cd == null || !cd.hasClass()) { if (configServer != null) cd = configServer.getORMEngineClass(); else cd = cdDefault; } // load class try { cd.getClazz(); // TODO check interface as well } catch (Exception e) { log.error("ORM", e); cd = cdDefault; } config.setORMEngineClass(cd); // config if (orm == null) orm = doc.createElement("orm"); // this is just a dummy ORMConfiguration def = hasCS ? configServer.getORMConfig() : null; ORMConfiguration ormConfig = ORMConfigurationImpl.load(config, null, orm, config.getRootDirectory(), def); config.setORMConfig(ormConfig); }
From source file:com.enonic.vertical.engine.handlers.MenuHandler.java
private void prepareCopy(Element menuitemsElem, CopyContext copyContext) { //int newSiteKey = copyContext.getNewSiteKey(); boolean includeContents = copyContext.isIncludeContents(); Element[] menuitemElems = XMLTool.getElements(menuitemsElem); for (Element menuitemElem : menuitemElems) { String type = menuitemElem.getAttribute("type"); // content is document if ("content".equals(type)) { Element pageElem = XMLTool.getElement(menuitemElem, "page"); pageElem.removeAttribute("key"); //pageElem.setAttribute("sitekey", Integer.toString(newSiteKey)); String oldPageTemplateKey = pageElem.getAttribute("pagetemplatekey"); pageElem.setAttribute("pagetemplatekey", String.valueOf(copyContext.getPageTemplateKey(Integer.parseInt(oldPageTemplateKey)))); Element contentobjectsElem = XMLTool.getElement(pageElem, "contentobjects"); Element[] contentobjectElems = XMLTool.getElements(contentobjectsElem); for (Element contentobjectElem : contentobjectElems) { String oldContentObjectKey = contentobjectElem.getAttribute("conobjkey"); contentobjectElem.setAttribute("conobjkey", String.valueOf(copyContext.getContentObjectKey(Integer.parseInt(oldContentObjectKey)))); }//from ww w . ja v a 2 s.c om Element documentElem = XMLTool.getElement(menuitemElem, "document"); if (!includeContents) { XMLTool.removeChildNodes(documentElem, false); XMLTool.createCDATASection(documentElem.getOwnerDocument(), documentElem, "Scratch document"); } } else if ("page".equals(type)) { Element pageElem = XMLTool.getElement(menuitemElem, "page"); pageElem.removeAttribute("key"); //pageElem.setAttribute("sitekey", Integer.toString(newSiteKey)); String oldPageTemplateKey = pageElem.getAttribute("pagetemplatekey"); pageElem.setAttribute("pagetemplatekey", String.valueOf(copyContext.getPageTemplateKey(Integer.parseInt(oldPageTemplateKey)))); Element contentobjectsElem = XMLTool.getElement(pageElem, "contentobjects"); Element[] contentobjectElems = XMLTool.getElements(contentobjectsElem); for (Element contentobjectElem : contentobjectElems) { String oldContentObjectKey = contentobjectElem.getAttribute("conobjkey"); contentobjectElem.setAttribute("conobjkey", String.valueOf(copyContext.getContentObjectKey(Integer.parseInt(oldContentObjectKey)))); } } Element elem = XMLTool.getElement(menuitemElem, "menuitems"); prepareCopy(elem, copyContext); } }
From source file:com.enonic.vertical.engine.handlers.MenuHandler.java
private void copyMenu(CopyContext copyContext, Element menuElem) throws VerticalCopyException, VerticalCreateException, VerticalUpdateException, VerticalSecurityException { if (menuElem != null) { User user = copyContext.getUser(); int oldMenuKey = Integer.parseInt(menuElem.getAttribute("key")); //if (copyContext.getOldSiteKey() != copyContext.getNewSiteKey()) // menuElem.setAttribute("sitekey", String.valueOf(copyContext.getNewSiteKey())); //else {/*w w w . j a v a 2 s . co m*/ Element nameElem = XMLTool.getElement(menuElem, "name"); Text text = (Text) nameElem.getFirstChild(); Map translationMap = languageMap.getTranslationMap(user.getSelectedLanguageCode()); text.setData(text.getData() + " (" + translationMap.get("%txtCopy%") + ")"); //} Element elem = XMLTool.getElement(menuElem, "firstpage"); String oldFirstPageKey = elem.getAttribute("key"); elem.removeAttribute("key"); elem = XMLTool.getElement(menuElem, "loginpage"); String oldLoginPageKey = elem.getAttribute("key"); elem.removeAttribute("key"); elem = XMLTool.getElement(menuElem, "errorpage"); String oldErrorPageKey = elem.getAttribute("key"); elem.removeAttribute("key"); String oldDefaultPageTemplateKey = null; elem = XMLTool.getElement(menuElem, "defaultpagetemplate"); if (elem != null) { oldDefaultPageTemplateKey = elem.getAttribute("pagetemplatekey"); elem.removeAttribute("pagetemplatekey"); } Document newDoc = XMLTool.createDocument(); Element newMenuElem = (Element) newDoc.importNode(menuElem, true); newDoc.appendChild(newMenuElem); Element menuitemsElem = XMLTool.getElement(newMenuElem, "menuitems"); newMenuElem.removeChild(menuitemsElem); SiteKey menuKey = createMenu(user, copyContext, newDoc, false); // copy content objects and page templates ContentObjectHandler contentObjectHandler = getContentObjectHandler(); contentObjectHandler.copyContentObjects(oldMenuKey, copyContext); PageTemplateHandler pageTemplateHandler = getPageTemplateHandler(); pageTemplateHandler.copyPageTemplates(oldMenuKey, copyContext); Document doc = getMenu(user, menuKey.toInt(), true, true); Element docElem = doc.getDocumentElement(); newMenuElem = (Element) docElem.getFirstChild(); doc.replaceChild(newMenuElem, docElem); Element newMenuitemsElem = (Element) doc.importNode(menuitemsElem, true); menuitemsElem = XMLTool.getElement(newMenuElem, "menuitems"); newMenuElem.replaceChild(newMenuitemsElem, menuitemsElem); // prepare copy of menu items prepareCopy(newMenuitemsElem, copyContext); updateMenu(user, copyContext, doc, false); if (oldFirstPageKey != null && oldFirstPageKey.length() > 0) { elem = XMLTool.createElementIfNotPresent(doc, newMenuElem, "firstpage"); elem.setAttribute("key", String.valueOf(copyContext.getMenuItemKey(Integer.parseInt(oldFirstPageKey)))); } if (oldLoginPageKey != null && oldLoginPageKey.length() > 0) { elem = XMLTool.createElementIfNotPresent(doc, newMenuElem, "loginpage"); elem.setAttribute("key", String.valueOf(copyContext.getMenuItemKey(Integer.parseInt(oldLoginPageKey)))); } if (oldErrorPageKey != null && oldErrorPageKey.length() > 0) { elem = XMLTool.createElementIfNotPresent(doc, newMenuElem, "errorpage"); elem.setAttribute("key", String.valueOf(copyContext.getMenuItemKey(Integer.parseInt(oldErrorPageKey)))); } if (oldDefaultPageTemplateKey != null && oldDefaultPageTemplateKey.length() > 0) { elem = XMLTool.createElement(doc, newMenuElem, "defaultpagetemplate"); elem.setAttribute("pagetemplatekey", String .valueOf(copyContext.getPageTemplateKey(Integer.parseInt(oldDefaultPageTemplateKey)))); } if (copyContext.isIncludeContents()) { menuitemsElem = XMLTool.getElement(newMenuElem, "menuitems"); prepareUpdate(menuitemsElem); } // update default css Element menudataElem = XMLTool.getElement(newMenuElem, "menudata"); Element defaultcssElem = XMLTool.getElement(menudataElem, "defaultcss"); if (defaultcssElem != null) { String cssKey = defaultcssElem.getAttribute("key"); if (cssKey != null && cssKey.length() > 0) { defaultcssElem.setAttribute("key", cssKey); } } updateMenu(user, copyContext, doc, true); // post-process content objects and page templates contentObjectHandler.copyContentObjectsPostOp(oldMenuKey, copyContext); pageTemplateHandler.copyPageTemplatesPostOp(oldMenuKey, copyContext); } }
From source file:com.amalto.core.plugin.base.xslt.XSLTTransformerPluginBean.java
/** * Process the mappings after xsl transformation * //w w w. jav a 2 s. co m * @param xrefElement * @return the processed Element */ private Element processMappings(Element xrefElement) throws XtentisException { try { String xrefcluster = xrefElement.getAttribute("xrefCluster"); //$NON-NLS-1$ String xrefIn = xrefElement.getAttribute("xrefIn"); //$NON-NLS-1$ String xrefOut = xrefElement.getAttribute("xrefOut"); //$NON-NLS-1$ String xrefIgnore = xrefElement.getAttribute("xrefIgnore"); //$NON-NLS-1$ String xrefDefault = xrefElement.getAttribute("xrefDefault"); //$NON-NLS-1$ Logger.getLogger(XSLTTransformerPluginBean.class) .debug("\n xrefIgnore=" + xrefIgnore + "\n xrefDefault=" + xrefDefault); //$NON-NLS-1$ //$NON-NLS-2$ // parse xrefbein dockey1:xrefkey1,dockey2:xrefkey2 String[] mappings = xrefIn.split(","); //$NON-NLS-1$ HashMap<String, String> itemvals = new HashMap<String, String>(); for (int j = 0; j < mappings.length; j++) { String[] relations = mappings[j].split("="); //$NON-NLS-1$ String docpath = relations[0]; String xrefpath = relations[1]; String itemval = ""; //$NON-NLS-1$ try { if (docpath.startsWith("[")) // hardcoded value //$NON-NLS-1$ itemval = docpath.substring(1, docpath.length() - 1); else itemval = Util.getFirstTextNode(xrefElement, docpath); } catch (Exception x) { throw new XtentisException( "Value for business element '" + xrefElement.getNodeName() + '/' + docpath //$NON-NLS-1$ + "' cannot be found!"); //$NON-NLS-1$ } if (itemval == null) itemval = ""; //$NON-NLS-1$ String content = stripeOuterBracket(itemval); if (content.split(",").length >= mappings.length) //$NON-NLS-1$ itemvals.put(xrefpath, content.split(",")[j]); //$NON-NLS-1$ else itemvals.put(xrefpath, ""); //$NON-NLS-1$ } WhereAnd wAnd = new WhereAnd(); Collection<Map.Entry<String, String>> c = itemvals.entrySet(); int i = 0; for (Iterator<Map.Entry<String, String>> iter = c.iterator(); iter.hasNext();) { i++; Map.Entry<String, String> entry = iter.next(); wAnd.add(new WhereCondition(entry.getKey(), WhereCondition.EQUALS, entry.getValue(), WhereCondition.PRE_NONE, false)); } ArrayList<String> resList = Util.getItemCtrl2Local().xPathsSearch(new DataClusterPOJOPK(xrefcluster), null, new ArrayList<String>(Arrays.asList(new String[] { xrefOut })), wAnd, -1, // spell 0, // start 1, // limit false); String val = ""; //$NON-NLS-1$ if ((resList == null) || (resList.size() == 0)) { if (xrefIgnore.equals("true") || xrefIgnore.equals("1")) { //$NON-NLS-1$ //$NON-NLS-2$ if (xrefDefault != null) val = xrefDefault; else val = ""; //$NON-NLS-1$ } else { String ks = ""; //$NON-NLS-1$ c = itemvals.entrySet(); for (Iterator<Map.Entry<String, String>> iter = c.iterator(); iter.hasNext();) { Map.Entry<String, String> entry = iter.next(); ks += " " + entry.getKey() + "=\"" + entry.getValue() + "\""; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ } throw new XtentisException("Foreign keys values not found for: " + ks); //$NON-NLS-1$ } } else { // read result Pattern p = Pattern.compile("<.*?>(.*?)</.*>", Pattern.DOTALL); //$NON-NLS-1$ Matcher m = p.matcher(resList.iterator().next()); if (m.matches()) val = StringEscapeUtils.unescapeXml(m.group(1)); else { Pattern p2 = Pattern.compile("<.*?/>", Pattern.DOTALL); //$NON-NLS-1$ Matcher m2 = p2.matcher(resList.iterator().next()); if (!m2.matches() && !(xrefIgnore.equals("true") || xrefIgnore.equals("1"))) { //$NON-NLS-1$ //$NON-NLS-2$ throw new XtentisException("Result values were not understood for crossref element"); //$NON-NLS-1$ } } } NodeList l = xrefElement.getChildNodes(); for (int j = 0; j < l.getLength(); j++) { switch (l.item(j).getNodeType()) { case Node.TEXT_NODE: l.item(j).setNodeValue(val); break; case Node.ELEMENT_NODE: xrefElement.removeChild(l.item(j)); break; default: } } xrefElement.removeAttribute("xrefCluster"); //$NON-NLS-1$ xrefElement.removeAttribute("xrefIgnore"); //$NON-NLS-1$ xrefElement.removeAttribute("xrefDefault"); //$NON-NLS-1$ xrefElement.removeAttribute("xrefIn"); //$NON-NLS-1$ xrefElement.removeAttribute("xrefOut"); //$NON-NLS-1$ return xrefElement; } catch (Exception e) { String err = "Unable to process the mappings for the element '" + xrefElement.getLocalName() + "'"; //$NON-NLS-1$ //$NON-NLS-2$ LOG.error(err, e); throw new XtentisException(err); } }
From source file:com.icesoft.faces.component.paneltabset.PanelTabSetRenderer.java
/** * writeHeaderCell is a DOM-enabled version of the MyFaces writeHeaderCell * implementation. Calls to the ResponseWriter have been substituted with * DOMContext and w3c DOM API calls./*from ww w .j ava 2s. c o m*/ * * @param domContext * @param facesContext * @param tabSet * @param tab * @param tabIndex * @param active * @param disabled * @param tr * @throws IOException */ protected void writeHeaderCell(DOMContext domContext, FacesContext facesContext, PanelTabSet tabSet, PanelTab tab, int tabIndex, boolean active, boolean disabled, Element tr) throws IOException { String baseClass = tabSet.getStyleClass(); // create a new table data Element using the DOMContext API Element td = domContext.createElement(HTML.TD_ELEM); td.setAttribute(HTML.ID_ATTR, ClientIdPool.get(tabSet.getClientId(facesContext) + "ht" + tabIndex)); // append the td to the tr tr.appendChild(td); String styleClass = tab.getStyleClass(); String label = tab.getLabel(); if (label == null || label.length() == 0) { label = "Tab " + tabIndex; } // label = DOMUtils.escapeAnsi(label); String tabPlacement = ""; if (tabSet.getTabPlacement().equalsIgnoreCase(PanelTabSet.TABPLACEMENT_BOTTOM)) { tabPlacement = CSS_DEFAULT.PANEL_TAB_SET_DEFAULT_BOTTOM; td.setAttribute(HTML.STYLE_ATTR, "vertical-align:top;"); } else { td.setAttribute(HTML.STYLE_ATTR, "vertical-align:bottom;"); } // create a table for the tab Element table = domContext.createElement(HTML.TABLE_ELEM); // append the table to the td td.appendChild(table); table.setAttribute(HTML.CELLPADDING_ATTR, "0"); table.setAttribute(HTML.CELLSPACING_ATTR, "0"); // table will have 3 rows Element tr_top = domContext.createElement(HTML.TR_ELEM); Element tr_mid = domContext.createElement(HTML.TR_ELEM); Element tr_bot = domContext.createElement(HTML.TR_ELEM); // each row will have 3 columns Element td_top_left = domContext.createElement(HTML.TD_ELEM); Element td_top_mid = domContext.createElement(HTML.TD_ELEM); Element td_top_right = domContext.createElement(HTML.TD_ELEM); this.renderSpacerImage(domContext, td_top_left); this.renderSpacerImage(domContext, td_top_mid); this.renderSpacerImage(domContext, td_top_right); Element td_mid_left = domContext.createElement(HTML.TD_ELEM); // the command link will go in this td Element td_mid_mid = domContext.createElement(HTML.TD_ELEM); Element td_mid_right = domContext.createElement(HTML.TD_ELEM); this.renderSpacerImage(domContext, td_mid_left); this.renderSpacerImage(domContext, td_mid_right); Element td_bot_left = domContext.createElement(HTML.TD_ELEM); Element td_bot_mid = domContext.createElement(HTML.TD_ELEM); Element td_bot_right = domContext.createElement(HTML.TD_ELEM); this.renderSpacerImage(domContext, td_bot_left); this.renderSpacerImage(domContext, td_bot_mid); this.renderSpacerImage(domContext, td_bot_right); UIComponent labelFacet = tab.getLabelFacet(); String disableStyleClassSuffix; if (disabled) { disableStyleClassSuffix = "-dis"; if (labelFacet == null) { Text text = domContext.createTextNode(label); td_mid_mid.appendChild(text); } else { Node cursor = domContext.getCursorParent(); domContext.setCursorParent(td_mid_mid); CustomComponentUtils.renderChild(facesContext, labelFacet); domContext.setCursorParent(cursor); } } else { disableStyleClassSuffix = ""; // Build a command link Element link = domContext.createElement(HTML.ANCHOR_ELEM); String linkId = ClientIdPool.get(tabSet.getClientId(facesContext) + "." + tabIndex); link.setAttribute(HTML.NAME_ATTR, linkId); link.setAttribute(HTML.ID_ATTR, linkId); link.setAttribute(HTML.HREF_ATTR, "javascript:;"); link.setAttribute(HTML.CLASS_ATTR, "icePnlTbLblLnk"); renderAttribute(tabSet, link, HTML.TABINDEX_ATTR, HTML.TABINDEX_ATTR); link.setAttribute(DOMPartialViewContext.DATA_ELEMENTUPDATE, linkId); if (labelFacet == null) { td_mid_mid.appendChild(link); // set focus handler if (tabSet.isKeyboardNavigationEnabled()) { link.setAttribute(HTML.ONFOCUS_ATTR, "return Ice.pnlTabOnFocus(this, false, true);"); link.setAttribute(HTML.ONBLUR_ATTR, "return Ice.pnlTabOnBlur(this, false, true);"); } else { link.setAttribute(HTML.ONFOCUS_ATTR, "return Ice.pnlTabOnFocus(this, false, false);"); link.setAttribute(HTML.ONBLUR_ATTR, "return Ice.pnlTabOnBlur(this, false, false);"); } renderLinkText(label, domContext, link, tab, tabSet); } else { //link.setAttribute(HTML.STYLE_ATTR, "display:none;"); // set focus handler if (tabSet.isKeyboardNavigationEnabled()) { link.setAttribute(HTML.ONFOCUS_ATTR, "return Ice.pnlTabOnFocus(this, true, true);"); link.setAttribute(HTML.ONBLUR_ATTR, "return Ice.pnlTabOnBlur(this, true, true);"); } else { link.setAttribute(HTML.ONFOCUS_ATTR, "return Ice.pnlTabOnFocus(this, true, false);"); link.setAttribute(HTML.ONBLUR_ATTR, "return Ice.pnlTabOnBlur(this, true, false);"); } link.setAttribute(HTML.STYLE_ELEM, "position:relative; top:0px;"); Element div = domContext.createElement(HTML.DIV_ELEM); td_mid_mid.appendChild(div); div.setAttribute(HTML.ONCLICK_ATTR, "if(!Ice.isEventSourceInputElement(event)) document.getElementById('" + linkId + "').onclick(event);"); // div.setAttribute(HTML.ONFOCUS_ATTR, "document.getElementById('"+ linkId+"').onclick();"); div.setAttribute(HTML.CLASS_ATTR, "ptfd"); /* if (active) { renderAttribute(tabSet, div, HTML.TABINDEX_ATTR, HTML.TABINDEX_ATTR); } */ div.appendChild(link); Node cursor = domContext.getCursorParent(); domContext.setCursorParent(div); CustomComponentUtils.renderChild(facesContext, labelFacet); domContext.setCursorParent(cursor); label = "<img src='" + CoreUtils.resolveResourceURL(facesContext, "/xmlhttp/css/xp/css-images/spacer.gif") + "' alt=''/>"; Text spacer = domContext.createTextNodeUnescaped(label); link.appendChild(spacer); } Map parameterMap = getParameterMap(facesContext, tab); renderOnClick(facesContext, tabSet, tab, link, parameterMap); Iterator parameterKeys = parameterMap.keySet().iterator(); String nextKey; while (parameterKeys.hasNext()) { nextKey = (String) parameterKeys.next(); FormRenderer.addHiddenField(facesContext, nextKey); } } String tabStyleClass; // set style class attributes if (active) { tabStyleClass = tab.getTabOnClass(tabPlacement); table.setAttribute(HTML.CLASS_ATTR, tabStyleClass); } else // inactive style with mouse over and out { tabStyleClass = tab.getTabOffClass(tabPlacement); table.setAttribute(HTML.CLASS_ATTR, tabStyleClass); if (!disabled) { table.setAttribute(HTML.ONMOUSEOVER_ATTR, "this.className='" + tab.getTabOverClass(tabPlacement) + "';"); table.setAttribute(HTML.ONMOUSEOUT_ATTR, "this.className='" + tab.getTabOffClass(tabPlacement) + "';"); } else { table.removeAttribute(HTML.ONMOUSEOVER_ATTR); table.removeAttribute(HTML.ONMOUSEOUT_ATTR); } } td_top_left.setAttribute(HTML.CLASS_ATTR, CSSNamePool.get(CSS_DEFAULT.PANEL_TAB_SET_DEFAULT_LEFT + CSS_DEFAULT.PANEL_TAB_SET_DEFAULT_TOP + disableStyleClassSuffix)); td_top_mid.setAttribute(HTML.CLASS_ATTR, CSSNamePool.get(CSS_DEFAULT.PANEL_TAB_SET_DEFAULT_MIDDLE + CSS_DEFAULT.PANEL_TAB_SET_DEFAULT_TOP + disableStyleClassSuffix)); td_top_right.setAttribute(HTML.CLASS_ATTR, CSSNamePool.get(CSS_DEFAULT.PANEL_TAB_SET_DEFAULT_RIGHT + CSS_DEFAULT.PANEL_TAB_SET_DEFAULT_TOP + disableStyleClassSuffix)); td_mid_left.setAttribute(HTML.CLASS_ATTR, CSSNamePool.get(CSS_DEFAULT.PANEL_TAB_SET_DEFAULT_LEFT + CSS_DEFAULT.PANEL_TAB_SET_DEFAULT_MIDDLE + disableStyleClassSuffix)); td_mid_mid.setAttribute(HTML.CLASS_ATTR, CSSNamePool.get(CSS_DEFAULT.PANEL_TAB_SET_DEFAULT_MIDDLE + CSS_DEFAULT.PANEL_TAB_SET_DEFAULT_MIDDLE + disableStyleClassSuffix)); td_mid_right.setAttribute(HTML.CLASS_ATTR, CSSNamePool.get(CSS_DEFAULT.PANEL_TAB_SET_DEFAULT_RIGHT + CSS_DEFAULT.PANEL_TAB_SET_DEFAULT_MIDDLE + disableStyleClassSuffix)); td_bot_left.setAttribute(HTML.CLASS_ATTR, CSSNamePool.get(CSS_DEFAULT.PANEL_TAB_SET_DEFAULT_LEFT + CSS_DEFAULT.PANEL_TAB_SET_DEFAULT_BOTTOM + disableStyleClassSuffix)); td_bot_mid.setAttribute(HTML.CLASS_ATTR, CSSNamePool.get(CSS_DEFAULT.PANEL_TAB_SET_DEFAULT_MIDDLE + CSS_DEFAULT.PANEL_TAB_SET_DEFAULT_BOTTOM + disableStyleClassSuffix)); td_bot_right.setAttribute(HTML.CLASS_ATTR, CSSNamePool.get(CSS_DEFAULT.PANEL_TAB_SET_DEFAULT_RIGHT + CSS_DEFAULT.PANEL_TAB_SET_DEFAULT_BOTTOM + disableStyleClassSuffix)); tr_top.appendChild(td_top_left); tr_top.appendChild(td_top_mid); tr_top.appendChild(td_top_right); table.appendChild(tr_top); tr_mid.appendChild(td_mid_left); tr_mid.appendChild(td_mid_mid); tr_mid.appendChild(td_mid_right); table.appendChild(tr_mid); tr_bot.appendChild(td_bot_left); tr_bot.appendChild(td_bot_mid); tr_bot.appendChild(td_bot_right); table.appendChild(tr_bot); // TODO: test passthru attributes //PassThruAttributeRenderer.renderAttributes(facesContext, tab, new String[] { "onclick" }); // append Elements using the w3c DOM API appendChild if (styleClass != null) { td.setAttribute(HTML.CLASS_ATTR, styleClass); } if (tab.getTitle() != null) { td.setAttribute(HTML.TITLE_ATTR, tab.getTitle()); } }
From source file:com.enonic.vertical.adminweb.PageTemplateHandlerServlet.java
public void handlerForm(HttpServletRequest request, HttpServletResponse response, HttpSession session, AdminService admin, ExtendedMap formItems) throws VerticalAdminException { try {/*w w w. ja v a 2 s . c om*/ User user = securityService.getLoggedInAdminConsoleUser(); boolean createPageTemplate; boolean updateStyleSheet = formItems.getBoolean("updatestylesheet", true); boolean updateCSS = formItems.getBoolean("updatecss", false); String xmlData; Document doc; String cssStylesheetKey = null; String datasourcesXML; int menuKey = formItems.getInt("menukey"); ResourceKey stylesheetKey = null; ResourceFile stylesheet = null; ResourceKey cssKey = null; boolean stylesheetValid = false; String cssKeyParam = request.getParameter("selectedcsskey"); if (cssKeyParam != null && !"".equals(cssKeyParam)) { cssKey = new ResourceKey(cssKeyParam); } if (request.getParameter("selstylesheetkey") != null && request.getParameter("selstylesheetkey").length() > 0) { stylesheetKey = new ResourceKey(request.getParameter("selstylesheetkey")); formItems.putString("stylesheetkey", stylesheetKey.toString()); } int key = formItems.getInt("key", -1); // If we have not selected a stylesheet yet if (stylesheetKey == null && cssKey == null && key == -1) { createPageTemplate = true; doc = XMLTool.createDocument("pagetemplates"); Document dsDoc = XMLTool.createDocument("datasources"); datasourcesXML = XMLTool.documentToString(dsDoc); } else { createPageTemplate = (key == -1); int pageTemplateKey; if (stylesheetKey == null && cssKey == null) // createPageTemplate = false { // If we are editing an existing template pageTemplateKey = Integer.parseInt(request.getParameter("key")); xmlData = admin.getPageTemplate(pageTemplateKey); doc = XMLTool.domparse(xmlData); Element pagetemplateElem = XMLTool.getElement(doc.getDocumentElement(), "pagetemplate"); Element stylesheetElem = XMLTool.getElement(pagetemplateElem, "stylesheet"); stylesheetKey = new ResourceKey(stylesheetElem.getAttribute("stylesheetkey")); } else { // If we are making a new template doc = buildPageTemplateXML(formItems, key == -1); Element oldRoot = doc.getDocumentElement(); String keyStr = oldRoot.getAttribute("key"); if (keyStr != null && keyStr.length() > 0) { pageTemplateKey = Integer.parseInt(keyStr); } else { pageTemplateKey = -1; } Element newRoot = XMLTool.createElement(doc, "pagetemplates"); doc.replaceChild(newRoot, oldRoot); newRoot.appendChild(oldRoot); updateStyleSheet = stylesheetKey != null; if (!updateStyleSheet) { Element elem = XMLTool.getElement(oldRoot, "stylesheet"); String styleSheetKeyStr = elem.getAttribute("stylesheetkey"); if (styleSheetKeyStr.length() > 0) { stylesheetKey = new ResourceKey(styleSheetKeyStr); } } } Element cssElem = (Element) XMLTool.selectNode(doc, "/pagetemplates/pagetemplate/css"); if (cssElem != null) { if (updateCSS && cssKey == null) { cssElem.getParentNode().removeChild(cssElem); } else { cssStylesheetKey = cssElem.getAttribute("stylesheetkey"); } } Element ptdElem = (Element) XMLTool.selectNode(doc, "/pagetemplates/pagetemplate/pagetemplatedata"); Element dselem = XMLTool.getElement(ptdElem, "datasources"); if (dselem != null) { Document dsDoc = XMLTool.createDocument(); dsDoc.appendChild(dsDoc.importNode(dselem, true)); datasourcesXML = XMLTool.documentToString(dsDoc); } else { Document dsDoc = XMLTool.createDocument("datasources"); datasourcesXML = XMLTool.documentToString(dsDoc); } if (!updateStyleSheet) { // Insert valuename attributes all parameters Element[] pagetemplateparamElems = XMLTool.getElements(ptdElem, "pagetemplateparameter"); for (Element pagetemplateparamElem : pagetemplateparamElems) { String keyStr = pagetemplateparamElem.getAttribute("value"); if (keyStr != null && keyStr.length() > 0) { String type = pagetemplateparamElem.getAttribute("type"); if ("category".equals(type)) { int categoryKey = Integer.parseInt(keyStr); String name = admin.getCategoryName(categoryKey); pagetemplateparamElem.setAttribute("valuename", name); } else if ("page".equals(type)) { int menuItemKey = Integer.parseInt(keyStr); String name = admin.getMenuItemName(menuItemKey); pagetemplateparamElem.setAttribute("valuename", name); } else if ("resource".equals(type)) { pagetemplateparamElem.setAttribute("valuename", keyStr); } } } } String menuItemsXML = admin.getMenuItemsByPageTemplates(user, new int[] { pageTemplateKey }); Document menuItemsDoc = XMLTool.domparse(menuItemsXML); XMLTool.mergeDocuments(doc, menuItemsDoc, true); } if (stylesheetKey != null && ((createPageTemplate && cssKey == null) || updateStyleSheet)) { Map<String, Element> elemMap = new HashMap<String, Element>(); Element root = doc.getDocumentElement(); if (updateStyleSheet) { // Remove all parameters from xml root = XMLTool.getElement(root, "pagetemplate"); Element pageTemplateParamterRootElement = XMLTool.getElement(root, "pagetemplateparameters"); Element[] pageTemplateParameterElems = XMLTool.getElements(pageTemplateParamterRootElement, "pagetemplateparameter"); for (Element elem : pageTemplateParameterElems) { String name = XMLTool.getElementText(XMLTool.getElement(elem, "name")); elemMap.put(name, elem); } root.removeChild(pageTemplateParamterRootElement); pageTemplateParamterRootElement = XMLTool.getElement(root, "pagetemplatedata"); pageTemplateParameterElems = XMLTool.getElements(pageTemplateParamterRootElement, "pagetemplateparameter"); for (Element elem1 : pageTemplateParameterElems) { String name = elem1.getAttribute("name"); elemMap.put(name, elem1); pageTemplateParamterRootElement.removeChild(elem1); } } Element stylesheetParams = XMLTool.createElement(doc, root, "pagetemplateparameters"); Element pagetemplatedataElem = XMLTool.getElement(root, "pagetemplatedata"); if (pagetemplatedataElem == null) { pagetemplatedataElem = XMLTool.createElement(doc, root, "pagetemplatedata"); } stylesheet = resourceService.getResourceFile(stylesheetKey); if (stylesheet != null) { Document stylesheetDoc = null; try { stylesheetDoc = stylesheet.getDataAsXml().getAsDOMDocument(); stylesheetValid = true; } catch (XMLException e) { } if (stylesheetDoc != null) { Element[] paramElems = XMLTool.getElements(stylesheetDoc.getDocumentElement(), "xsl:param"); for (Element paramElem : paramElems) { Element typeElem = XMLTool.getElement(paramElem, "type"); Element tempElem; String name = paramElem.getAttribute("name"); if (typeElem != null) { String type = XMLTool.getElementText(typeElem); if ("object".equals(type) || "region".equals(type)) { Element elem = elemMap.get(name); if (elem != null && elem.getAttribute("type").length() == 0) { stylesheetParams.appendChild(elem); } else { tempElem = XMLTool.createElement(doc, stylesheetParams, "pagetemplateparameter"); XMLTool.createElement(doc, tempElem, "name", name); } } else { if (elemMap.containsKey(name)) { Element elem = elemMap.get(name); String elemType = elem.getAttribute("type"); if (elemType.length() == 0) { elem.setAttribute("name", name); XMLTool.removeChildNodes(elem, true); } elem.setAttribute("type", type); pagetemplatedataElem.appendChild(elem); } else { tempElem = XMLTool.createElement(doc, pagetemplatedataElem, "pagetemplateparameter"); tempElem.setAttribute("name", name); tempElem.setAttribute("type", type); } } } else { // Alle vanlige parametere, spesifisert som as="xs:string", e.l. i XSL'en. if (elemMap.containsKey(name)) { Element elem = elemMap.get(name); String type = elem.getAttribute("type"); if (type.length() == 0) { elem.setAttribute("name", name); XMLTool.removeChildNodes(elem, true); } else { elem.removeAttribute("type"); elem.removeAttribute("valuename"); } pagetemplatedataElem.appendChild(elem); } else { tempElem = XMLTool.createElement(doc, pagetemplatedataElem, "pagetemplateparameter"); tempElem.setAttribute("name", name); } } } } } } if (stylesheet == null && stylesheetKey != null) { stylesheet = resourceService.getResourceFile(stylesheetKey); } if (stylesheet != null) { Document stylesheetDoc = null; try { stylesheetDoc = stylesheet.getDataAsXml().getAsDOMDocument(); stylesheetValid = true; } catch (XMLException e) { } if (stylesheetDoc != null) { Element tmpElem = XMLTool.createElement(doc.getDocumentElement(), "resource"); tmpElem.appendChild(doc.importNode(stylesheetDoc.getDocumentElement(), true)); } } // Get content types for this site XMLTool.mergeDocuments(doc, admin.getContentTypes(false).getAsDOMDocument(), true); DOMSource xmlSource = new DOMSource(doc); Source xslSource = AdminStore.getStylesheet(session, "pagetemplate_form.xsl"); HashMap<String, Object> parameters = new HashMap<String, Object>(); addCommonParameters(admin, user, request, parameters, -1, menuKey); if (cssStylesheetKey != null) { parameters.put("cssStylesheetKey", cssStylesheetKey); parameters.put("cssStylesheetExist", resourceService.getResourceFile(new ResourceKey(cssStylesheetKey)) == null ? "false" : "true"); } ResourceKey defaultCSSKey = admin.getDefaultCSSByMenu(menuKey); if (defaultCSSKey != null) { parameters.put("defaultcsskey", defaultCSSKey.toString()); parameters.put("defaultcssExist", resourceService.getResourceFile(defaultCSSKey) == null ? "false" : "true"); } if (createPageTemplate) { parameters.put("create", "1"); } else { parameters.put("create", "0"); } parameters.put("page", String.valueOf(request.getParameter("page"))); datasourcesXML = StringUtil.formatXML(datasourcesXML, 2); parameters.put("datasources", datasourcesXML); parameters.put("menukey", formItems.getString("menukey")); parameters.put("selectedtabpageid", formItems.getString("selectedtabpageid", "none")); if (stylesheetKey != null) { parameters.put("selstylesheetkey", stylesheetKey.toString()); parameters.put("selstylesheetExist", stylesheet == null ? "false" : "true"); parameters.put("selstylesheetValid", stylesheetValid ? "true" : "false"); } addAccessLevelParameters(user, parameters); UserEntity defaultRunAsUser = siteDao.findByKey(formItems.getInt("menukey")).resolveDefaultRunAsUser(); String defaultRunAsUserName = "NA"; if (defaultRunAsUser != null) { defaultRunAsUserName = defaultRunAsUser.getDisplayName(); } parameters.put("defaultRunAsUser", defaultRunAsUserName); transformXML(session, response.getWriter(), xmlSource, xslSource, parameters); } catch (TransformerException e) { VerticalAdminLogger.errorAdmin(this.getClass(), 10, "XSLT error: %t", e); } catch (IOException e) { VerticalAdminLogger.errorAdmin(this.getClass(), 20, "I/O error: %t", e); } }