KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > extend > ScriptBufferUtil


1 /*
2  * Copyright 2005 Joe Walker
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.directwebremoting.extend;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.directwebremoting.ScriptBuffer;
23 import org.directwebremoting.ScriptBuffer.StringWrapper;
24
25 /**
26  * A simple utility class to extract a {@link String} from a {@link ScriptBuffer}.
27  * @author Joe Walker [joe at getahead dot ltd dot uk]
28  */

29 public class ScriptBufferUtil
30 {
31     /**
32      * Ensure we can't be created
33      */

34     private ScriptBufferUtil()
35     {
36     }
37
38     /**
39      * Return a string ready for output.
40      * @param buffer The source of the script data
41      * @param converterManager How we convert script variable to Javascript
42      * @return Some Javascript to be eval()ed by a browser.
43      * @throws MarshallException If an error happens during parameter marshalling
44      */

45     public static String JavaDoc createOutput(ScriptBuffer buffer, ConverterManager converterManager) throws MarshallException
46     {
47         OutboundContext context = new OutboundContext();
48         List JavaDoc ovs = new ArrayList JavaDoc();
49
50         // First convert everything
51
for (Iterator JavaDoc it = buffer.getParts().iterator(); it.hasNext();)
52         {
53             Object JavaDoc element = it.next();
54             if (!(element instanceof StringWrapper))
55             {
56                 OutboundVariable ov = converterManager.convertOutbound(element, context);
57                 ovs.add(ov);
58             }
59             else
60             {
61                 ovs.add(element);
62             }
63         }
64
65         StringBuffer JavaDoc output = new StringBuffer JavaDoc();
66
67         // First we look for the declaration code
68
for (Iterator JavaDoc it = ovs.iterator(); it.hasNext();)
69         {
70             Object JavaDoc element = it.next();
71             if (element instanceof OutboundVariable)
72             {
73                 OutboundVariable ov = (OutboundVariable) element;
74                 output.append(ov.getDeclareCode());
75             }
76         }
77
78         // Then we look for the construction code
79
for (Iterator JavaDoc it = ovs.iterator(); it.hasNext();)
80         {
81             Object JavaDoc element = it.next();
82             if (element instanceof OutboundVariable)
83             {
84                 OutboundVariable ov = (OutboundVariable) element;
85                 output.append(ov.getBuildCode());
86             }
87         }
88
89         // Then we output everything else
90
for (Iterator JavaDoc it = ovs.iterator(); it.hasNext();)
91         {
92             Object JavaDoc element = it.next();
93             if (element instanceof StringWrapper)
94             {
95                 StringWrapper str = (StringWrapper) element;
96                 output.append(str.toString());
97             }
98             else
99             {
100                 OutboundVariable ov = (OutboundVariable) element;
101                 output.append(ov.getAssignCode());
102             }
103         }
104
105         String JavaDoc exported = output.toString();
106         return exported;
107     }
108 }
109
Popular Tags