001: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
002: // Released under the terms of the GNU General Public License version 2 or later.
003: package fitnesse.html;
004:
005: import fitnesse.wiki.PageCrawlerImpl;
006: import fitnesse.wiki.PageData;
007: import fitnesse.wiki.PathParser;
008: import fitnesse.wiki.ProxyPage;
009: import fitnesse.wiki.WikiPage;
010: import fitnesse.wiki.WikiPagePath;
011:
012: public class HtmlUtil {
013: public static HtmlElement BR = new RawHtml("<br/>");
014:
015: public static HtmlElement HR = new RawHtml("<hr/>");
016:
017: public static HtmlElement NBSP = new RawHtml(" ");
018:
019: public static HtmlElement P = new RawHtml("<p>");
020:
021: public static final boolean NO_NEW_WINDOW = false;
022:
023: public static HtmlTag makeDivTag(String divClass) {
024: HtmlTag div = new HtmlTag("div");
025: div.addAttribute("class", divClass);
026: div.add("");
027: return div;
028: }
029:
030: public static void addTitles(HtmlPage page, String title) {
031: page.title.use(title);
032: HtmlTag span = new HtmlTag("span");
033: span.addAttribute("class", "page_title");
034: span.add(title);
035: page.header.use(span);
036: }
037:
038: public static HtmlTag makeBold(String content) {
039: HtmlTag bold = new HtmlTag("b");
040: bold.add(content);
041: return bold;
042: }
043:
044: public static HtmlTag makeItalic(String content) {
045: HtmlTag italic = new HtmlTag("i");
046: italic.add(content);
047: return italic;
048: }
049:
050: public static HtmlTag makeSpanTag(String spanClass, String content) {
051: HtmlTag span = new HtmlTag("span");
052: span.addAttribute("class", spanClass);
053: span.add(content);
054: return span;
055: }
056:
057: public static HtmlTag makeFormTag(String method, String action) {
058: HtmlTag formTag = new HtmlTag("form");
059: formTag.addAttribute("method", method);
060: formTag.addAttribute("action", action);
061: return formTag;
062: }
063:
064: public static HtmlTag makeAnchorTag(String name) {
065: HtmlTag anchorTag = new HtmlTag("a");
066: anchorTag.addAttribute("name", name);
067: return anchorTag;
068: }
069:
070: public static HtmlTag makeActionLink(String action, String name,
071: String inputName, String accessKey, boolean newWindow) {
072: return HtmlUtil.makeActionLink(action, name, inputName,
073: accessKey, newWindow, true);
074: }
075:
076: public static HtmlTag makeActionLink(String action, String name,
077: String inputName, String accessKey, boolean newWindow,
078: boolean addNameToLink) {
079: TagGroup group = new TagGroup();
080: String href = action;
081: if ((inputName != null) && (addNameToLink == true)) {
082: href = href + "?" + inputName;
083: }
084:
085: HtmlTag image = new HtmlTag("img");
086: String toolName = inputName;
087: if (inputName == null) {
088: toolName = name;
089: }
090:
091: image.addAttribute("src", "/files/images/toolbar/" + toolName
092: + ".gif");
093: image.addAttribute("title", name);
094: image.addAttribute("id", toolName + "Tool");
095: image.addAttribute("class", "toolbarImage");
096:
097: HtmlTag link = HtmlUtil.makeLink(href, image);
098:
099: if (newWindow) {
100: link.addAttribute("target", "newWindow");
101: }
102: link.addAttribute("accesskey", accessKey);
103: link.addAttribute("title", name);
104: link.addAttribute("id", inputName);
105: link.addAttribute("class", "toolbarLink");
106:
107: group.add(new HtmlComment(name + " button"));
108: group.add(link);
109: return group;
110: }
111:
112: public static HtmlTag makeInputTag(String type, String name,
113: String value) {
114: HtmlTag input = HtmlUtil.makeInputTag(type, name);
115: input.addAttribute("value", value);
116: return input;
117: }
118:
119: public static HtmlTag makeInputTag(String type, String name) {
120: HtmlTag input = new HtmlTag("input");
121: input.addAttribute("type", type);
122: input.addAttribute("name", name);
123: return input;
124: }
125:
126: public static HtmlTag makeRadioTag(String name, String value,
127: boolean checked) {
128: HtmlTag input = new HtmlTag("input");
129: input.addAttribute("type", "radio");
130: input.addAttribute("name", name);
131: input.addAttribute("value", value);
132: if (checked) {
133: input.addAttribute("checked", "checked");
134: }
135: return input;
136: }
137:
138: public static HtmlTag makeOptionTag(String value, String text) {
139: HtmlTag option = new HtmlTag("option");
140: option.addAttribute("value", value);
141: option.add(text);
142:
143: return option;
144: }
145:
146: public static HtmlTag makeLink(String href, String text) {
147: return HtmlUtil.makeLink(href, new RawHtml(text));
148: }
149:
150: public static HtmlTag makeLink(String href, HtmlElement content) {
151: HtmlTag link = new HtmlTag("a");
152: link.addAttribute("href", href);
153: link.add(content);
154: return link;
155: }
156:
157: public static HtmlTag makeSelectTag(String name) {
158: HtmlTag select = new HtmlTag("select");
159: select.addAttribute("name", name);
160: return select;
161: }
162:
163: public static TagGroup makeBreadCrumbsWithCurrentPageLinked(
164: String path) throws Exception {
165: return HtmlUtil.makeBreadCrumbsWithCurrentPageLinked(path, ".");
166: }
167:
168: public static HtmlTag makeBreadCrumbsWithCurrentPageNotLinked(
169: String trail) throws Exception {
170: return HtmlUtil.makeBreadCrumbsWithCurrentPageNotLinked(trail,
171: ".");
172: }
173:
174: public static TagGroup makeBreadCrumbsWithCurrentPageLinked(
175: String path, String separator) throws Exception {
176: TagGroup tagGroup = new TagGroup();
177: String[] crumbs = path.split("[" + separator + "]");
178: String trail = HtmlUtil.makeAllButLastCrumb(crumbs, separator,
179: tagGroup);
180: tagGroup.add(HtmlUtil.getLastCrumbAsLink(crumbs, trail));
181: return tagGroup;
182: }
183:
184: public static HtmlTag makeBreadCrumbsWithCurrentPageNotLinked(
185: String path, String separator) throws Exception {
186: TagGroup tagGroup = new TagGroup();
187: String[] crumbs = path.split("[" + separator + "]");
188: HtmlUtil.makeAllButLastCrumb(crumbs, separator, tagGroup);
189: tagGroup.add(HtmlUtil.getLastCrumbAsText(crumbs));
190: return tagGroup;
191: }
192:
193: private static HtmlTag getLastCrumbAsLink(String[] crumbs,
194: String trail) throws Exception {
195: String crumb = HtmlUtil.getLastCrumb(crumbs);
196: HtmlTag link = HtmlUtil.makeLink("/" + trail + crumb, crumb);
197: // link.head = HtmlUtil.BR.html();
198: link.addAttribute("class", "page_title");
199: return link;
200: }
201:
202: private static String getLastCrumb(String[] crumbs) {
203: String crumb = "";
204: if (crumbs.length > 0) {
205: crumb = crumbs[crumbs.length - 1];
206: }
207: return crumb;
208: }
209:
210: private static HtmlTag getLastCrumbAsText(String[] crumbs)
211: throws Exception {
212: String crumb = HtmlUtil.getLastCrumb(crumbs);
213: HtmlTag this Page = new HtmlTag("span", crumb);
214: this Page.addAttribute("class", "page_title");
215: // thisPage.head = HtmlUtil.BR.html();
216: return this Page;
217: }
218:
219: public static HtmlTag makeBreadCrumbsWithPageType(String trail,
220: String type) throws Exception {
221: return HtmlUtil.makeBreadCrumbsWithPageType(trail, ".", type);
222: }
223:
224: public static HtmlTag makeBreadCrumbsWithPageType(String trail,
225: String separator, String type) throws Exception {
226: TagGroup group = HtmlUtil.makeBreadCrumbsWithCurrentPageLinked(
227: trail, separator);
228: // group.add(HtmlUtil.BR);
229: group.add(HtmlUtil.makeSpanTag("page_type", type));
230: return group;
231: }
232:
233: private static String makeAllButLastCrumb(String[] crumbs,
234: String separator, TagGroup group) {
235: // Make the first character "." so we have a more accurate trail for !include in tests
236: String trail = ".";
237: HtmlTag link = HtmlUtil.makeLink("/", trail);
238: group.add(link);
239: for (int i = 0; i < crumbs.length - 1; i++) {
240: String crumb = crumbs[i];
241: link = HtmlUtil.makeLink("/" + trail + crumb, crumb);
242: link.tail = separator;
243: trail = trail + crumb + separator;
244: group.add(link);
245: }
246: return trail;
247: }
248:
249: public static HtmlTag makeActions(PageData pageData)
250: throws Exception {
251: WikiPage page = pageData.getWikiPage();
252:
253: WikiPagePath localPagePath = page.getPageCrawler().getFullPath(
254: page);
255: String localPageName = PathParser.render(localPagePath);
256: String localOrRemotePageName = localPageName;
257: boolean newWindowIfRemote = HtmlUtil.NO_NEW_WINDOW;
258: if (page instanceof ProxyPage) {
259: ProxyPage proxyPage = (ProxyPage) page;
260: localOrRemotePageName = proxyPage.getThisPageUrl();
261: newWindowIfRemote = true;
262: }
263: return HtmlUtil.makeActions(pageData, localPageName,
264: localOrRemotePageName, newWindowIfRemote);
265: }
266:
267: public static HtmlTag makeActions(PageData pageData,
268: String localPageName, String localOrRemotePageName,
269: boolean newWindowIfRemote) throws Exception {
270: TagGroup actions = new TagGroup();
271:
272: // Generally used only by FitNesse; we leave them first for FitNesse users
273: if (pageData.hasAttribute(WikiPage.ACTION_TEST)) {
274: actions.add(HtmlUtil.makeActionLink(localPageName,
275: WikiPage.ACTION_TEST, "test", "t",
276: HtmlUtil.NO_NEW_WINDOW));
277: }
278: if (pageData.hasAttribute(WikiPage.ACTION_SUITE)) {
279: actions.add(HtmlUtil.makeActionLink(localPageName,
280: WikiPage.ACTION_SUITE, "suite", "",
281: HtmlUtil.NO_NEW_WINDOW));
282: }
283:
284: // Generally used by STIQ and FitNesse
285: if (pageData.hasAttribute(WikiPage.ACTION_EDIT)) {
286: actions.add(makeActionLinkWithRemovalCall(
287: localOrRemotePageName, WikiPage.ACTION_EDIT,
288: "edit", "e", newWindowIfRemote));
289: }
290: if (pageData.hasAttribute(WikiPage.ACTION_PROPERTIES)) {
291: actions.add(makeActionLinkWithRemovalCall(
292: localOrRemotePageName, WikiPage.ACTION_PROPERTIES,
293: "properties", "p", newWindowIfRemote));
294: }
295: if (pageData.hasAttribute(WikiPage.ACTION_REFACTOR)) {
296: actions.add(makeActionLinkWithRemovalCall(
297: localOrRemotePageName, WikiPage.ACTION_REFACTOR,
298: "refactor", "", newWindowIfRemote));
299: }
300:
301: // Generally used only by FitNesse
302: if (pageData.hasAttribute(WikiPage.ACTION_SEARCH)) {
303: actions.add(HtmlUtil.makeActionLink("?searchForm",
304: WikiPage.ACTION_SEARCH, "search", "s",
305: HtmlUtil.NO_NEW_WINDOW, false));
306: }
307: if (pageData.hasAttribute(WikiPage.ACTION_WHERE_USED)) {
308: actions.add(HtmlUtil.makeActionLink(localOrRemotePageName,
309: WikiPage.ACTION_WHERE_USED, "whereUsed", "w",
310: HtmlUtil.NO_NEW_WINDOW));
311: }
312: if (pageData.hasAttribute(WikiPage.ACTION_RECENT_CHANGES)) {
313: actions.add(HtmlUtil.makeActionLink("/RecentChanges",
314: WikiPage.ACTION_RECENT_CHANGES, null, "",
315: HtmlUtil.NO_NEW_WINDOW));
316: }
317: if (pageData.hasAttribute(WikiPage.ACTION_FILES)) {
318: actions.add(HtmlUtil.makeActionLink("/files",
319: WikiPage.ACTION_FILES, null, "f",
320: HtmlUtil.NO_NEW_WINDOW));
321: }
322:
323: if (pageData.hasAttribute(WikiPage.ACTION_VERSIONS)) {
324: actions.add(HtmlUtil.makeActionLink(localOrRemotePageName,
325: WikiPage.ACTION_VERSIONS, "versions", "v",
326: newWindowIfRemote));
327: }
328:
329: // Only used by STIQ
330:
331: // Open the current page in a new browser window
332: actions.add(HtmlUtil.makeActionLink(localOrRemotePageName,
333: WikiPage.ACTION_OPEN_IN_WINDOW, null, "o", true));
334:
335: // Create a new STIQ Test page as a child of the current page
336: actions.add(makeActionLinkWithRemovalCall(
337: localOrRemotePageName, WikiPage.ACTION_NEW_PAGE,
338: "newStiqPage", "n", HtmlUtil.NO_NEW_WINDOW));
339:
340: // Reload the current page
341: actions.add(makeActionLinkWithRemovalCall(
342: localOrRemotePageName, WikiPage.ACTION_REFRESH,
343: "Refresh", "r", HtmlUtil.NO_NEW_WINDOW));
344:
345: // Navigate up to the Parent page
346: // WikiPage wPage = pageData.getWikiPage();
347: // PageCrawler crawler = wPage.getPageCrawler();
348: // WikiPagePath pagePath = crawler.getFullPath(wPage);
349: // String parent = PathParser.render(pagePath.parentPath());
350: // actions.add(HtmlUtil.makeActionLink(parent, "UpToParent", null, "u", HtmlUtil.NO_NEW_WINDOW));
351:
352: return actions;
353: }
354:
355: private static HtmlTag makeActionLinkWithRemovalCall(
356: String LORName, String pageName, String pageId,
357: String accessKey, boolean newWindow) {
358: TagGroup actionLink = (TagGroup) HtmlUtil.makeActionLink(
359: LORName, pageName, pageId, accessKey, newWindow);
360: String script = "top.removeTestCaseResultsFromSuite();return true;";
361: HtmlTag tag = (HtmlTag) (actionLink.childTags.get(1));
362: tag.addAttribute("onclick", script);
363: return tag;
364: }
365:
366: public static String makeNormalWikiPageContent(PageData pageData)
367: throws Exception {
368: String content = HtmlUtil.testableHtml(pageData);
369: return HtmlUtil.addHeaderAndFooter(pageData.getWikiPage(),
370: content);
371: }
372:
373: public static String addHeaderAndFooter(WikiPage page,
374: String content) throws Exception {
375: StringBuffer buffer = new StringBuffer();
376: buffer.append(HtmlUtil.getHtmlOfInheritedPage("PageHeader",
377: page));
378: buffer.append(content);
379: buffer.append(HtmlUtil.getHtmlOfInheritedPage("PageFooter",
380: page));
381: return buffer.toString();
382: }
383:
384: public static String testableHtml(PageData pageData)
385: throws Exception {
386: WikiPage wikiPage = pageData.getWikiPage();
387: StringBuffer buffer = new StringBuffer();
388: if (pageData.hasAttribute(WikiPage.ACTION_TEST)) {
389: WikiPage setup = PageCrawlerImpl.getInheritedPage("SetUp",
390: wikiPage);
391: if (setup != null) {
392: WikiPagePath setupPath = wikiPage.getPageCrawler()
393: .getFullPath(setup);
394: String setupPathName = PathParser.render(setupPath);
395: buffer.append("!include -setup .")
396: .append(setupPathName).append("\n");
397: }
398: }
399: buffer.append(pageData.getContent());
400: if (pageData.hasAttribute(WikiPage.ACTION_TEST)) {
401: WikiPage teardown = PageCrawlerImpl.getInheritedPage(
402: "TearDown", wikiPage);
403: if (teardown != null) {
404: WikiPagePath tearDownPath = wikiPage.getPageCrawler()
405: .getFullPath(teardown);
406: String tearDownPathName = PathParser
407: .render(tearDownPath);
408: buffer.append("\n").append("!include -teardown .")
409: .append(tearDownPathName).append("\n");
410: }
411: }
412: pageData.setContent(buffer.toString());
413: return pageData.getHtml();
414: }
415:
416: public static String getHtmlOfInheritedPage(String pageName,
417: WikiPage context) throws Exception {
418: return HtmlUtil.getLabeledHtmlOfInheritedPage(pageName,
419: context, "");
420: }
421:
422: public static String getLabeledHtmlOfInheritedPage(String pageName,
423: WikiPage context, String label) throws Exception {
424: WikiPage inheritedPage = PageCrawlerImpl.getInheritedPage(
425: pageName, context);
426: if (inheritedPage != null) {
427: PageData data = inheritedPage.getData();
428: if (label != null && label.length() > 1) {
429: WikiPagePath inheritedPagePath = context
430: .getPageCrawler().getFullPath(inheritedPage);
431: String inheritedPagePathName = PathParser
432: .render(inheritedPagePath);
433: String fullLabel = "!meta " + label + ": ."
434: + inheritedPagePathName + "\n";
435: String newContent = fullLabel + data.getContent();
436: data.setContent(newContent);
437: }
438: return data.getHtml(context);
439: } else {
440: return "";
441: }
442: }
443:
444: public static String metaText(String text) {
445: return "<span class=\"meta\">" + text + "</span>";
446: }
447:
448: public static HtmlTag makeJavascriptLink(String jsFile) {
449: HtmlTag scriptTag = new HtmlTag("script");
450: scriptTag.addAttribute("src", jsFile);
451: scriptTag.addAttribute("type", "text/javascript");
452: scriptTag.use("");
453: return scriptTag;
454: }
455:
456: public static HtmlTag makeJavascriptSection(String content) {
457: HtmlTag scriptTag = new HtmlTag("script");
458: scriptTag.addAttribute("type", "text/javascript");
459: scriptTag.add(content);
460: return scriptTag;
461: }
462: }
|