|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.modules.tasklist.usertasks.ics; import java.io.Reader; import java.io.Writer; import java.util.ArrayList; import java.util.List; import org.netbeans.modules.tasklist.usertasks.ics.parser.*; /** * Reads ics files */ public class ICalReader { /** * A parameter in a content line */ public static class Param { /** name of the parameter */ public String name; /** parameter values */ public String[] values; } /** * A property in a content line */ public static class Property { /** name of a property */ public String name; /** parameters */ public Param[] params; /** value */ public String value; } private static final int CONTEXT_FILE = 0; private static final int CONTEXT_VCALENDAR = 1; private static final int CONTEXT_VEVENT = 2; private int context = CONTEXT_FILE; private boolean calendarPropsSection = false; private ICalendar calendar; private ICalEvent event; /** * Creates a new instance of ICalReader */ public ICalReader() { } /** * Read the content from the specified Reader * * @param r input * @return read content */ public ICalFile read(Reader r) throws ICalParseException { try { Node n = new IcsParser(r).File(); if (n.jjtGetNumChildren() == 0) throw new ICalParseException("VCALENDAR section expected"); // for each content line for (int i = 0; i < n.jjtGetNumChildren(); i++) { SimpleNode lineNode = (SimpleNode) n.jjtGetChild(i); String name = lineNode.firstToken.image; String value = lineNode.lastToken.image; List params = new ArrayList(); // for each parameter for (int j = 0; j < lineNode.jjtGetNumChildren(); j++) { SimpleNode paramNode = (SimpleNode) lineNode.jjtGetChild(j); Param p = new Param(); p.name = paramNode.firstToken.image; List values = new ArrayList(); // for each parameter value for (int k = 0; k < paramNode.jjtGetNumChildren(); k++) { SimpleNode valueNode = (SimpleNode) paramNode.jjtGetChild(k); values.add(valueNode.firstToken.image); } p.values = (String[]) values.toArray(new String[values.size()]); params.add(p); } Param[] parameters = (Param[]) params.toArray(new Param[params.size()]); Property p = new Property(); p.name = name; p.value = value; p.params = parameters; handleContentLine(p); } } catch (ParseException e) { throw new ICalParseException(e.getMessage()); } return null; // TODO } /** * Handles one line in an ics file * * @param p a content line */ private void handleContentLine(Property p) throws ICalParseException { switch (context) { case CONTEXT_FILE: if (isBegin(p, "VCALENDAR")) { calendar = new ICalendar(); context = CONTEXT_VCALENDAR; calendarPropsSection = true; } else { throw new ICalParseException("Unexpected line: " + p.name); } break; case CONTEXT_VCALENDAR: if (isEnd(p, "VCALENDAR")) { calendar = null; context = CONTEXT_FILE; } else if (p.name.equals("END")) { throw new ICalParseException("END:VCALENDAR expected"); } else if (p.name.equals("PRODID")) { if (!calendarPropsSection) throw new ICalParseException("Unexpected line:" + p.name); if (calendar.prodid != null) throw new ICalParseException("Duplicate PRODID"); calendar.prodid = p; } else if (p.name.equals("VERSION")) { if (!calendarPropsSection) throw new ICalParseException("Unexpected line:" + p.name); if (calendar.version != null) throw new ICalParseException("Duplicate VERSION"); calendar.version = p; } else if (p.name.equals("CALSCALE")) { if (!calendarPropsSection) throw new ICalParseException("Unexpected line:" + p.name); if (calendar.scale != null) throw new ICalParseException("Duplicate CALSCALE"); calendar.scale = p; } else if (p.name.equals("METHOD")) { if (!calendarPropsSection) throw new ICalParseException("Unexpected line:" + p.name); if (calendar.method != null) throw new ICalParseException("Duplicate METHOD"); calendar.method = p; } else if (isBegin(p, "VEVENT")) { event = new ICalEvent(); calendar.components.add(event); context = CONTEXT_VEVENT; calendarPropsSection = false; } else if (isBegin(p, "dddddd")) { calendar.props.add(p); } else { throw new ICalParseException("Unexpected line: " + p.name); } break; case CONTEXT_VEVENT: if (isEnd(p, "VEVENT")) { event = null; context = CONTEXT_VCALENDAR; } else if (p.name.equals("CLASS")) { event.class_ = p; } break; default: throw new InternalError("Unexpected context"); } } /** * Test for BEGIN:SOMETHING * * @param p a content line * @param n expected value (name of the section) */ private static boolean isBegin(Property p, String n) { return p.name.equals("BEGIN") && p.params.length == 0 && p.value.equals(n); } /** * Test for END:SOMETHING * * @param p a content line * @param n expected value (name of the section) */ private static boolean isEnd(Property p, String n) { return p.name.equals("END") && p.params.length == 0 && p.value.equals(n); } public void write(Writer w) { // TODO } } |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.