KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > framework > protocol > v11 > ClientModificationsRequirements


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package sync4j.framework.protocol.v11;
19
20 import sync4j.framework.core.*;
21
22 import sync4j.framework.protocol.ProtocolException;
23 import sync4j.framework.protocol.v11.BasicRequirements;
24 import sync4j.framework.protocol.v11.Errors;
25
26 /**
27  * This class groups utility methods used for checking that a client modifications
28  * massage follows the requirements that the protocol mandates.
29  *
30  * @author Stefano Fornari @ Funambol
31  * @version $Id: ClientModificationsRequirements.java,v 1.6 2005/03/02 20:57:38 harrie Exp $
32  */

33 public class ClientModificationsRequirements
34 extends BasicRequirements
35 implements Errors {
36     
37     // --------------------------------------------------------------- Constants
38

39     // ---------------------------------------------------------- Public methods
40

41     /**
42      * Checks if the given command contains a valid request for server capablities.
43      *
44      * @param cmd the command containing the request
45      *
46      * @throws ProtocolException
47      */

48     static public void checkSync(Sync cmd) throws ProtocolException {
49         //
50
// Checks command id
51
//
52
try {
53             checkCommandId(cmd.getCmdID());
54         } catch (ProtocolException e) {
55             String JavaDoc[] args = new String JavaDoc[] { e.getMessage() };
56             throw new ProtocolException(ERRMSG_INVALID_SYNC_COMMAND, args);
57         }
58         
59         //
60
// Checks modifications
61
//
62
AbstractCommand[] modifications =
63            (AbstractCommand[])cmd.getCommands().toArray(new AbstractCommand[0]);
64         
65         for (int i=0; ((modifications != null) && (i<modifications.length)); ++i) {
66             checkModification((ItemizedCommand)modifications[i]);
67         } // next i
68
}
69     
70     /**
71      * Checks the requirements for a modification command.
72      *
73      * @param cmd the modification command
74      *
75      * @throws ProtocolException
76      */

77     static public void checkModification(ItemizedCommand cmd)
78     throws ProtocolException {
79         //
80
// Checks command id
81
//
82
try {
83             checkCommandId(cmd.getCmdID());
84         
85         
86             Item[] items = (Item[])cmd.getItems().toArray(new Item[0]);
87
88             //
89
// The type of each single item can be specified at command level
90
// or at item level. If the type is specified at command level, items
91
// without type will take the command type as default. If the type
92
// is not specified at command level, each single item MUST specify
93
// its type.
94
//
95
Meta meta = cmd.getMeta();
96             
97             boolean checkType = false;
98             if (meta.getType() != null) {
99                 checkType = true;
100             }
101
102             for(int i=0; ((items != null) && (i<items.length)); ++i) {
103                 //
104
// NOTE: all commands but delete use <Data> to carry data about
105
// the modification
106
//
107
checkModificationItem(items[i], checkType, !cmd.getName().equals("Delete"));
108             }
109             
110         } catch (ProtocolException e) {
111             String JavaDoc[] args = new String JavaDoc[] { e.getMessage() };
112             throw new ProtocolException(ERRMSG_INVALID_MODIFICATION_COMMAND, args);
113         }
114     }
115     
116     /**
117      * Checks an item included into a modification command
118      *
119      * @param item the item to be checked
120      * @param checkType indicats if the type specified in the <Meta> tag is
121      * mandatory and must be checked
122      * @param checkData indicates when check for the existance of the <Data> tag
123      *
124      * @throws ProtocolException
125      */

126     static public void checkModificationItem(final Item item ,
127                                              final boolean checkType,
128                                              final boolean checkData)
129     throws ProtocolException {
130         checkSource(item.getSource());
131         
132         if (checkType) {
133             Meta meta = item.getMeta();
134             if (meta.getType() == null) {
135                String JavaDoc[] args = new String JavaDoc[] { item.toString() };
136                throw new ProtocolException(ERRMSG_MISSING_TYPE, args);
137             }
138         }
139         
140         if (checkData) {
141             if (item.getData() == null) {
142                 String JavaDoc[] args = new String JavaDoc[] { item.toString() };
143                 throw new ProtocolException(ERRMSG_MISSING_DATA, args);
144             }
145         }
146     }
147         
148 }
Popular Tags