001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2007, GeoTools Project Managment Committee (PMC)
005: * (C) 2007, Geomatys
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation;
010: * version 2.1 of the License.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.image.io.metadata;
018:
019: // J2SE dependencies
020: import java.util.ArrayList;
021: import java.util.List;
022: import org.w3c.dom.Node;
023:
024: // Geotools dependencies
025: import org.geotools.resources.i18n.Errors;
026: import org.geotools.resources.i18n.ErrorKeys;
027:
028: /**
029: * A list of child elements, for example {@code <SampleDimensions>} or {@code <Axis>}.
030: *
031: * @since 2.4
032: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/coverageio/src/main/java/org/geotools/image/io/metadata/ChildList.java $
033: * @version $Id: ChildList.java 26151 2007-07-04 18:54:48Z desruisseaux $
034: * @author Martin Desruisseaux
035: */
036: abstract class ChildList/*<T extends MetadataAccessor>*/extends
037: MetadataAccessor {
038: /**
039: * The list of childs.
040: */
041: private final List/*<T>*/childs;
042:
043: /**
044: * Creates a parser for childs. The arguments are given unchanged to the
045: * {@linkplain MetadataAccessor#MetadataAccessor super-class constructor}.
046: *
047: * @param metadata The metadata node.
048: * @param parentPath The path to the {@linkplain Node node} of interest, or {@code null}
049: * if {@code metadata} is directly the node of interest.
050: * @param childPath The path (relative to {@code parentPath}) to the child
051: * {@linkplain Element elements}, or {@code null} if none.
052: */
053: protected ChildList(final GeographicMetadata metadata,
054: final String parentPath, final String childPath) {
055: super (metadata, parentPath, childPath);
056: final int count = childCount();
057: childs = new ArrayList(count != 0 ? count : 4);
058: }
059:
060: /**
061: * Returns the child at the specified index.
062: *
063: * @param index the child index.
064: * @throws IndexOutOfBoundsException if the index is out of bounds.
065: */
066: public/*T*/MetadataAccessor getChild(final int index)
067: throws IndexOutOfBoundsException {
068: if (index < 0 || index >= childCount()) {
069: throw new IndexOutOfBoundsException(Errors.format(
070: outOfBounds(), new Integer(index)));
071: }
072: while (childs.size() <= index) {
073: childs.add(null);
074: }
075: MetadataAccessor candidate = (MetadataAccessor) childs
076: .get(index);
077: if (candidate == null) {
078: candidate = newChild(index);
079: childs.set(index, candidate);
080: }
081: return candidate;
082: }
083:
084: /**
085: * Creates a new child, append to the list and returns it.
086: */
087: public/*T*/MetadataAccessor addChild() {
088: final int index = appendChild();
089: final MetadataAccessor candidate = newChild(index);
090: assert index == childs.size();
091: childs.add(candidate);
092: return candidate;
093: }
094:
095: /**
096: * Creates a new child at the specified index.
097: */
098: protected abstract/*T*/MetadataAccessor newChild(int index);
099:
100: /**
101: * Returns the key for "out of range" error localization.
102: */
103: int outOfBounds() {
104: return ErrorKeys.INDEX_OUT_OF_BOUNDS_$1;
105: }
106:
107: /**
108: * A list of {@linkplain Band bands}.
109: */
110: static final class Bands extends ChildList/*<Band>*/{
111: /** Creates a parser for bands. */
112: public Bands(final GeographicMetadata metadata) {
113: super (metadata, "SampleDimensions", "SampleDimension");
114: }
115:
116: /** Create a new band. */
117: protected/*<Band>*/MetadataAccessor newChild(final int index) {
118: return new Band(this , index);
119: }
120:
121: /** Returns the key for "out of range" error localization. */
122: int outOfBounds() {
123: return ErrorKeys.BAD_BAND_NUMBER_$1;
124: }
125: }
126:
127: /**
128: * A list of {@linkplain Axis axis}.
129: */
130: static final class Axes extends ChildList/*<Axis>*/{
131: /** Creates a parser for axis. */
132: public Axes(final GeographicMetadata metadata) {
133: super (metadata,
134: "CoordinateReferenceSystem/CoordinateSystem",
135: "Axis");
136: }
137:
138: /** Create a new band. */
139: protected/*<Axis>*/MetadataAccessor newChild(final int index) {
140: return new Axis(this, index);
141: }
142: }
143: }
|