KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > mail > imap > protocol > Namespaces


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 /*
23  * @(#)Namespaces.java 1.5 05/08/29
24  *
25  * Copyright 2000-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package com.sun.mail.imap.protocol;
29
30 import java.util.*;
31 import com.sun.mail.iap.*;
32
33 /**
34  * This class and its inner class represent the response to the
35  * NAMESPACE command.
36  *
37  * @see RFC2342
38  * @author Bill Shannon
39  */

40
41 public class Namespaces {
42
43     /**
44      * A single namespace entry.
45      */

46     public static class Namespace {
47     /**
48      * Prefix string for the namespace.
49      */

50     public String JavaDoc prefix;
51
52     /**
53      * Delimiter between names in this namespace.
54      */

55     public char delimiter;
56
57     /**
58      * Parse a namespace element out of the response.
59      */

60     public Namespace(Response r) throws ProtocolException {
61         // Namespace_Element = "(" string SP (<"> QUOTED_CHAR <"> / nil)
62
// *(Namespace_Response_Extension) ")"
63
if (r.readByte() != '(')
64         throw new ProtocolException(
65                     "Missing '(' at start of Namespace");
66         // first, the prefix
67
prefix = BASE64MailboxDecoder.decode(r.readString());
68         r.skipSpaces();
69         // delimiter is a quoted character or NIL
70
if (r.peekByte() == '"') {
71         r.readByte();
72         delimiter = (char)r.readByte();
73         if (delimiter == '\\')
74             delimiter = (char)r.readByte();
75         if (r.readByte() != '"')
76             throw new ProtocolException(
77                     "Missing '\"' at end of QUOTED_CHAR");
78         } else {
79         String JavaDoc s = r.readAtom();
80         if (s == null)
81             throw new ProtocolException("Expected NIL, got null");
82         if (!s.equalsIgnoreCase("NIL"))
83             throw new ProtocolException("Expected NIL, got " + s);
84         delimiter = 0;
85         }
86         // at end of Namespace data?
87
if (r.peekByte() != ')') {
88         // otherwise, must be a Namespace_Response_Extension
89
// Namespace_Response_Extension = SP string SP
90
// "(" string *(SP string) ")"
91
r.skipSpaces();
92         r.readString();
93         r.skipSpaces();
94         r.readStringList();
95         }
96         if (r.readByte() != ')')
97         throw new ProtocolException("Missing ')' at end of Namespace");
98     }
99     };
100
101     /**
102      * The personal namespaces.
103      * May be null.
104      */

105     public Namespace[] personal;
106
107     /**
108      * The namespaces for other users.
109      * May be null.
110      */

111     public Namespace[] otherUsers;
112
113     /**
114      * The shared namespace.
115      * May be null.
116      */

117     public Namespace[] shared;
118
119     /**
120      * Parse out all the namespaces.
121      */

122     public Namespaces(Response r) throws ProtocolException {
123     personal = getNamespaces(r);
124     otherUsers = getNamespaces(r);
125     shared = getNamespaces(r);
126     }
127
128     /**
129      * Parse out one of the three sets of namespaces.
130      */

131     private Namespace[] getNamespaces(Response r) throws ProtocolException {
132     r.skipSpaces();
133     // Namespace = nil / "(" 1*( Namespace_Element) ")"
134
if (r.peekByte() == '(') {
135         Vector v = new Vector();
136         r.readByte();
137         do {
138         Namespace ns = new Namespace(r);
139         v.addElement(ns);
140         } while (r.peekByte() != ')');
141         r.readByte();
142         Namespace[] nsa = new Namespace[v.size()];
143         v.copyInto(nsa);
144         return nsa;
145     } else {
146         String JavaDoc s = r.readAtom();
147         if (s == null)
148         throw new ProtocolException("Expected NIL, got null");
149         if (!s.equalsIgnoreCase("NIL"))
150         throw new ProtocolException("Expected NIL, got " + s);
151         return null;
152     }
153     }
154 }
155
Popular Tags