KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > intro > impl > util > IntroModelSerializer


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.intro.impl.util;
12
13 import java.util.Enumeration JavaDoc;
14 import java.util.Hashtable JavaDoc;
15
16 import org.eclipse.ui.internal.intro.impl.model.AbstractIntroContainer;
17 import org.eclipse.ui.internal.intro.impl.model.AbstractIntroElement;
18 import org.eclipse.ui.internal.intro.impl.model.AbstractIntroPage;
19 import org.eclipse.ui.internal.intro.impl.model.IntroAnchor;
20 import org.eclipse.ui.internal.intro.impl.model.IntroContentProvider;
21 import org.eclipse.ui.internal.intro.impl.model.IntroGroup;
22 import org.eclipse.ui.internal.intro.impl.model.IntroHTML;
23 import org.eclipse.ui.internal.intro.impl.model.IntroHead;
24 import org.eclipse.ui.internal.intro.impl.model.IntroHomePage;
25 import org.eclipse.ui.internal.intro.impl.model.IntroImage;
26 import org.eclipse.ui.internal.intro.impl.model.IntroInclude;
27 import org.eclipse.ui.internal.intro.impl.model.IntroLink;
28 import org.eclipse.ui.internal.intro.impl.model.IntroModelRoot;
29 import org.eclipse.ui.internal.intro.impl.model.IntroPage;
30 import org.eclipse.ui.internal.intro.impl.model.IntroPageTitle;
31 import org.eclipse.ui.internal.intro.impl.model.IntroSeparator;
32 import org.eclipse.ui.internal.intro.impl.model.IntroText;
33 import org.osgi.framework.Bundle;
34
35 /**
36  * Print the model to a string buffer (only) for debugging.
37  */

38 public class IntroModelSerializer {
39
40     private StringBuffer JavaDoc buffer;
41     private boolean filter;
42
43     public IntroModelSerializer(IntroModelRoot root) {
44         this(root, false);
45     }
46     
47     public IntroModelSerializer(IntroModelRoot root, boolean filter) {
48         this.buffer = new StringBuffer JavaDoc();
49         this.filter = filter;
50         printModelRootInfo(root, buffer);
51
52         // Root Page
53
IntroHomePage rootPage = root.getHomePage();
54         printHomePage(rootPage, buffer);
55         printPageChildren(rootPage, buffer);
56
57         IntroPage[] pages = root.getPages();
58         printPages(pages, buffer);
59
60         buffer.append("\n\n"); //$NON-NLS-1$
61
printModelFlagTests(root, buffer);
62     }
63
64     /*
65      * Removes any platform/machine-specific paths from the given URL. This is
66      * used to remove absolute paths from the serialized model in order to compare
67      * results in automated tests.
68      */

69     private String JavaDoc filterURL(String JavaDoc url) {
70         if (filter) {
71             if (url != null && url.startsWith("file:/")) { //$NON-NLS-1$
72
return "file:/" + "<filtered>" + url.substring(url.lastIndexOf('/')); //$NON-NLS-1$ //$NON-NLS-2$
73
}
74         }
75         return url;
76     }
77     
78     private void printModelRootInfo(IntroModelRoot model, StringBuffer JavaDoc text) {
79         text.append("\nIntro Model Content:"); //$NON-NLS-1$
80
text.append("\n======================"); //$NON-NLS-1$
81
text.append("\n\nModel has valid config = " + model.hasValidConfig()); //$NON-NLS-1$
82
text.append("\nPresentation Kind = " //$NON-NLS-1$
83
+ model.getPresentation().getImplementationKind());
84         text.append("\nPresentation Shared Style = " //$NON-NLS-1$
85
+ model.getPresentation().getImplementationStyles());
86         text.append("\nPresentation type = " //$NON-NLS-1$
87
+ model.getPresentation().getType());
88         text.append("\nHome page id = " //$NON-NLS-1$
89
+ model.getPresentation().getHomePageId());
90         IntroHead headContent = model.getPresentation().getHead();
91         if (headContent != null)
92             text.append("\nPresentation Shared Head = " + headContent.getSrc()); //$NON-NLS-1$
93
text.append("\nNumber of pages (not including Root Page) = " //$NON-NLS-1$
94
+ model.getPages().length);
95         text.append("\nNumber of shared groups = " //$NON-NLS-1$
96
+ model.getChildrenOfType(AbstractIntroElement.GROUP).length);
97         text
98             .append("\nNumber of unresolved extensions = " //$NON-NLS-1$
99
+ model
100                         .getChildrenOfType(AbstractIntroElement.CONTAINER_EXTENSION).length);
101     }
102
103     /**
104      * @param text
105      * @param root
106      */

107     private void printHomePage(IntroHomePage rootPage, StringBuffer JavaDoc text) {
108         text.append("\n\nHOME PAGE: "); //$NON-NLS-1$
109
text.append("\n--------------"); //$NON-NLS-1$
110
text
111             .append("\n\tis dynamic= " + ((IntroModelRoot) rootPage.getParent()).isDynamic()); //$NON-NLS-1$
112

113         text.append("\n\tid = " + rootPage.getId()); //$NON-NLS-1$
114
text.append("\n\ttitle = " + rootPage.getTitle()); //$NON-NLS-1$
115
text.append("\n\tstyle = " + filterURL(rootPage.getStyle())); //$NON-NLS-1$
116
text.append("\n\talt-style = " + filterURL(rootPage.getAltStyle())); //$NON-NLS-1$
117
text.append("\n\turl = " + rootPage.getUrl()); //$NON-NLS-1$
118
text.append("\n\tstyle-id = " + rootPage.getStyleId()); //$NON-NLS-1$
119
printPageStyles(rootPage, text);
120     }
121
122     private void printPageStyles(AbstractIntroPage page, StringBuffer JavaDoc text) {
123         text.append("\n\tpage styles are = "); //$NON-NLS-1$
124
String JavaDoc[] styles = page.getStyles();
125         for (int i = 0; i < styles.length; i++)
126             text.append(filterURL(styles[i] + "\n\t\t\t")); //$NON-NLS-1$
127
text.append("\n\tpage alt-styles are = "); //$NON-NLS-1$
128

129         Hashtable JavaDoc altStylesHashtable = page.getAltStyles();
130         if (altStylesHashtable == null)
131             return;
132
133         Enumeration JavaDoc altStyles = altStylesHashtable.keys();
134         while (altStyles.hasMoreElements()) {
135             String JavaDoc altStyle = (String JavaDoc) altStyles.nextElement();
136
137             Bundle bundle = (Bundle) altStylesHashtable.get(altStyle);
138             text.append(filterURL(altStyle) + " from " + bundle.getSymbolicName()); //$NON-NLS-1$
139
text.append("\n\t\t"); //$NON-NLS-1$
140
}
141     }
142
143     private void printPageChildren(AbstractIntroPage page, StringBuffer JavaDoc text) {
144
145         text.append("\n\tpage children = " + page.getChildren().length); //$NON-NLS-1$
146
text.append("\n"); //$NON-NLS-1$
147
printContainerChildren(page, text, "\n\t\t"); //$NON-NLS-1$
148

149     }
150
151     private void printContainerChildren(AbstractIntroContainer container,
152             StringBuffer JavaDoc text, String JavaDoc indent) {
153
154         AbstractIntroElement[] children = container.getChildren();
155         for (int i = 0; i < children.length; i++) {
156             int childType = children[i].getType();
157             switch (childType) {
158             case AbstractIntroElement.ELEMENT:
159                 text.append("SHOULD NEVER BE HERE"); //$NON-NLS-1$
160
break;
161             case AbstractIntroElement.GROUP:
162                 printGroup(text, (IntroGroup) children[i], indent);
163                 break;
164             case AbstractIntroElement.LINK:
165                 printLink(text, (IntroLink) children[i], indent);
166                 break;
167             case AbstractIntroElement.TEXT:
168                 printText(text, (IntroText) children[i], indent);
169                 break;
170             case AbstractIntroElement.IMAGE:
171                 printImage(text, (IntroImage) children[i], indent);
172                 break;
173             case AbstractIntroElement.HR:
174                 printSeparator(text, (IntroSeparator) children[i], indent);
175                 break;
176             case AbstractIntroElement.HTML:
177                 printHtml(text, (IntroHTML) children[i], indent);
178                 break;
179             case AbstractIntroElement.INCLUDE:
180                 printInclude(text, (IntroInclude) children[i], indent);
181                 break;
182             case AbstractIntroElement.HEAD:
183                 printHead(text, (IntroHead) children[i], indent);
184                 break;
185             case AbstractIntroElement.PAGE_TITLE:
186                 printPageTitle(text, (IntroPageTitle) children[i], indent);
187                 break;
188             case AbstractIntroElement.ANCHOR:
189                 printAnchor(text, (IntroAnchor) children[i], indent);
190                 break;
191             case AbstractIntroElement.CONTENT_PROVIDER:
192                 printContentProvidor(text, (IntroContentProvider) children[i],
193                     indent);
194                 break;
195
196             }
197         }
198     }
199
200     private void printGroup(StringBuffer JavaDoc text, IntroGroup group, String JavaDoc indent) {
201         text.append(indent + "GROUP: id = " + group.getId()); //$NON-NLS-1$
202
indent = indent + "\t\t"; //$NON-NLS-1$
203
text.append(indent + "label = " + group.getLabel()); //$NON-NLS-1$
204
text.append(indent + "children = " + group.getChildren().length); //$NON-NLS-1$
205
text.append(indent + "style-id = " + group.getStyleId()); //$NON-NLS-1$
206
printContainerChildren(group, text, indent + "\t\t"); //$NON-NLS-1$
207
}
208
209     private void printLink(StringBuffer JavaDoc text, IntroLink link, String JavaDoc indent) {
210         text.append(indent + "LINK: id = " + link.getId()); //$NON-NLS-1$
211
indent = indent + "\t\t"; //$NON-NLS-1$
212
text.append(indent + "label = " + link.getLabel()); //$NON-NLS-1$
213
text.append(indent + "text = " + link.getText()); //$NON-NLS-1$
214
text.append(indent + "url = " + link.getUrl()); //$NON-NLS-1$
215
text.append(indent + "style-id = " + link.getStyleId()); //$NON-NLS-1$
216
}
217
218     private void printText(StringBuffer JavaDoc text, IntroText introText, String JavaDoc indent) {
219         text.append(indent + "TEXT: id = " + introText.getId()); //$NON-NLS-1$
220
indent = indent + "\t\t"; //$NON-NLS-1$
221
text.append(indent + "text = " + introText.getText()); //$NON-NLS-1$
222
text.append(indent + "style-id = " + introText.getStyleId()); //$NON-NLS-1$
223
}
224
225     private void printImage(StringBuffer JavaDoc text, IntroImage image, String JavaDoc indent) {
226         text.append(indent + "IMAGE: id = " + image.getId()); //$NON-NLS-1$
227
indent = indent + "\t\t"; //$NON-NLS-1$
228
text.append(indent + "src = " + image.getSrc()); //$NON-NLS-1$
229
text.append(indent + "alt = " + image.getAlt()); //$NON-NLS-1$
230
text.append(indent + "style-id = " + image.getStyleId()); //$NON-NLS-1$
231
}
232     
233     private void printSeparator(StringBuffer JavaDoc text, IntroSeparator sep, String JavaDoc indent) {
234         text.append(indent + "HR: id = " + sep.getId()); //$NON-NLS-1$
235
indent = indent + "\t\t"; //$NON-NLS-1$
236
text.append(indent + "style-id = " + sep.getStyleId()); //$NON-NLS-1$
237
}
238
239     private void printHtml(StringBuffer JavaDoc text, IntroHTML html, String JavaDoc indent) {
240         text.append(indent + "HTML: id = " + html.getId()); //$NON-NLS-1$
241
indent = indent + "\t\t"; //$NON-NLS-1$
242
text.append(indent + "src = " + html.getSrc()); //$NON-NLS-1$
243
text.append(indent + "isInlined = " + html.isInlined()); //$NON-NLS-1$
244
text.append(indent + "style-id = " + html.getStyleId()); //$NON-NLS-1$
245
if (html.getIntroImage() != null)
246             printImage(text, html.getIntroImage(), indent + "\t\t"); //$NON-NLS-1$
247
if (html.getIntroText() != null)
248             printText(text, html.getIntroText(), indent + "\t\t"); //$NON-NLS-1$
249

250     }
251
252     private void printInclude(StringBuffer JavaDoc text, IntroInclude include,
253             String JavaDoc indent) {
254         text.append(indent + "INCLUDE: configId = " + include.getConfigId()); //$NON-NLS-1$
255
indent = indent + "\t\t"; //$NON-NLS-1$
256
text.append(indent + "path = " + include.getPath()); //$NON-NLS-1$
257
text.append(indent + "merge-style = " + include.getMergeStyle()); //$NON-NLS-1$
258
}
259
260     private void printHead(StringBuffer JavaDoc text, IntroHead head, String JavaDoc indent) {
261         text.append(indent + "HEAD: src = " + head.getSrc()); //$NON-NLS-1$
262
}
263
264     private void printPageTitle(StringBuffer JavaDoc text, IntroPageTitle title,
265             String JavaDoc indent) {
266         text.append(indent + "TITLE: id = " + title.getId()); //$NON-NLS-1$
267
indent = indent + "\t\t"; //$NON-NLS-1$
268
text.append(indent + "title = " + title.getTitle()); //$NON-NLS-1$
269
text.append(indent + "style-id = " + title.getStyleId()); //$NON-NLS-1$
270
}
271
272     private void printAnchor(StringBuffer JavaDoc text, IntroAnchor anchor,
273             String JavaDoc indent) {
274         text.append(indent + "ANCHOR: id = " + anchor.getId()); //$NON-NLS-1$
275
}
276
277     private void printContentProvidor(StringBuffer JavaDoc text,
278             IntroContentProvider provider, String JavaDoc indent) {
279         text.append(indent + "CONTENT PROVIDER: id = " + provider.getId()); //$NON-NLS-1$
280
indent = indent + "\t\t"; //$NON-NLS-1$
281
text.append(indent + "class = " + provider.getClassName()); //$NON-NLS-1$
282
text.append(indent + "pluginId = " + provider.getPluginId()); //$NON-NLS-1$
283
if (provider.getIntroText() != null)
284             printText(text, provider.getIntroText(), indent + "\t\t"); //$NON-NLS-1$
285

286     }
287
288     /**
289      * Appends a given page's categories to the Text buffer.
290      *
291      * @param text
292      */

293     private void printPages(IntroPage[] pages, StringBuffer JavaDoc text) {
294         for (int i = 0; i < pages.length; i++) {
295             text.append("\n\nPAGE id = " + pages[i].getId()); //$NON-NLS-1$
296
text.append("\n----------"); //$NON-NLS-1$
297
text.append("\n\ttitle = " + pages[i].getTitle()); //$NON-NLS-1$
298
text.append("\n\tstyle = " + filterURL(pages[i].getStyle())); //$NON-NLS-1$
299
text.append("\n\talt-style = " + filterURL(pages[i].getAltStyle())); //$NON-NLS-1$
300
text.append("\n\tstyle-id = " + pages[i].getStyleId()); //$NON-NLS-1$
301
printPageStyles(pages[i], text);
302             printPageChildren(pages[i], text);
303         }
304     }
305
306     private void printModelFlagTests(IntroModelRoot model, StringBuffer JavaDoc text) {
307         text.append("Model Flag Tests: "); //$NON-NLS-1$
308
text.append("\n----------------"); //$NON-NLS-1$
309
if (model.getPages().length == 0) {
310             text.append("\nNo first page in model\n\n"); //$NON-NLS-1$
311
return;
312         }
313         IntroPage firstPage = model.getPages()[0];
314         text.append("\n\t\tFirst page children are: "); //$NON-NLS-1$
315
text
316             .append("\n\t\t\tGroups: " //$NON-NLS-1$
317
+ firstPage.getChildrenOfType(AbstractIntroElement.GROUP).length);
318         text
319             .append("\n\t\t\tLinks: " //$NON-NLS-1$
320
+ firstPage.getChildrenOfType(AbstractIntroElement.LINK).length);
321         text
322             .append("\n\t\t\tTexts: " //$NON-NLS-1$
323
+ firstPage.getChildrenOfType(AbstractIntroElement.TEXT).length);
324         text
325             .append("\n\t\t\tHTMLs: " //$NON-NLS-1$
326
+ firstPage.getChildrenOfType(AbstractIntroElement.HTML).length);
327         text
328             .append("\n\t\t\tImages: " //$NON-NLS-1$
329
+ firstPage.getChildrenOfType(AbstractIntroElement.IMAGE).length);
330         text
331             .append("\n\t\t\tIncludes: " //$NON-NLS-1$
332
+ firstPage.getChildrenOfType(AbstractIntroElement.INCLUDE).length);
333         text
334             .append("\n\t\t\tPage Titles: " //$NON-NLS-1$
335
+ firstPage
336                         .getChildrenOfType(AbstractIntroElement.PAGE_TITLE).length);
337         text
338             .append("\n\t\t\tPage Heads: " //$NON-NLS-1$
339
+ firstPage.getChildrenOfType(AbstractIntroElement.HEAD).length);
340         text
341             .append("\n\t\t\tModel Elements: " //$NON-NLS-1$
342
+ firstPage.getChildrenOfType(AbstractIntroElement.ELEMENT).length);
343         text
344             .append("\n\t\t\tContainers: " //$NON-NLS-1$
345
+ firstPage
346                         .getChildrenOfType(AbstractIntroElement.ABSTRACT_CONTAINER).length);
347         text
348             .append("\n\t\t\tAll Pages: " //$NON-NLS-1$
349
+ firstPage
350                         .getChildrenOfType(AbstractIntroElement.ABSTRACT_PAGE).length);
351         text
352             .append("\n\t\t\tAnchors: " //$NON-NLS-1$
353
+ firstPage.getChildrenOfType(AbstractIntroElement.ANCHOR).length);
354         text
355             .append("\n\t\t\tContent providers: " //$NON-NLS-1$
356
+ firstPage
357                         .getChildrenOfType(AbstractIntroElement.CONTENT_PROVIDER).length);
358         text
359             .append("\n\t\t\tElements with Text child(AbstractTextElemets): " //$NON-NLS-1$
360
+ firstPage
361                         .getChildrenOfType(AbstractIntroElement.ABSTRACT_TEXT).length);
362
363         AbstractIntroElement[] linksAndGroups = (AbstractIntroElement[]) firstPage
364             .getChildrenOfType(AbstractIntroElement.GROUP
365                     | AbstractIntroElement.LINK);
366         text.append("\n\t\t\tGroups and Links: " + linksAndGroups.length); //$NON-NLS-1$
367
}
368
369     /**
370      * @return Returns the textUI.
371      */

372     public String JavaDoc toString() {
373         return buffer.toString();
374     }
375 }
376
Popular Tags