List of usage examples for org.dom4j.tree FlyweightCDATA FlyweightCDATA
public FlyweightCDATA(String text)
From source file:com.cladonia.xngreditor.actions.ToolsAddNodeAction.java
License:Open Source License
/** * add one of various types of node to the xpath - selected nodes * //from www .j a v a2 s. com * @param document * @param xpathPredicate * @param nodeType * @param namespace * @param name * @param value * @return */ public String addNode(ExchangerDocument document, String xpathPredicate, String nodeType, String namespace, String prefix, String name, String value) throws Exception { Vector nodeList = document.search(xpathPredicate); Vector attributeList = new Vector(); String warning = ""; if (nodeList.size() > 0) { try { for (int cnt = 0; cnt < nodeList.size(); ++cnt) { Node node = (Node) nodeList.get(cnt); if (node instanceof Element) { XElement e = (XElement) node; if (nodeType.equalsIgnoreCase("Element Node")) { QName qname = null; //resolve the namespace string if (!namespace.equalsIgnoreCase("none")) { Namespace newNs; try { newNs = new Namespace(prefix, namespace); qname = new QName(name, newNs); } catch (StringIndexOutOfBoundsException e1) { //cannot parse string MessageHandler.showError(parent, "Could not resolve Namespace:\n" + namespace, "Tools Add Node"); qname = new QName(name); } } else { qname = new QName(name); } XElement newNode = new XElement(qname); e.add(newNode); newNode.setValue(value); } else if (nodeType.equalsIgnoreCase("Attribute Node")) { QName qname = null; //resolve the namespace string if (!namespace.equalsIgnoreCase("none")) { Namespace newNs; try { newNs = new Namespace(namespace.substring(0, namespace.indexOf(":")), namespace.substring(namespace.indexOf(":") + 1, namespace.length())); qname = new QName(name, newNs); } catch (StringIndexOutOfBoundsException e1) { //cannot parse string MessageHandler.showError(parent, "Could not resolve Namespace:\n" + namespace, "Tools Add Node"); qname = new QName(name); } } else { qname = new QName(name); } XAttribute newNode = new XAttribute(qname, value); e.add(newNode); } else if (nodeType.equalsIgnoreCase("Text Node")) { FlyweightText newNode = new FlyweightText(value); e.add(newNode); } else if (nodeType.equalsIgnoreCase("CDATA Section Node")) { FlyweightCDATA newNode = new FlyweightCDATA(value); e.add(newNode); } else if (nodeType.equalsIgnoreCase("Processing Instruction Node")) { FlyweightProcessingInstruction newNode = new FlyweightProcessingInstruction(name, value); e.add(newNode); } else if (nodeType.equalsIgnoreCase("Comment Node")) { FlyweightComment newNode = new FlyweightComment(value); e.add(newNode); } } else if (node instanceof Document) { XDocument d = (XDocument) node; if (nodeType.equalsIgnoreCase("Processing Instruction Node")) { FlyweightProcessingInstruction newNode = new FlyweightProcessingInstruction(name, value); d.add(newNode); } else if (nodeType.equalsIgnoreCase("Comment Node")) { FlyweightComment newNode = new FlyweightComment(value); d.add(newNode); } else { //cant handle any others // can only handle elements MessageHandler.showError(parent, "Cannot add nodes to this xpath\n+" + "XPath: " + xpathPredicate + "refers to a" + node.getNodeTypeName(), "Tools Add Node"); //end for loop cnt = nodeList.size(); return (null); } } else { //can only handle elements MessageHandler.showError(parent, "Cannot add nodes to this xpath\n+" + "XPath: " + xpathPredicate + "refers to a" + node.getNodeTypeName(), "Tools Add Node"); //end for loop cnt = nodeList.size(); return (null); } } } catch (NullPointerException e) { MessageHandler.showError(parent, "XPath: " + xpathPredicate + "\nCannot be resolved", "Tools Add Node"); return (null); } catch (Exception e) { MessageHandler.showError(parent, "Error Adding Node", "Tools Add Node"); return (null); } document.update(); } else { MessageHandler.showError(parent, "No nodes could be found for:\n" + xpathPredicate, "Tools Add Node"); return (null); } return (document.getText()); }
From source file:com.cladonia.xngreditor.actions.ToolsConvertNodeAction.java
License:Open Source License
/** * add one of various types of node to the xpath - selected nodes * //from w w w.jav a2 s . co m * @param document * @param xpathPredicate * @param nodeType * @return */ public String convertNode(ExchangerDocument document, String xpathPredicate, String nodeType) { Vector nodeList = document.search(xpathPredicate); Vector attributeList = new Vector(); String warning = ""; if (nodeList.size() > 0) { try { for (int cnt = 0; cnt < nodeList.size(); ++cnt) { Node node = (Node) nodeList.get(cnt); if (node instanceof Element) { XElement e = (XElement) node; Element parentE = e.getParent(); if ((e.hasChildElements()) || (e.attributeCount() > 0)) { if ((e.hasChildElements())) { MessageHandler.showError(parent, "Cannont convert since node has child elements ", "Tools Convert Node Error"); return (null); } else if (e.attributeCount() > 0) { MessageHandler.showError("Cannont convert since node has attributes", "Tools Convert Node Error"); return (null); } cnt = nodeList.size(); } else { if (nodeType.equalsIgnoreCase("Element Node")) { MessageHandler.showError(parent, "Node is already an Element", "Tools Convert Node Error"); //end loop cnt = nodeList.size(); return (null); } else if (nodeType.equalsIgnoreCase("Attribute Node")) { //check if it has child elements QName qname = e.getQName(); //resolve the namespace string parentE.add(new XAttribute(qname, e.getValue())); parentE.remove(e); } else if (nodeType.equalsIgnoreCase("Text Node")) { FlyweightText newNode = new FlyweightText(e.getText()); parentE.add(newNode); parentE.remove(e); } else if (nodeType.equalsIgnoreCase("CDATA Section Node")) { FlyweightCDATA newNode = new FlyweightCDATA(e.getText()); parentE.add(newNode); parentE.remove(e); } else if (nodeType.equalsIgnoreCase("Processing Instruction Node")) { FlyweightProcessingInstruction newNode = new FlyweightProcessingInstruction( e.getText(), ""); parentE.add(newNode); parentE.remove(e); } else if (nodeType.equalsIgnoreCase("Comment Node")) { FlyweightComment newNode = new FlyweightComment(e.getText()); parentE.add(newNode); parentE.remove(e); } } } else if (node instanceof Attribute) { XAttribute e = (XAttribute) node; Element parentE = e.getParent(); if (nodeType.equalsIgnoreCase("Element Node")) { QName qname = e.getQName(); //resolve the namespace string XElement newE = new XElement(qname); parentE.add(newE); newE.setValue(e.getValue()); parentE.remove(e); } else if (nodeType.equalsIgnoreCase("Attribute Node")) { MessageHandler.showError(parent, "Node is already an Attribute", "Tools Convert Node Error"); //end loop cnt = nodeList.size(); return (null); } else if (nodeType.equalsIgnoreCase("Text Node")) { FlyweightText newNode = new FlyweightText(e.getText()); parentE.add(newNode); parentE.remove(e); } else if (nodeType.equalsIgnoreCase("CDATA Section Node")) { FlyweightCDATA newNode = new FlyweightCDATA(e.getText()); parentE.add(newNode); parentE.remove(e); } else if (nodeType.equalsIgnoreCase("Processing Instruction Node")) { FlyweightProcessingInstruction newNode = new FlyweightProcessingInstruction(e.getText(), ""); parentE.add(newNode); parentE.remove(e); } else if (nodeType.equalsIgnoreCase("Comment Node")) { FlyweightComment newNode = new FlyweightComment(e.getText()); parentE.add(newNode); parentE.remove(e); } } else { //can only handle elements MessageHandler .showError(parent, "Can only Convert Nodes to elements\n+" + "XPath: " + xpathPredicate + "refers to a" + node.getNodeTypeName(), "Tools Convert Node Error"); //end for loop cnt = nodeList.size(); } } } catch (NullPointerException e) { MessageHandler.showError(parent, "XPath: " + xpathPredicate + "\nCannot be resolved", "Tools Convert Node Error"); return (null); } catch (Exception e) { MessageHandler.showError(parent, "Error Adding Node", "Tools Convert Node Error"); return (null); } document.update(); } else { MessageHandler.showError(parent, "No nodes could be found for:\n" + xpathPredicate, "Tools Convert Node Error"); return (null); } return (document.getText()); }
From source file:com.eurelis.opencms.ant.task.ManifestBuilderTask.java
License:Open Source License
public Document createDocument() { document = DocumentHelper.createDocument(); document.setXMLEncoding("UTF-8"); Element root = document.addElement("export"); Element info = root.addElement("info"); if (creator != null) info.addElement("creator").addText(creator); if (opencmsversion != null) info.addElement("opencms_version").addText(opencmsversion); info.addElement("createdate").addText(dateformat.format(new Date())); if (project != null) info.addElement("infoproject").addText(project); if (exportversion != null) info.addElement("export_version").addText(exportversion); Element module = root.addElement("module"); if (name != null) module.addElement("name").addText(name); if (nicename != null) module.addElement("nicename").addText(nicename); if (group != null) module.addElement("group").addText(group); if (moduleclass != null) module.addElement("class").addText(moduleclass); if (moduledescription != null) module.addElement("description").add(new FlyweightCDATA(moduledescription)); if (version != null) module.addElement("version").addText(version); if (authorname != null) module.addElement("authorname").add(new FlyweightCDATA(authorname)); if (authoremail != null) module.addElement("authoremail").add(new FlyweightCDATA(authoremail)); module.addElement("datecreated").addText(dateformat.format(new Date())); module.addElement("userinstalled").addText(userinstalled); module.addElement("dateinstalled").addText(dateinstalled); Element dependenciesBlock = module.addElement("dependencies"); for (Dependency dep : dependencies) { dependenciesBlock.addElement("dependency").addAttribute("name", dep.getName()).addAttribute("version", dep.getVersion());//from ww w.java2 s . co m } Element exportPointsBlock = module.addElement("exportpoints"); for (ExportPoint ep : exportpoints) { exportPointsBlock.addElement("exportpoint").addAttribute("uri", ep.getSrc()).addAttribute("destination", ep.getDst()); } Element resourcesBlock = module.addElement("resources"); for (Resource res : resources) { resourcesBlock.addElement("resource").addAttribute("uri", res.getUri()); } Element parametersBlock = module.addElement("parameters"); for (Parameter par : parameters) { parametersBlock.addElement("param").addAttribute("name", par.getName()).addText(par.getValue()); } insertResourceTypes(module); insertExplorerTypes(module); if (!filesets.isEmpty()) { Element files = root.addElement("files"); for (FileSet fileset : filesets) { DirectoryScanner ds = fileset.getDirectoryScanner(fileset.getProject()); String[] dirs = ds.getIncludedDirectories(); String[] filesColl = ds.getIncludedFiles(); String[] excluDirsArray = ds.getExcludedDirectories(); List<String> excluDirs = new ArrayList<String>(); excluDirs.addAll(Arrays.asList(excluDirsArray)); String[] excluFilesArray = ds.getExcludedFiles(); List<String> excluFiles = new ArrayList<String>(); excluFiles.addAll(Arrays.asList(excluFilesArray)); CmsUUID.init("B4:B6:76:78:7F:3E"); // FOLDERS MANAGEMENT for (int i = 0; i < dirs.length; i++) { String filepath = dirs[i]; String filepathUnix = dirs[i].replace(SEPARATOR, "/"); if (dirs[i] != "") { Element tmpFile = files.addElement("file"); tmpFile.addElement("destination").addText(filepathUnix); String folderPropertiesPath = getProject().getBaseDir() + SEPARATOR + srcfolder + SEPARATOR + folderPropertiesPath(filepath); String tmpType = getEurelisProperty("type", folderPropertiesPath); if (null == tmpType) { tmpType = "folder"; } tmpFile.addElement("type").addText(tmpType); if (generateuuids) { Element uuidNode = tmpFile.addElement("uuidstructure"); String tmpUUID = getEurelisProperty("structureUUID", folderPropertiesPath); if (null != tmpUUID) uuidNode.addText(tmpUUID); else uuidNode.addText(new CmsUUID().toString()); // AJOUTER SAUVEGARDE DU NOUVEL UUID } long date = new File( getProject().getBaseDir() + SEPARATOR + srcfolder + SEPARATOR + filepath) .lastModified(); if (0L == date) date = new Date().getTime(); String formattedDate = dateformat.format(date); tmpFile.addElement("datelastmodified").addText(formattedDate); tmpFile.addElement("userlastmodified").addText("Admin"); // WARNING : CONSTANT VALUE tmpFile.addElement("datecreated").addText(formattedDate); // WARNING : CONSTANT VALUE tmpFile.addElement("usercreated").addText("Admin"); tmpFile.addElement("flags").addText("0"); Element properties = tmpFile.addElement("properties"); // props detection and implementation String tmpPropFile = folderPropertiesPath; addPropertiesToTree(properties, tmpPropFile); String tmpAccessFile = getProject().getBaseDir() + SEPARATOR + srcfolder + SEPARATOR + folderAccessesPath(filepath); addAccessesToTree(tmpFile, tmpAccessFile); } } // FILES MANAGEMENT for (int i = 0; i < filesColl.length; i++) { String filepath = filesColl[i]; String filepathUnix = filesColl[i].replace(SEPARATOR, "/"); if (filesColl[i] != "") { Element tmpFile = files.addElement("file"); tmpFile.addElement("source").addText(filepathUnix); tmpFile.addElement("destination").addText(filepathUnix); String propertiesFilepath = getProject().getBaseDir() + SEPARATOR + srcfolder + SEPARATOR + filePropertiesPath(filepath); String tmpType = getEurelisProperty("type", propertiesFilepath); if (null == tmpType) { if (filepathUnix.endsWith(".config")) tmpType = "module_config"; else if (filepathUnix.endsWith("main.jsp")) tmpType = "containerpage_template"; else if (filepathUnix.endsWith(".jsp")) tmpType = "jsp"; else if (filepathUnix.endsWith(".png") || filepathUnix.endsWith(".gif") || filepathUnix.endsWith(".jpg") || filepathUnix.endsWith(".jpeg")) tmpType = "image"; else if (filepathUnix.endsWith(".html") && filepathUnix.contains("/models/")) tmpType = "containerpage"; else tmpType = "plain"; } tmpFile.addElement("type").addText(tmpType); if (generateuuids) { Element uuidNode = tmpFile.addElement("uuidresource"); Element uuidNode2 = tmpFile.addElement("uuidstructure"); String tmpUUID = getEurelisProperty("resourceUUID", propertiesFilepath); if (null != tmpUUID) uuidNode.addText(tmpUUID); else uuidNode.addText(new CmsUUID().toString()); tmpUUID = getEurelisProperty("structureUUID", propertiesFilepath); if (null != tmpUUID) uuidNode2.addText(tmpUUID); else uuidNode2.addText(new CmsUUID().toString()); } long date = new File( getProject().getBaseDir() + SEPARATOR + srcfolder + SEPARATOR + filepath) .lastModified(); if (0L == date) date = new Date().getTime(); String formattedDate = dateformat.format(date); tmpFile.addElement("datelastmodified").addText(formattedDate); tmpFile.addElement("userlastmodified").addText("Admin"); tmpFile.addElement("datecreated").addText(formattedDate); tmpFile.addElement("usercreated").addText("Admin"); tmpFile.addElement("flags").addText("0"); Element properties = tmpFile.addElement("properties"); String tmpPropFile = propertiesFilepath; addPropertiesToTree(properties, tmpPropFile); tmpFile.addElement("accesscontrol"); } } } } return document; }
From source file:com.eurelis.opencms.ant.task.ManifestBuilderTask.java
License:Open Source License
private void addPropertiesToTree(Element root, String propFilePath) { try {/* w w w . j a v a 2 s . c o m*/ Properties props = new Properties(); if (new File(propFilePath).exists()) props.load(new FileInputStream(propFilePath)); if (!props.isEmpty()) { for (Object keyObject : props.keySet()) { try { String key = (String) keyObject; if (key == null) continue; String value = props.getProperty(key); if (value == null) continue; if (value.length() > 0) { if (key.contains("EurelisProperty")) { continue; } Element property = root.addElement("property"); property.addElement("name") .addText(key.matches("^.*\\.[is]$") ? key.substring(0, key.length() - 2) : key); property.addElement("value").add(new FlyweightCDATA(value)); if (key.endsWith(".s")) property.addAttribute("type", "shared"); } } catch (Exception e) { log(e, Project.MSG_ERR); e.printStackTrace(); } } } } catch (Exception e) { log(e, Project.MSG_ERR); e.printStackTrace(); } }
From source file:org.alfresco.module.org_alfresco_module_wcmquickstart.util.AssetSerializerXmlImpl.java
License:Open Source License
private void writeValue(ValueType valueType, Object value) throws Exception { switch (valueType) { case id://w ww. j a v a2 s .c o m case integer: case bool: case number: writer.write(value.toString()); break; case text: writer.write(new FlyweightCDATA(value.toString())); break; case content: ContentData contentData = (ContentData) value; AttributesImpl attrs = new AttributesImpl(); attrs.addAttribute(null, "mime", "mime", "String", contentData.getMimetype()); attrs.addAttribute(null, "size", "size", "String", Long.toString(contentData.getSize())); attrs.addAttribute(null, "enc", "enc", "String", contentData.getEncoding()); startElement("content", attrs); endElement("content"); break; case time: writer.write(getDateFormat().format((Date) value)); break; case missing: // write nothing break; } }