1 /***
2 * Created on Aug 5, 2005, Copyright UC Regents
3 */
4 package wise2.converter.converters;
5
6 import java.beans.BeanInfo;
7 import java.io.File;
8 import java.io.FileInputStream;
9 import java.io.InputStream;
10 import java.net.URI;
11 import java.net.URISyntaxException;
12 import java.net.URL;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Set;
17 import java.util.Map.Entry;
18
19 import net.sf.sail.core.entity.Rim;
20
21 import org.concord.datagraph.state.OTDataDrawingToolView;
22 import org.concord.framework.otrunk.OTObjectList;
23 import org.concord.framework.otrunk.OTrunk;
24 import org.concord.graph.util.state.OTDrawingTool;
25 import org.concord.graph.util.state.OTDrawingTool2;
26 import org.concord.otrunk.OTSystem;
27 import org.concord.otrunk.OTrunkImpl;
28 import org.concord.otrunk.datamodel.OTDataObject;
29 import org.concord.otrunk.datamodel.OTDatabase;
30 import org.concord.otrunk.view.OTViewEntry;
31 import org.concord.otrunk.view.OTViewService;
32 import org.concord.otrunk.view.document.OTCompoundDoc;
33 import org.concord.otrunk.view.document.OTDocumentView;
34 import org.concord.otrunk.xml.Exporter;
35 import org.concord.otrunk.xml.XMLBlobResource;
36 import org.concord.otrunk.xml.XMLDatabase;
37 import org.dom4j.Node;
38 import org.telscenter.pas.beans.PasStep;
39 import org.telscenter.pas.otrunk.BeanIcons;
40 import org.telscenter.pas.otrunk.OTrunkStep;
41 import org.telscenter.pas.steps.icons.PasStepIconProvider;
42
43 import wise2.converter.AbstractStepConverter;
44 import wise2.converter.util.HTMLUtils;
45
46 /***
47 * @author turadg
48 */
49 public class Wisedraw2Converter extends AbstractStepConverter {
50
51 private static final String CONVERSION_UNSPECIFIED = "CONVERSION_UNSPECIFIED";
52
53 @Override
54 public PasStep getTypedPasStep() {
55 OTrunkStep step = new OTrunkStep();
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99 OTDatabase otDb = new XMLDatabase();
100
101 try {
102 OTrunk otrunk = new OTrunkImpl(otDb);
103 OTSystem system = (OTSystem)otrunk.createObject(OTSystem.class);
104 otrunk.setRoot(system);
105
106 OTObjectList services = system.getServices();
107 OTViewService viewService =
108 (OTViewService)otrunk.createObject(OTViewService.class);
109 services.add(viewService);
110
111 OTViewEntry viewEntry;
112
113 viewEntry = (OTViewEntry)otrunk.createObject(OTViewEntry.class);
114 viewEntry.setObjectClass(OTDrawingTool.class.getName());
115 viewEntry.setViewClass(OTDataDrawingToolView.class.getName());
116 viewService.getViewEntries().add(viewEntry);
117
118
119 viewEntry = (OTViewEntry)otrunk.createObject(OTViewEntry.class);
120 viewEntry.setObjectClass(OTCompoundDoc.class.getName());
121 viewEntry.setViewClass(OTDocumentView.class.getName());
122 viewService.getViewEntries().add(viewEntry);
123
124 OTCompoundDoc compoundDoc =
125 (OTCompoundDoc)otrunk.createObject(OTCompoundDoc.class);
126 system.setRoot(compoundDoc);
127
128 OTDrawingTool drawingTool =
129 (OTDrawingTool)otrunk.createObject(OTDrawingTool2.class);
130
131 Map<String, URI> backgrounds = getNamedUris("backgrounds");
132
133
134 Set<Entry<String, URI>> entries = backgrounds.entrySet();
135
136 if(entries.size() > 0) {
137 Entry<String, URI> entry = entries.iterator().next();
138
139 URL backgroundURL = entry.getValue().toURL();
140
141
142 XMLBlobResource blobResource = new XMLBlobResource(backgroundURL);
143 OTDataObject drawingToolDO =
144 otDb.getOTDataObject(null, drawingTool.getGlobalId());
145 drawingToolDO.setResource("backgroundImage", blobResource);
146 }
147
148 String instructionText = getInstructionText();
149
150
151 instructionText =
152 instructionText.replaceFirst("(?s).*<body>(.*)</body>.*", "$1");
153
154 instructionText += "<object refid=\"" +
155 drawingTool.getGlobalId() + "\"/>";
156 compoundDoc.setBodyText(instructionText);
157
158
159
160 File outputFile = File.createTempFile("drawing", "otml");
161 Exporter.export(outputFile, otDb.getRoot(), otDb);
162
163 InputStream otmlInput = new FileInputStream(outputFile);
164 URL podURL = addToArchive("drawing.otml", otmlInput);
165
166 step.setAuthoredDataURL(podURL);
167
168
169 URL iconUrl;
170 BeanIcons icons = new BeanIcons();
171
172 iconUrl = PasStepIconProvider.getIconUrl("draw", BeanInfo.ICON_COLOR_16x16);
173 if(iconUrl != null){
174 InputStream iconInput = iconUrl.openStream();
175 URL podUrl = addToArchive(iconUrl.getFile(), iconInput);
176 icons.setColor16x16(podUrl);
177 }
178 iconUrl = PasStepIconProvider.getIconUrl("draw", BeanInfo.ICON_COLOR_32x32);
179 if(iconUrl != null){
180 InputStream iconInput = iconUrl.openStream();
181 URL podUrl = addToArchive(iconUrl.getFile(), iconInput);
182 icons.setColor32x32(podUrl);
183 }
184 iconUrl = PasStepIconProvider.getIconUrl("draw", BeanInfo.ICON_MONO_16x16);
185 if(iconUrl != null){
186 InputStream iconInput = iconUrl.openStream();
187 URL podUrl = addToArchive(iconUrl.getFile(), iconInput);
188 icons.setMono16x16(podUrl);
189 }
190 iconUrl = PasStepIconProvider.getIconUrl("draw", BeanInfo.ICON_MONO_32x32);
191 if(iconUrl != null){
192 InputStream iconInput = iconUrl.openStream();
193 URL podUrl = addToArchive(iconUrl.getFile(), iconInput);
194 icons.setMono32x32(podUrl);
195 }
196 } catch (Exception e){
197 e.printStackTrace();
198 }
199
200
201
202
203
204
205
206 Rim rim = new Rim();
207
208
209 rim.setShape(byte[].class);
210 rim.setName("otrunk_drawing");
211 step.setWorkRim(rim);
212 getPod().add(rim);
213
214 return step;
215 }
216
217 /***
218 * @return instruction text to use in this step
219 */
220 private String getInstructionText() {
221 Node htmlNode = stepNode.selectSingleNode("parameters/html");
222 String instructionText = (htmlNode == null) ? CONVERSION_UNSPECIFIED
223 : htmlNode.getText();
224
225 instructionText = HTMLUtils.tidyHtml(instructionText, "Wisedraw2" );
226
227 return instructionText;
228 }
229
230
231 private Map<String, URI> getNamedUris(String type) {
232 Map<String, URI> map = new HashMap<String, URI>();
233 String xpath = "parameters/" + type + "/*";
234
235
236 @SuppressWarnings("unchecked")
237 List<Node> backgroundNodes = stepNode.selectNodes(xpath);
238
239 for (Node node : backgroundNodes) {
240 String name = node.getName();
241 String uriStr = node.getText();
242 try {
243 URI uri = new URI(uriStr);
244 map.put(name, uri);
245 } catch (URISyntaxException e) {
246 e.printStackTrace();
247 }
248 }
249 return map;
250 }
251
252 }