KickJava   Java API By Example, From Geeks To Geeks.

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


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: SyncModificationsRequirements.java,v 1.3 2005/03/02 20:57:38 harrie Exp $
32  */

33 public class SyncModificationsRequirements extends BasicRequirements implements Errors {
34     
35     // --------------------------------------------------------------- Constants
36

37     // ---------------------------------------------------------- Public methods
38

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

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

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

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