2010-07-29 12:17:46 -04:00
|
|
|
/*
|
|
|
|
* DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
|
|
|
|
* Copyright (C) 2010 Mickael Guessant
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
package davmail.exchange;
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.IOException;
|
2010-07-30 11:09:44 -04:00
|
|
|
import java.io.StringReader;
|
2010-07-29 12:17:46 -04:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class for VCalendar, VTimezone, VEvent.
|
|
|
|
*/
|
|
|
|
public class VObject {
|
|
|
|
/**
|
|
|
|
* VObject properties
|
|
|
|
*/
|
|
|
|
ArrayList<VProperty> properties;
|
|
|
|
/**
|
|
|
|
* Inner VObjects (e.g. VEVENT, VALARM, ...)
|
|
|
|
*/
|
2010-07-29 17:18:59 -04:00
|
|
|
ArrayList<VObject> vObjects;
|
2010-07-29 12:17:46 -04:00
|
|
|
/**
|
|
|
|
* Object base name (VCALENDAR, VEVENT, VCARD...).
|
|
|
|
*/
|
|
|
|
public String type;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create VObject with given type
|
|
|
|
*
|
|
|
|
* @param beginProperty first line property
|
|
|
|
* @param reader stream reader just after the BEGIN:TYPE line
|
|
|
|
* @throws IOException on error
|
|
|
|
*/
|
|
|
|
public VObject(VProperty beginProperty, BufferedReader reader) throws IOException {
|
|
|
|
if (!"BEGIN".equals(beginProperty.getKey())) {
|
|
|
|
throw new IOException("Invalid first line: " + beginProperty);
|
|
|
|
}
|
|
|
|
type = beginProperty.getValue();
|
|
|
|
String endLine = "END:" + type;
|
|
|
|
String line = reader.readLine();
|
2010-08-17 09:39:14 -04:00
|
|
|
while (line != null && !line.startsWith(endLine)) {
|
2010-07-29 12:17:46 -04:00
|
|
|
handleLine(line, reader);
|
|
|
|
line = reader.readLine();
|
|
|
|
}
|
|
|
|
if (line == null) {
|
|
|
|
throw new IOException("Unexpected end of stream");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create VObject from reader.
|
|
|
|
*
|
|
|
|
* @param reader stream reader just after the BEGIN:TYPE line
|
|
|
|
* @throws IOException on error
|
|
|
|
*/
|
|
|
|
public VObject(BufferedReader reader) throws IOException {
|
|
|
|
this(new VProperty(reader.readLine()), reader);
|
|
|
|
}
|
|
|
|
|
2010-07-30 11:09:44 -04:00
|
|
|
/**
|
|
|
|
* Create VCalendar object from string;
|
|
|
|
*
|
|
|
|
* @param itemBody item body
|
|
|
|
* @throws IOException on error
|
|
|
|
*/
|
|
|
|
public VObject(String itemBody) throws IOException {
|
|
|
|
this(new ICSBufferedReader(new StringReader(itemBody)));
|
|
|
|
}
|
|
|
|
|
2010-09-13 12:07:49 -04:00
|
|
|
public VObject() {
|
|
|
|
}
|
|
|
|
|
2010-07-30 11:09:44 -04:00
|
|
|
|
2010-07-29 12:17:46 -04:00
|
|
|
protected void handleLine(String line, BufferedReader reader) throws IOException {
|
2010-08-06 17:21:13 -04:00
|
|
|
// skip empty lines
|
|
|
|
if (line.length() > 0) {
|
|
|
|
VProperty property = new VProperty(line);
|
|
|
|
// inner object
|
|
|
|
if ("BEGIN".equals(property.getKey())) {
|
|
|
|
addVObject(new VObject(property, reader));
|
|
|
|
} else if (property.getKey() != null) {
|
|
|
|
addProperty(property);
|
|
|
|
}
|
2010-07-29 12:17:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-13 12:07:49 -04:00
|
|
|
public void addVObject(VObject vObject) {
|
2010-07-29 17:18:59 -04:00
|
|
|
if (vObjects == null) {
|
|
|
|
vObjects = new ArrayList<VObject>();
|
2010-07-29 12:17:46 -04:00
|
|
|
}
|
2010-07-29 17:18:59 -04:00
|
|
|
vObjects.add(vObject);
|
2010-07-29 12:17:46 -04:00
|
|
|
}
|
|
|
|
|
2010-09-13 12:07:49 -04:00
|
|
|
public void addProperty(VProperty property) {
|
2010-07-29 17:18:59 -04:00
|
|
|
if (property.getValue() != null) {
|
|
|
|
if (properties == null) {
|
|
|
|
properties = new ArrayList<VProperty>();
|
|
|
|
}
|
|
|
|
properties.add(property);
|
2010-07-29 12:17:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-29 17:18:59 -04:00
|
|
|
/**
|
|
|
|
* Write VObject to writer.
|
|
|
|
*
|
|
|
|
* @param writer buffered writer
|
|
|
|
*/
|
2010-07-29 12:17:46 -04:00
|
|
|
public void writeTo(ICSBufferedWriter writer) {
|
|
|
|
writer.write("BEGIN:");
|
|
|
|
writer.writeLine(type);
|
|
|
|
if (properties != null) {
|
|
|
|
for (VProperty property : properties) {
|
|
|
|
writer.writeLine(property.toString());
|
|
|
|
}
|
|
|
|
}
|
2010-07-29 17:18:59 -04:00
|
|
|
if (vObjects != null) {
|
|
|
|
for (VObject object : vObjects) {
|
2010-07-29 12:17:46 -04:00
|
|
|
object.writeTo(writer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writer.write("END:");
|
|
|
|
writer.writeLine(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String toString() {
|
|
|
|
ICSBufferedWriter writer = new ICSBufferedWriter();
|
|
|
|
writeTo(writer);
|
|
|
|
return writer.toString();
|
|
|
|
}
|
|
|
|
|
2010-07-29 17:18:59 -04:00
|
|
|
/**
|
|
|
|
* Get VObject properties
|
|
|
|
*
|
|
|
|
* @return properties
|
|
|
|
*/
|
2010-07-29 12:17:46 -04:00
|
|
|
public List<VProperty> getProperties() {
|
|
|
|
return properties;
|
|
|
|
}
|
2010-07-29 17:18:59 -04:00
|
|
|
|
|
|
|
public VProperty getProperty(String name) {
|
|
|
|
if (properties != null) {
|
|
|
|
for (VProperty property : properties) {
|
2010-08-06 17:21:13 -04:00
|
|
|
if (property.getKey() != null && property.getKey().equalsIgnoreCase(name)) {
|
2010-07-29 17:18:59 -04:00
|
|
|
return property;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2010-07-31 18:52:45 -04:00
|
|
|
public List<VProperty> getProperties(String name) {
|
|
|
|
List<VProperty> result = null;
|
|
|
|
if (properties != null) {
|
|
|
|
for (VProperty property : properties) {
|
2010-08-06 17:21:13 -04:00
|
|
|
if (property.getKey() != null && property.getKey().equalsIgnoreCase(name)) {
|
2010-07-31 18:52:45 -04:00
|
|
|
if (result == null) {
|
|
|
|
result = new ArrayList<VProperty>();
|
|
|
|
}
|
|
|
|
result.add(property);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-07-29 17:18:59 -04:00
|
|
|
public String getPropertyValue(String name) {
|
|
|
|
VProperty property = getProperty(name);
|
|
|
|
if (property != null) {
|
|
|
|
return property.getValue();
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPropertyValue(String name, String value) {
|
2010-07-30 11:09:44 -04:00
|
|
|
if (value == null) {
|
|
|
|
removeProperty(name);
|
2010-07-29 17:18:59 -04:00
|
|
|
} else {
|
2010-07-30 11:09:44 -04:00
|
|
|
VProperty property = getProperty(name);
|
|
|
|
if (property == null) {
|
|
|
|
property = new VProperty(name, value);
|
|
|
|
addProperty(property);
|
|
|
|
} else {
|
|
|
|
property.setValue(value);
|
|
|
|
}
|
2010-07-29 17:18:59 -04:00
|
|
|
}
|
2010-07-30 11:09:44 -04:00
|
|
|
}
|
2010-07-29 17:18:59 -04:00
|
|
|
|
2010-09-13 15:27:16 -04:00
|
|
|
public void addPropertyValue(String name, String value) {
|
|
|
|
if (value != null) {
|
|
|
|
VProperty property = new VProperty(name, value);
|
|
|
|
addProperty(property);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-30 11:09:44 -04:00
|
|
|
public void removeProperty(String name) {
|
2010-07-31 18:52:45 -04:00
|
|
|
if (properties != null) {
|
2010-07-30 11:09:44 -04:00
|
|
|
VProperty property = getProperty(name);
|
|
|
|
if (property != null) {
|
2010-07-31 18:52:45 -04:00
|
|
|
properties.remove(property);
|
2010-07-30 11:09:44 -04:00
|
|
|
}
|
|
|
|
}
|
2010-07-29 17:18:59 -04:00
|
|
|
}
|
2010-07-31 18:52:45 -04:00
|
|
|
|
|
|
|
public void removeProperty(VProperty property) {
|
|
|
|
if (properties != null) {
|
|
|
|
properties.remove(property);
|
|
|
|
}
|
|
|
|
}
|
2010-07-29 12:17:46 -04:00
|
|
|
}
|