/**
 * Copyright 2009 The Australian National University (ANU)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.ands.rifcs.base;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * Class representing a RIF-CS address.
 *
 * @author Scott Yeadon
 *
 */
public class Format extends RIFCSElement {
    /** List of Identifier nodes. */
    private List<Identifier> identifiers = new ArrayList<Identifier>();

    /**
     * Construct aa Format object.
     *
     * @param n
     *        A w3c Node, typically an Element
     *
     * @throws RIFCSException A RIFCSException
     */
    protected Format(final Node n) throws RIFCSException {
        super(n, Constants.ELEMENT_FORMAT);
        initStructures();
    }


    /**
     * Create and return an empty identifer.
     *
     * The returned object has no properties or content and is not part
     * of the RIF-CS document, it is essentially a constructor of an object
     * owned by the RIF-CS document. The returned object needs to be
     * "filled out" (e.g. with properties, additional sub-elements, etc)
     * before being added to the RIF-CS document.
     *
     * @return the new Electronic object
     *
     * @throws RIFCSException A RIFCSException
     *
     */
    public final Identifier newIdentifier() throws RIFCSException {
        return new Identifier(this.newElement(Constants.ELEMENT_IDENTIFIER));
    }


    /**
     * Add an identifier to the format object.
     *
     * @param identifier
     *    an Identifier object
     */
    public final void addIdentifier(final Identifier identifier) {
        this.getElement().appendChild(identifier.getElement());
        this.identifiers.add(identifier);
    }


    /**
     * Obtain the identifiers for this format.
     *
     * @return
     *      A list of all identifiers within this format.
     */
    public final List<Identifier> getIdentifiers() {
        return this.identifiers;
    }


    /** Initialisation code for existing documents. A wrapper that
     *  invokes initPhysicals() and initElectronics() in turn.
     *
     * @throws RIFCSException A RIFCSException
     *
     */
    private void initStructures() throws RIFCSException {
        initIdentifiers();
    }


    /** Initialisation code for identifier elements.
     *
     * @throws RIFCSException A RIFCSException
     *
     */
    private void initIdentifiers() throws RIFCSException {
        NodeList nl = super.getElements(Constants.ELEMENT_IDENTIFIER);

        for (int i = 0; i < nl.getLength(); i++) {
            identifiers.add(new Identifier(nl.item(i)));
        }
    }
}

@@ -2268,32 +2268,615 @@
 ifier);%0A    %7D%0A%0A%0A
+    /**%0A     * Convenience method to add an identifier to the format object.%0A     *%0A     * @param identifier%0A     *            an identifier string%0A     * @param type%0A     *            the identifier type%0A     *%0A     * @throws RIFCSException A RIFCSException%0A     *%0A     */%0A    public final void addIdentifier(final String identifier,%0A            final String type) throws RIFCSException %7B%0A        Identifier i = newIdentifier();%0A        i.setType(type);%0A        i.setValue(identifier);%0A        this.getElement().appendChild(i.getElement());%0A        this.identifiers.add(i);%0A    %7D%0A%0A%0A
     /**%0A     * O

Add Format.addIdentifier() convenience method