* child element.
*
* @return newly created element for this override
*/
public GbeanType writeXml() {
GbeanType gbean = new GbeanType();
String gbeanName;
if (name instanceof String) {
gbeanName = (String) name;
} else {
gbeanName = name.toString();
}
gbean.setName(gbeanName);
if (gbeanInfo != null) {
gbean.setGbeanInfo(gbeanInfo);
}
if (!load) {
gbean.setLoad(false);
}
if (comment != null) {
gbean.setComment(comment);
}
// attributes
for (Map.Entry<String, String> entry : attributes.entrySet()) {
String name = entry.getKey();
String value = entry.getValue();
if (value == null) {
nullAttributes.add(name);
clearAttributes.remove(name);
} else {
nullAttributes.remove(name);
clearAttributes.remove(name);
if (encryptedAttributes.contains(name)) {
value = EncryptionManager.encrypt(value);
}
/**
* if there was a value such as jdbc url with & then when that value was oulled
* from the config.xml the & would have been replaced/converted to '&', we need to check
* and change it back because an & would create a parse exception.
*/
value = "<attribute xmlns='" + ATTRIBUTE_NAMESPACE + "'>" + value.replaceAll("&(?!amp;)", "&") + "</attribute>";
Reader reader = new StringReader(value);
try {
AttributeType attribute = AttributesXmlUtil.loadAttribute(reader);
attribute.setName(name);
String editorClass = propertyEditors.get(name);
if (null != editorClass) {
attribute.setPropertyEditor(editorClass);
}
gbean.getAttributeOrReference().add(attribute);
} catch (Exception e) {
log.error("Could not serialize attribute " + name + " in gbean " + gbeanName + ", value: " + value, e);
}
}
}
// cleared attributes
for (String name : clearAttributes) {
AttributeType attribute = new AttributeType();
attribute.setName(name);
gbean.getAttributeOrReference().add(attribute);
}
// Null attributes
for (String name : nullAttributes) {
AttributeType attribute = new AttributeType();
attribute.setName(name);
attribute.setNull(true);
gbean.getAttributeOrReference().add(attribute);
}
// references
for (Map.Entry<String, ReferencePatterns> entry : references.entrySet()) {
String name = entry.getKey();
ReferencePatterns patterns = entry.getValue();
ReferenceType reference = new ReferenceType();
reference.setName(name);
Set<AbstractNameQuery> patternSet;
if (patterns.isResolved()) {
patternSet = Collections.singleton(new AbstractNameQuery(patterns.getAbstractName()));
} else {
patternSet = patterns.getPatterns();
}
for (AbstractNameQuery pattern : patternSet) {
ReferenceType.Pattern patternType = new ReferenceType.Pattern();
Artifact artifact = pattern.getArtifact();
if (artifact != null) {
if (artifact.getGroupId() != null) {
patternType.setGroupId(artifact.getGroupId());
}
if (artifact.getArtifactId() != null) {
patternType.setArtifactId(artifact.getArtifactId());
}
if (artifact.getVersion() != null) {
patternType.setVersion(artifact.getVersion().toString());
}
if (artifact.getType() != null) {
patternType.setType(artifact.getType());
}
}
Map nameMap = pattern.getName();
if (nameMap.get("module") != null) {
patternType.setModule((String) nameMap.get("module"));
}
if (nameMap.get("name") != null) {
patternType.setName((String) nameMap.get("name"));
}
reference.getPattern().add(patternType);
}
gbean.getAttributeOrReference().add(reference);
}
// cleared references
for (String name : clearReferences) {
ReferenceType reference = new ReferenceType();
reference.setName(name);
gbean.getAttributeOrReference().add(reference);
}
return gbean;
}