001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2003-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.data;
017:
018: import java.io.IOException;
019:
020: import org.geotools.feature.AttributeType;
021:
022: /** Provides ...
023: *
024: * @author Sean Geoghegan, Defence Science and Technology Organisation.
025: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/data/JoiningAttributeWriter.java $
026: */
027: public class JoiningAttributeWriter implements AttributeWriter {
028: private AttributeWriter[] writers;
029: private int[] index;
030: private AttributeType[] metaData;
031:
032: /**
033: *
034: */
035: public JoiningAttributeWriter(AttributeWriter[] writers) {
036: this .writers = writers;
037: metaData = joinMetaData(writers);
038: }
039:
040: private AttributeType[] joinMetaData(AttributeWriter[] writers) {
041: int total = 0;
042: index = new int[writers.length];
043: for (int i = 0, ii = writers.length; i < ii; i++) {
044: index[i] = total;
045: total += writers[i].getAttributeCount();
046: }
047: AttributeType[] md = new AttributeType[total];
048: int idx = 0;
049: for (int i = 0, ii = writers.length; i < ii; i++) {
050: for (int j = 0, jj = writers[i].getAttributeCount(); j < jj; j++) {
051: md[idx] = writers[i].getAttributeType(j);
052: idx++;
053: }
054: }
055: return md;
056: }
057:
058: /* (non-Javadoc)
059: * @see org.geotools.data.AttributeWriter#close()
060: */
061: public void close() throws IOException {
062: IOException dse = null;
063: for (int i = 0, ii = writers.length; i < ii; i++) {
064: try {
065: writers[i].close();
066: } catch (IOException e) {
067: dse = e;
068: }
069: }
070: if (dse != null)
071: throw dse;
072:
073: }
074:
075: public boolean hasNext() throws IOException {
076: for (int i = 0, ii = writers.length; i < ii; i++) {
077: if (writers[i].hasNext()) {
078: return true;
079: }
080: }
081: return false;
082: }
083:
084: public void next() throws IOException {
085: for (int i = 0, ii = writers.length; i < ii; i++) {
086: //if (writers[i].hasNext()) Dont want to check this, need to be able to insert
087: writers[i].next();
088: }
089: }
090:
091: /* (non-Javadoc)
092: * @see org.geotools.data.AttributeWriter#write(int, java.lang.Object)
093: */
094: public void write(int position, Object attribute)
095: throws IOException {
096: AttributeWriter writer = null;
097: for (int i = index.length - 1; i >= 0; i--) {
098: if (position >= index[i]) {
099: position -= index[i];
100: writer = writers[i];
101: break;
102: }
103: }
104: if (writer == null)
105: throw new ArrayIndexOutOfBoundsException(position);
106:
107: writer.write(position, attribute);
108: }
109:
110: public int getAttributeCount() {
111: return metaData.length;
112: }
113:
114: public AttributeType getAttributeType(int i) {
115: return metaData[i];
116: }
117: }
|