001: /**
002: * Speedo: an implementation of JDO compliant personality on top of JORM generic
003: * I/O sub-system.
004: * Copyright (C) 2001-2004 France Telecom R&D
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; either
009: * version 2 of the License, or (at your option) any later version.
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: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: *
021: *
022: * Contact: speedo@objectweb.org
023: *
024: * Authors: S.Chassande-Barrioz.
025: *
026: */package org.objectweb.speedo.metadata;
027:
028: import java.io.Serializable;
029: import java.lang.reflect.Array;
030: import java.util.ArrayList;
031: import java.util.List;
032:
033: import org.objectweb.speedo.api.SpeedoException;
034: import org.objectweb.speedo.api.SpeedoRuntimeException;
035: import org.objectweb.speedo.lib.Personality;
036:
037: /**
038: * Description of all elements which can contain an extension.
039: * @author S.Chassande-Barrioz
040: */
041: public abstract class SpeedoElement implements Serializable {
042: private static final long serialVersionUID = 457503247952030234L;
043: public Personality personality;
044: /**
045: * The extension corresponding to the element.
046: */
047: public transient List jdoExtension = new ArrayList();
048:
049: public SpeedoElement() {
050: SpeedoDefaults.setDefaults(this );
051: }
052:
053: public String getExtensionValueByKey(String key) {
054: SpeedoExtension se = getExtensionByKey(key);
055: return (se == null ? null : se.value);
056: }
057:
058: public SpeedoExtension getExtensionByKey(String key) {
059: if (jdoExtension == null || key == null) {
060: return null;
061: }
062: for (int i = 0; i < jdoExtension.size(); i++) {
063: SpeedoExtension se = (SpeedoExtension) jdoExtension.get(i);
064: if (key.equals(se.key)) {
065: return se;
066: }
067: }
068: return null;
069: }
070:
071: public SpeedoExtension getExtension(String vendor, String key) {
072: if (jdoExtension == null || vendor == null || key == null) {
073: return null;
074: }
075: for (int i = 0; i < jdoExtension.size(); i++) {
076: SpeedoExtension se = (SpeedoExtension) jdoExtension.get(i);
077: if (vendor.equals(se.vendorName) && key.equals(se.key)) {
078: return se;
079: }
080: }
081: return null;
082: }
083:
084: public void addExtension(SpeedoExtension se) {
085: jdoExtension.add(se);
086: }
087:
088: protected static Object[] setValueInArray(Object value,
089: Object[] array, int pos, int arraySize) {
090: if (array == null) {
091: array = new Object[arraySize];
092: } else if (array.length <= pos) {
093: Object[] values = new Object[array.length + 1];
094: System.arraycopy(array, 0, values, 0, array.length);
095: array = values;
096: }
097: array[pos] = value;
098: return array;
099:
100: }
101:
102: protected static int indexOfInArray(Object[] array, Object element) {
103: int idx;
104: for (idx = 0; idx < array.length && array[idx] != element; idx++)
105: ;
106: return idx;
107: }
108:
109: protected static Object[] addInArray(Object value, Object[] array,
110: Class type) {
111: Object[] newArray;
112: if (array == null) {
113: newArray = (Object[]) Array.newInstance(type
114: .getComponentType(), 1);
115: newArray[0] = value;
116: } else {
117: newArray = (Object[]) Array.newInstance(type
118: .getComponentType(), array.length + 1);
119: System.arraycopy(array, 0, newArray, 0, array.length);
120: newArray[array.length] = value;
121: }
122: for (int i = 0; i < newArray.length; i++) {
123: if (newArray[i] == null) {
124: throw new SpeedoRuntimeException(
125: "a null element found: " + i);
126: }
127: }
128: return newArray;
129: }
130:
131: protected static Object[] removeInArray(Object value,
132: Object[] array, Class type) {
133: Object[] newArray;
134: if (array == null || array.length == 0) {
135: return array;
136: } else {
137: newArray = (Object[]) Array.newInstance(type
138: .getComponentType(), array.length - 1);
139: int idx = 0;
140: for (int i = 0; i < array.length; i++) {
141: if (array[i] != value) {
142: newArray[idx] = array[i];
143: }
144: idx++;
145: }
146: }
147: for (int i = 0; i < newArray.length; i++) {
148: if (newArray[i] == null) {
149: throw new SpeedoRuntimeException(
150: "a null element found: " + i);
151: }
152: }
153: return newArray;
154: }
155: }
|