1 /******************************************************************
2 JADE - Java Agent DEvelopment Framework is a framework to develop
3 multi-agent systems in compliance with the FIPA specifications.
4 Copyright (C) 2000 CSELT S.p.A.
5
6 GNU Lesser General Public License
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation,
11 version 2.1 of the License.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the
20 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.
22 *****************************************************************/
23
24 package sl;
25
26 import java.io.Writer;
27 import java.io.StringWriter;
28 import java.io.IOException;
29
30 import java.util.*;
31
32 /***
33
34 @author Giovanni Rimassa - Universita` di Parma
35 @version $Date: 2003/10/09 13:00:37 $ $Revision: 1.1.1.1 $
36 */
37 class SL0Encoder {
38
39 public String encode(Frame f) {
40 StringWriter out = new StringWriter();
41 try {
42 if (f.size()<=0)
43 out.write(f.getName());
44 else
45 writeFrame(f, out);
46 out.flush();
47 }
48 catch(IOException ioe) {
49 ioe.printStackTrace();
50 }
51 catch(OntologyException oe) {
52 oe.printStackTrace();
53 }
54 catch(CodecException ce) {
55 ce.printStackTrace();
56 }
57 return out.toString();
58 }
59
60
61 private void writeFrame(Frame f, Writer w) throws IOException, OntologyException, CodecException {
62 w.write("(" + f.getName() + " ");
63 for (int i=0; i<f.size(); i++ ) {
64 String slotName = f.getSlotName(i);
65 Object slotValue = f.getSlot(i);
66 if (!slotName.startsWith(Frame.UNNAMEDPREFIX)) {
67
68 if (!slotName.startsWith(":"))
69 w.write(":");
70
71 if (slotName.indexOf(" ")>-1)
72 w.write("\""+slotName+"\"");
73 else
74 w.write(slotName);
75 w.write(" ");
76 }
77 if (isFrame(slotValue))
78 writeFrame((Frame)slotValue, w);
79 else if (slotValue.getClass().equals(java.util.Date.class))
80
81
82
83 w.write(ISO8601.toString((java.util.Date)slotValue));
84 else if (slotValue.getClass().equals(java.lang.Byte[].class))
85 throw new CodecException("SL0 does not support bynary fields", null);
86 else {
87
88 String stringifiedValue = slotValue.toString();
89 if (mustBeQuoted(stringifiedValue))
90 w.write("\""+stringifiedValue+"\"");
91 else
92 w.write(stringifiedValue);
93 }
94 w.write(" ");
95 }
96 w.write(")");
97 }
98
99 private boolean mustBeQuoted(String s) {
100 s.trim();
101 if ((s.charAt(0) == '\"') && (s.charAt(s.length()-1) == '\"'))
102 return false;
103 else if (s.indexOf(" ") > -1 || s.equals("") || isAToken(s))
104 return true;
105 else
106 return false;
107 }
108
109 /***
110 If this is a token of the language, then it must be quoted.
111 **/
112 private boolean isAToken(String str) {
113 return str.equalsIgnoreCase(SL0Codec.NAME_OF_ACTION_FRAME);
114 }
115
116 private boolean isFrame(Object f) {
117 return (f.getClass()==Frame.class);
118 }
119 }