Skip to Content.
Sympa Menu

xom-interest - [XOM-interest] Copy functionality for subclassed Elements and Attributes

xom-interest AT lists.ibiblio.org

Subject: XOM API for Processing XML with Java

List archive

Chronological Thread  
  • From: Peter Murray-Rust <pm286 AT cam.ac.uk>
  • To: xom-interest AT lists.ibiblio.org
  • Subject: [XOM-interest] Copy functionality for subclassed Elements and Attributes
  • Date: Sun, 07 Aug 2005 09:11:54 +0100

As mentioned earlier I am converting my CML software to XOM. I need copy
functionality for subclassed Elements, Attributes (conformant to XSD
simpleTypes) and possibly Text (also conformant). I have read the
documentation, the examples and some of the source code, but please
forgive me if I have missed something obvious. I apologize for the
length of this message but maybe there are other users who have the same
requirements.

The design allows for elements, attributes and text from the CML
namespace and from foreign (namespaced and non-namespaced) schemas. The
design is exmplified by the inheritance hierarchy
Element
CMLElement //possibly abstract - haven't yet decided
Molecule
Atom
Attribute
CMLAttribute //to support CML xsd:simpleTypes
Text
CMLText //to support CML xsd:simpleTypes

Elements and CMLElements may have Attributes and CMLAttributes and may
have Element and CMLElement children and also Text and CMLText children.
Mixed content is deprecated in CML itself but some elements may contain
XHTML (or other namespaces which need mixed content). The CML content
models are sufficiently flexible that normally an element can contain
any other elements as children, or a single text child, but the code
should allow for complete generality.

I need the functionality of Molecule.copy() or Molecule(Molecule) which
copies all attributes and children including creation of subclasses.
This an Atom child of Molecule should be deepCopied to an Atom child in
the new Molecule. Similarly a CMLAttribute on Molecule should result in
a new CMLAttribute.

I am unclear about the relation between Element(Element), Element.copy()
and Element.shallowCopy(). I note that copy() is user-deprecated, that
users should use shallowCopy() but that any attributes and children will
be overwritten (but it is not clear where or by what).

Syntactically there seem to be two possibilities (though this goes
against the XOM philosophy):

Molecule newMol = (Molecule) mol.copy();
Molecule newMol = new Molecule(mol);
I assume that the latter is preferred (as the former is
user-deprecated). However since it presumably has to be implemented
recursively it suffers from the same problems of copy(). Note that I
already have a CMLNodeFactory (subclassed from NodeFactory) so as a last
resort I can serialize the Element and parse it. This is rather a
horrible solution which also loses any local fields in the CMLElements
and CMLAttributes.

My current approach (which implicitly uses recursion) is:
CMLElement:
public CMLElement(CMLElement element) {
this(element.getLocalName()); // manages CMLNamespace
for (int i = 0; i < element.getAttributeCount(); i++) {
Attribute att = element.getAttribute(i);
Attribute newAtt = (Attribute) att.copy();
this.addAttribute(newAtt);
}
for (int i = 0; i < element.getChildCount(); i++) {
Node childNode = element.getChild(i);
Node newNode = childNode.copy();
this.appendChild(newNode);
}
}

public Node copy() {
return new CMLElement(this);
}

Molecule:
public Molecule(Molecule mol) {
super(mol);
this.stuff = mol.stuff; // local field
}

public Node copy() {
return new Molecule(this);
}

CMLAttribute:
public CMLAttribute(CMLAttribute att) {
super(att);
this.schemaType = att.schemaType; // local field
}

public Node copy() {
return new CMLAttribute(this);
}

I feel that I have missed something as it makes no explicit use of
shallowCopy() - which I am urged to so - and uses copy() which I am used
not to do. I cannot use copyAttributes() as it is privatized, presumably
for good reasons.

Sorry for the length and detail...

P.

I am very pleased with progress on XOM so far and thank members of the
list for their help

--
Peter Murray-Rust
Unilever Centre for Molecular Informatics,
Department of Chemistry, University of Cambridge
Cambridge, CB2 1EW, UK
Tel: +44-1223-760369





Archive powered by MHonArc 2.6.24.

Top of Page