List of usage examples for org.dom4j Branch addElement
Element addElement(QName qname);
Element
node with the given QName to this branch and returns a reference to the new node. From source file:org.dentaku.gentaku.tools.cgen.visitor.JellyTemplateGeneratingVisitor.java
License:Apache License
public void handleElement(Element element) throws VisitorException { if (element.getParent().getName().equals("schema")) { QName rootName = DocumentFactory.getInstance().createQName("jelly", "j", "jelly:core"); Branch parent = DocumentHelper.createDocument().addElement(rootName).addNamespace("x", "jelly:xml"); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding(getEncoding()); format.setSuppressDeclaration(false); format.setExpandEmptyElements(false); pushParent(parent.addElement(element.getName())); for (Iterator it = element.elementIterator(); it.hasNext();) { visit((Element) it.next()); }// w ww. j a v a 2 s . co m popParent(); try { String filename = element.getName() + ".jelly"; Writer out = new FileWriter(new File(getRootDir(), filename)); final XMLWriter xmlWriter = new XMLWriter(out, format); xmlWriter.setEscapeText(false); xmlWriter.write(parent); xmlWriter.flush(); xmlWriter.close(); } catch (Exception e) { throw new VisitorException("Exception occurred when running Jelly", e); } topLevelElements.put(element.getName(), element); } else { String refName = element.attributeValue("ref"); if (refName != null) { Branch parent = getCurrentParent(); // create an include QName qName = DocumentFactory.getInstance().createQName("import", "j", "jelly:core"); parent.addElement(qName).addAttribute("uri", refName + ".jelly").addAttribute("inherit", "true"); } } }
From source file:org.projectforge.framework.xstream.XmlObjectWriter.java
License:Open Source License
private Element write(final Branch parent, final Object obj, String name, final boolean asAttribute, final boolean asCDATA) { if (obj == null) { return null; }//from w w w.j a v a2 s. c o m final Class<?> type = obj.getClass(); if (name == null) { name = getAliasMap().getAliasForClass(type); } if (name == null && obj.getClass().isAnnotationPresent(XmlObject.class) == true) { final XmlObject xmlObject = obj.getClass().getAnnotation(XmlObject.class); if (StringUtils.isNotEmpty(xmlObject.alias()) == true) { name = xmlObject.alias(); } } if (name == null) { name = xmlRegistry.getAliasForClass(type); } if (name == null) { name = obj.getClass().getName(); } if (isRegistered(obj) == true) { final Element el = getRegisteredElement(obj); Integer refId; final Attribute attr = el.attribute(ATTR_ID); if (attr == null) { // Id attribute not yet written. So add this attribute: refId = refIdCounter++; el.addAttribute(ATTR_ID, String.valueOf(refId)); } else { refId = NumberHelper.parseInteger(attr.getText()); } if (refId == null) { log.error("Can't parse ref id: " + attr.getText()); } else { final Element element = parent.addElement(name); element.addAttribute(ATTR_REF_ID, String.valueOf(refId)); return element; } } IConverter<?> converter = xmlRegistry.getConverter(type); if (converter != null) { final String sValue = converter.toString(obj); writeValue(parent, obj, name, sValue, asAttribute, asCDATA); return (Element) parent; } else if (Enum.class.isAssignableFrom(type) == true) { final String sValue = ((Enum<?>) obj).name(); writeValue(parent, obj, name, sValue, asAttribute, asCDATA); return (Element) parent; } else if (obj instanceof Collection<?>) { final Element listElement = parent.addElement(name); final Iterator<?> it = ((Collection<?>) obj).iterator(); while (it.hasNext() == true) { write(listElement, it.next(), null, false, false); } return listElement; } final Element element = parent.addElement(name); registerElement(obj, element); final Field[] fields = BeanHelper.getAllDeclaredFields(obj.getClass()); AccessibleObject.setAccessible(fields, true); for (final Field field : fields) { if (field.isAnnotationPresent(XmlOmitField.class) == true || ignoreField(obj, field) == true) { continue; } final XmlField ann = field.isAnnotationPresent(XmlField.class) == true ? field.getAnnotation(XmlField.class) : null; if (onlyAnnotatedFields == true && field.isAnnotationPresent(XmlField.class) == false) { continue; } final Object value = BeanHelper.getFieldValue(obj, field); writeField(field, obj, value, ann, element); } return element; }
From source file:org.projectforge.framework.xstream.XmlObjectWriter.java
License:Open Source License
private void writeValue(final Branch branch, final Object obj, final String key, final String sValue, final boolean asAttribute, final boolean asCDATA) { if (sValue == null) { return;//from ww w . j a v a 2s.co m } if (asAttribute == true) { addAttribute((Element) branch, obj, key, sValue); } else if (asCDATA == true) { branch.addElement(key).add(new DefaultCDATA(sValue)); } else { branch.addElement(key).setText(sValue); } }
From source file:pt.webdetails.cdf.dd.model.meta.writer.cdexml.XmlComponentTypeWriter.java
License:Mozilla Public License
public void write(java.lang.Object output, IThingWriteContext context, Thing t) throws ThingWriteException { //TODO: use methods instead of comments to segment this ComponentType comp = (ComponentType) t; Branch parent = (Branch) output; // Element or Document Element compElem = parent.addElement("DesignerComponent"); writeHeader(comp, compElem);/*w ww . j av a 2s . c om*/ Attribute cdeModelIgnoreAttr = comp.tryGetAttribute("cdeModelIgnore"); Attribute cdeModelPrefixAttr = comp.tryGetAttribute("cdeModelPrefix"); int ignoreCount = 0; if (cdeModelIgnoreAttr != null) { ignoreCount++; } if (cdeModelPrefixAttr != null) { ignoreCount++; } if (comp.getAttributeCount() > ignoreCount) { writeMetadata(comp, compElem); } // ---------------- // CONTENTS Element contentsElem = compElem.addElement("Contents"); Element modelElem = contentsElem.addElement("Model"); if (cdeModelIgnoreAttr != null) { modelElem.addAttribute("ignore", cdeModelIgnoreAttr.getValue()); } if (cdeModelPrefixAttr != null) { modelElem.addAttribute("prefix", cdeModelPrefixAttr.getValue()); } // Property Usages for (String defName : comp.getDefinitionNames()) { Element definitionElem; Iterable<PropertyTypeUsage> propUsages = comp.getPropertiesByDefinition(defName); if (StringUtils.isEmpty(defName)) { definitionElem = modelElem; } else { definitionElem = modelElem.addElement("Definition"); definitionElem.addAttribute("name", defName); } for (PropertyTypeUsage propUsage : propUsages) { String propName = propUsage.getProperty().getName(); // TODO: exclusion of "system" properties should not be done // in such an explicit way. if (!"name".equalsIgnoreCase(propName) && !"priority".equalsIgnoreCase(propName)) { Element propertyElem = definitionElem.addElement("Property"); propertyElem.setText(propName); String propAlias = propUsage.getAlias(); if (propAlias != null && !propAlias.equals(propName)) { propertyElem.addAttribute("name", propAlias); } } } } // ------------------------ // CONTENTS / IMPLEMENTATION Element implElem = null; String implPath = comp.getImplementationPath(); if (StringUtils.isNotEmpty(implPath)) { implElem = contentsElem.addElement("Implementation"); implElem.addElement("Code").addAttribute("src", comp.getImplementationPath()); } if (comp.getResourceCount() > 0) { if (implElem == null) { implElem = contentsElem.addElement("Implementation"); } Element depsElem = implElem.addElement("Dependencies"); Element stylesElem = implElem.addElement("Styles"); Element rawElem = implElem.addElement("Raw"); for (Resource res : comp.getResources()) { Element resElem; if (res.getType() == Resource.Type.SCRIPT) { // Script resElem = depsElem.addElement("Dependency"); resElem.addAttribute("src", res.getSource()); resElem.setText(res.getName()); } else if (res.getType() == Resource.Type.STYLE) { resElem = stylesElem.addElement("Style"); resElem.addAttribute("src", res.getSource()); resElem.setText(res.getName()); } else if (res.getType() == Resource.Type.RAW) { resElem = rawElem.addElement("Code"); // TODO: couldn't find any example using this so I made up the item tag name "Code"... resElem.addAttribute("name", res.getName()); resElem.setText(res.getSource()); } else { continue; } resElem.addAttribute("version", res.getVersion()).addAttribute("app", res.getApp()); } if (!depsElem.hasContent()) { implElem.remove(depsElem); } if (!stylesElem.hasContent()) { implElem.remove(stylesElem); } if (!rawElem.hasContent()) { implElem.remove(rawElem); } } // OwnProperties if (comp.getPropertyUsageCount() > 0) { Element custPropsElem = null; IThingWriterFactory factory = context.getFactory(); for (PropertyTypeUsage propUsage : comp.getPropertyUsages()) { if (propUsage.isOwned()) { if (custPropsElem == null) { if (implElem == null) { implElem = contentsElem.addElement("Implementation"); } custPropsElem = implElem.addElement("CustomProperties"); } PropertyType prop = propUsage.getProperty(); IThingWriter writer; try { writer = factory.getWriter(prop); } catch (UnsupportedThingException ex) { throw new ThingWriteException(ex); } writer.write(custPropsElem, context, prop); } } } }
From source file:pt.webdetails.cdf.dd.model.meta.writer.cdexml.XmlPropertyTypeWriter.java
License:Mozilla Public License
public void write(java.lang.Object output, IThingWriteContext context, Thing t) throws ThingWriteException { PropertyType prop = (PropertyType) t; Branch parent = (Branch) output; // Element or Document boolean isAdvanced = PropertyType.CAT_ADVANCED.equals(prop.getCategory()); Element propElem = parent.addElement("DesignerProperty"); Element headerElem = propElem.addElement("Header"); String defValue = prop.getDefaultValue(); if ("\"\"".equals(defValue)) { defValue = ""; }/*from w ww. ja v a 2s . c om*/ headerElem.addElement("Name").setText(prop.getName()); headerElem.addElement("Parent").setText(prop.getBase()); headerElem.addElement("DefaultValue").setText(defValue); headerElem.addElement("Description").setText(prop.getLabel()); headerElem.addElement("Tooltip").setText(prop.getTooltip()); headerElem.addElement("Advanced").setText(isAdvanced ? "true" : "false"); headerElem.addElement("InputType").setText(prop.getInputType()); headerElem.addElement("OutputType").setText(prop.getValueType().toString()); headerElem.addElement("Order").setText(String.valueOf(prop.getOrder())); headerElem.addElement("Version").setText(prop.getVersion()); headerElem.addElement("Visible").setText(prop.getVisible() ? "true" : "false"); }