Revision 7744

View differences:

trunk/libraries/libIverUtiles/src/com/iver/utiles/XMLEntity.java
40 40
 */
41 41
package com.iver.utiles;
42 42

  
43
import java.io.IOException;
44
import java.io.StringReader;
45
import java.io.StringWriter;
46
import java.util.Iterator;
47
import java.util.NoSuchElementException;
48

  
49
import org.exolab.castor.xml.MarshalException;
50
import org.exolab.castor.xml.Marshaller;
51
import org.exolab.castor.xml.ValidationException;
52

  
43 53
import com.iver.utiles.xmlEntity.generate.Property;
44 54
import com.iver.utiles.xmlEntity.generate.XmlTag;
45 55

  
......
1029 1039
	 * Elimina todos los hijos de XMLEntity.
1030 1040
	 *
1031 1041
	 */
1032
	public void removeAllChilds() {
1042
	public void removeAllChildren() {
1033 1043
		xmltag.removeAllXmlTag();
1034 1044
	}
1035 1045

  
......
1105 1115
		}
1106 1116
		return result;
1107 1117
	}
1118
	/**
1119
	 * Devuelve el primer hijo que el valor de su propieda 'key'
1120
	 * es igual a 'value'
1121
	 * 
1122
	 * @param key propiedad a comparar 
1123
	 * @param value valor a comparar
1124
	 * @return XMLEntity hijo o null si no se encuentra
1125
	 */
1126
	
1127
	public XMLEntity firstChild(String key, String value) {
1128
		int num = this.getChildrenCount();
1129
		XMLEntity child;
1130
		for (int i=0;i < num; i++) {
1131
			child = this.getChild(i);
1132
			if (child.getStringProperty(key).equals(value)) {
1133
				return child;
1134
			}
1135
		}
1136
		return null;		
1137
	}
1138
	
1139
	/**
1140
	 * Devuelve el indice del primer hijo que el valor de su propieda 'key'
1141
	 * es igual a 'value'
1142
	 * 
1143
	 * @param key propiedad a comparar 
1144
	 * @param value valor a comparar
1145
	 * @return int indice del hijo o -1 si no se encuentra
1146
	 */
1147
	public int firstIndexOfChild(String key, String value) {
1148
		int num = this.getChildrenCount();
1149
		XMLEntity child;
1150
		for (int i=0;i < num; i++) {
1151
			child = this.getChild(i);
1152
			if (child.getStringProperty(key).equals(value)) {
1153
				return i;
1154
			}
1155
		}
1156
		return -1;		
1157
	}
1158
		
1159
	/**
1160
	 * Devuelve un iterador sobre los hijos que cumplen la condicion
1161
	 * que el valor de su propiedad 'key' es igual a 'value'
1162
	 * 
1163
	 * El iterador no permite eliminacion
1164
	 * 
1165
	 * @param key nombre de la propidedad
1166
	 * @param value valor de la propiedad
1167
	 * @return
1168
	 */
1169
	public Iterator findChildren(String key, String value) {
1170
		return new XMLEntityIterator(this,key,value);
1171
	}
1172
	
1173
	public String toString() {
1174
		StringWriter buffer = new StringWriter();
1175

  
1176
		Marshaller m;
1177
		try {
1178
			m = new Marshaller(buffer);
1179
		} catch (IOException e4) {
1180
			// TODO Auto-generated catch block
1181
			e4.printStackTrace();
1182
			return null;
1183
		}
1184
		m.setEncoding("ISO-8859-1");
1185

  
1186
		try {
1187
			m.marshal(this.getXmlTag());			
1188
		} catch (MarshalException e2) {
1189
			// TODO Auto-generated catch block
1190
			e2.printStackTrace();
1191
			return null;
1192
		} catch (ValidationException e3) {
1193
			// TODO Auto-generated catch block
1194
			e3.printStackTrace();
1195
			return null;
1196
		}
1197

  
1198
		return buffer.toString();		
1199
	}
1200
	
1201
	public static XMLEntity parse(String data) throws MarshalException, ValidationException {
1202
		StringReader reader = new StringReader(data);
1203

  
1204
		XmlTag tag;
1205
		tag = (XmlTag) XmlTag.unmarshal(reader);
1206
		return new XMLEntity(tag);
1207
	}
1208
	
1108 1209
}
1210

  
1211
class XMLEntityIterator implements Iterator {
1212
	private int lastIndex;
1213
	private int lastHasNextIndex;
1214
	private XMLEntity entity;
1215
	private String key;
1216
	private String value;
1217
	
1218
	public XMLEntityIterator(XMLEntity entity,String key,String value) {
1219
		this.entity = entity;
1220
		this.key = key;
1221
		this.value = value;
1222
		this.lastIndex = -1;
1223
		this.lastHasNextIndex = -1;
1224
	}
1225
	public void remove() {
1226
		throw new UnsupportedOperationException();		
1227
	}
1228
	
1229
	public boolean hasNext() {
1230
		if (entity.getChildrenCount() == 0 || entity.getChildrenCount() <= this.lastIndex){
1231
			return false;
1232
		}
1233
		int num = this.entity.getChildrenCount();
1234
		XMLEntity child;
1235
		for (int i=this.lastIndex+1;i < num; i++) {
1236
			child = this.entity.getChild(i);
1237
			if (child.contains(key) && child.getStringProperty(key).equals(value)) {
1238
				this.lastHasNextIndex = i;
1239
				return true;
1240
			}
1241
		}		
1242
		return false;
1243
	}
1244
	
1245
	public Object next() {
1246
		if (entity.getChildrenCount() == 0 || entity.getChildrenCount() <= this.lastIndex){
1247
			throw new NoSuchElementException();
1248
		}
1249
		
1250
		XMLEntity child;
1251
		
1252
		if (this.lastHasNextIndex > -1) {
1253
			if (this.entity.getChildrenCount() > this.lastHasNextIndex) {
1254
				child = this.entity.getChild(this.lastHasNextIndex);
1255
				if (child.contains(key) && child.getStringProperty(key).equals(value)) {
1256
					this.lastIndex = this.lastHasNextIndex;
1257
					this.lastHasNextIndex = -1;
1258
					return child;
1259
				}
1260
			}
1261
		}
1262
			
1263
		
1264
		int num = this.entity.getChildrenCount();
1265
		
1266
		for (int i=this.lastIndex+1;i < num; i++) {
1267
			child = this.entity.getChild(i);
1268
			if (child.contains(key) && child.getStringProperty(key).equals(value)) {
1269
				this.lastIndex = i;
1270
				this.lastHasNextIndex = -1;
1271
				return child;
1272
			}
1273
		}
1274
		this.lastHasNextIndex = -1;
1275
		throw new NoSuchElementException();
1276
	}
1277
}

Also available in: Unified diff