package addressbook;
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*
* $Id: Group.java 511428 2007-02-25 03:30:52Z vgritsenko $
*/
import java.util.Vector;
import org.apache.xpath.XPathAPI;
import org.apache.xpath.objects.XObject;
import org.w3c.dom.Node;
import org.xmldb.api.base.ResourceIterator;
import org.xmldb.api.modules.XMLResource;
/**
* Class for holding groups of the Person class
*/
public class Group {
private Vector personList = new Vector();
private int index = 0;
/**
* Returns current index
*/
public int getIndex() {
return index - 1;
}
/**
* Return the next person from the list
*/
public Person getNext() {
Person person = (Person) personList.get(index);
index = index + 1;
return person;
}
/**
* Return the person element at a certain index
*/
public Person getPersonAt(int i) {
return (Person) personList.get(i);
}
/**
* Add person to Group
*/
public void add(Person person) {
personList.add(person);
}
/**
* Add Persons from Result set
*/
public void addResults(ResourceIterator results) {
try {
while (results.hasMoreResources()) {
// Create a new person instance to add to Group
Person person = new Person();
// Retrieve the next node
XMLResource resource = (XMLResource) results.nextResource();
// Cast to Dom Node
Node originalnode = resource.getContentAsDOM();
// Get the document key for this node for later use
String dockey = resource.getDocumentId();
person.setDocKey(dockey);
// Use Xalan xpath parser to extract data, store into hastable
XObject xo = new XObject();
xo = XPathAPI.eval(originalnode, "/person/fname");
person.setFname(xo.toString());
xo = XPathAPI.eval(originalnode, "/person/lname");
person.setLname(xo.toString());
xo = XPathAPI.eval(originalnode, "/person/phone[@type='work']");
person.setWorkPhone(xo.toString());
xo = XPathAPI.eval(originalnode, "/person/phone[@type='home']");
person.setHomePhone(xo.toString());
xo = XPathAPI.eval(originalnode, "/person/phone[@type='cell']");
person.setCellPhone(xo.toString());
xo = XPathAPI.eval(originalnode, "/person/email[@type='home']");
person.setHomeEmail(xo.toString());
xo = XPathAPI.eval(originalnode, "/person/email[@type='work']");
person.setWorkEmail(xo.toString());
xo = XPathAPI.eval(originalnode, "/person/address[@type='home']");
person.setHomeAddress(xo.toString());
xo = XPathAPI.eval(originalnode, "/person/address[@type='work']");
person.setWorkAddress(xo.toString());
// Add person to group
this.add(person);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* Remove Person from Group
*/
public void remove(int i) {
personList.remove(i);
}
/**
* Remove all Persons from Group
*/
public void removeAll() {
personList.removeAllElements();
}
/**
* Return the size of the Group
*/
public int sizeOf() {
return personList.size();
}
/**
* Reset counter for Person retrieval
*/
public void reset() {
index = 0;
}
/**
* Return true if more persons exist
*/
public boolean hasMorePersons() {
return index < personList.size();
}
}