001: package org.andromda.maven.doxia.module.xdoc;
002:
003: /*
004: * Copyright 2004-2006 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import org.apache.maven.doxia.module.HtmlTools;
020: import org.apache.maven.doxia.module.apt.AptParser;
021: import org.apache.maven.doxia.sink.SinkAdapter;
022: import org.apache.maven.doxia.sink.StructureSink;
023: import org.apache.maven.doxia.util.LineBreaker;
024:
025: import java.io.Writer;
026:
027: /**
028: * A doxia Sink which produces an xdoc model.
029: *
030: * @author <a href="mailto:james@jamestaylor.org">James Taylor</a>
031: * @version $Id: AndromdadocSink.java,v 1.1.2.1 2006/09/14 08:10:39 vancek Exp $
032: *
033: * @plexus.component role="org.apache.maven.doxia.sink.Sink" role-hint="andromdadoc"
034: */
035: public class AndromdadocSink extends SinkAdapter {
036: private static final String EOL = System
037: .getProperty("line.separator");
038:
039: private LineBreaker out;
040:
041: private StringBuffer buffer = new StringBuffer();
042:
043: private boolean headFlag;
044:
045: private int itemFlag;
046:
047: private boolean boxedFlag;
048:
049: private boolean verbatimFlag;
050:
051: private int[] cellJustif;
052:
053: private int cellCount;
054:
055: private String section;
056:
057: public AndromdadocSink(Writer out) {
058: this .out = new LineBreaker(out);
059: }
060:
061: protected void resetState() {
062: headFlag = false;
063: buffer = new StringBuffer();
064: itemFlag = 0;
065: boxedFlag = false;
066: verbatimFlag = false;
067: cellJustif = null;
068: cellCount = 0;
069: }
070:
071: public void head() {
072: resetState();
073:
074: headFlag = true;
075:
076: markup("<?xml version=\"1.0\" ?>" + EOL);
077:
078: markup("<document>" + EOL);
079:
080: markup("<properties>" + EOL);
081: }
082:
083: public void head_() {
084: headFlag = false;
085:
086: markup("</properties>" + EOL);
087: }
088:
089: public void title_() {
090: if (buffer.length() > 0) {
091: markup("<title>");
092: content(buffer.toString());
093: markup("</title>" + EOL);
094: buffer = new StringBuffer();
095: }
096: }
097:
098: public void author_() {
099: if (buffer.length() > 0) {
100: markup("<author>");
101: content(buffer.toString());
102: markup("</author>" + EOL);
103: buffer = new StringBuffer();
104: }
105: }
106:
107: public void date_() {
108: if (buffer.length() > 0) {
109: markup("<date>");
110: content(buffer.toString());
111: markup("</date>");
112: buffer = new StringBuffer();
113: }
114: }
115:
116: public void body() {
117: markup("<body>" + EOL);
118: }
119:
120: public void body_() {
121: markup("</body>" + EOL);
122:
123: markup("</document>" + EOL);
124:
125: out.flush();
126:
127: resetState();
128: }
129:
130: public void section1() {
131: section = "section";
132: }
133:
134: public void section2() {
135: section = "subsection";
136: }
137:
138: public void section3() {
139: section = "subsection";
140: }
141:
142: public void section4() {
143: section = "subsection";
144: }
145:
146: public void section5() {
147: section = "subsection";
148: }
149:
150: public void sectionTitle() {
151: markup("<" + section + " name=\"");
152: }
153:
154: public void sectionTitle_() {
155: markup("\">");
156: }
157:
158: public void section1_() {
159: markup("</section>");
160: }
161:
162: public void section2_() {
163: markup("</subsection>");
164: }
165:
166: public void section3_() {
167: markup("</subsection>");
168: }
169:
170: public void section4_() {
171: markup("</subsection>");
172: }
173:
174: public void section5_() {
175: markup("</subsection>");
176: }
177:
178: public void list() {
179: markup("<ul>" + EOL);
180: }
181:
182: public void list_() {
183: markup("</ul>");
184: }
185:
186: public void listItem() {
187: markup("<li>");
188: itemFlag++;
189: // What follows is at least a paragraph.
190: }
191:
192: public void listItem_() {
193: markup("</li>" + EOL);
194: }
195:
196: public void numberedList(int numbering) {
197: String style;
198: switch (numbering) {
199: case NUMBERING_UPPER_ALPHA:
200: style = "upper-alpha";
201: break;
202: case NUMBERING_LOWER_ALPHA:
203: style = "lower-alpha";
204: break;
205: case NUMBERING_UPPER_ROMAN:
206: style = "upper-roman";
207: break;
208: case NUMBERING_LOWER_ROMAN:
209: style = "lower-roman";
210: break;
211: case NUMBERING_DECIMAL:
212: default:
213: style = "decimal";
214: }
215: markup("<ol style=\"list-style-type: " + style + "\">" + EOL);
216: }
217:
218: public void numberedList_() {
219: markup("</ol>");
220: }
221:
222: public void numberedListItem() {
223: markup("<li>");
224: itemFlag++;
225: // What follows is at least a paragraph.
226: }
227:
228: public void numberedListItem_() {
229: markup("</li>" + EOL);
230: }
231:
232: public void definitionList() {
233: markup("<dl compact=\"compact\">" + EOL);
234: }
235:
236: public void definitionList_() {
237: markup("</dl>");
238: }
239:
240: public void definedTerm() {
241: markup("<dt><b>");
242: }
243:
244: public void definedTerm_() {
245: markup("</b></dt>" + EOL);
246: }
247:
248: public void definition() {
249: markup("<dd>");
250: itemFlag++;
251: // What follows is at least a paragraph.
252: }
253:
254: public void definition_() {
255: markup("</dd>" + EOL);
256: }
257:
258: public void paragraph() {
259: if (itemFlag == 0) {
260: markup("<p>");
261: }
262: }
263:
264: public void paragraph_() {
265: if (itemFlag == 0) {
266: markup("</p>");
267: } else {
268: itemFlag--;
269: }
270:
271: }
272:
273: public void verbatim(boolean boxed) {
274: verbatimFlag = true;
275: boxedFlag = boxed;
276: if (boxed) {
277: markup("<source>");
278: } else {
279: markup("<pre>");
280: }
281: }
282:
283: public void verbatim_() {
284: if (boxedFlag) {
285: markup("</source>");
286: } else {
287: markup("</pre>");
288: }
289:
290: verbatimFlag = false;
291:
292: boxedFlag = false;
293: }
294:
295: public void horizontalRule() {
296: markup("<hr />");
297: }
298:
299: public void table() {
300: markup("<table align=\"center\">" + EOL);
301: }
302:
303: public void table_() {
304: markup("</table>");
305: }
306:
307: public void tableRows(int[] justification, boolean grid)
308:
309: {
310: markup("<table align=\"center\" border=\"" + (grid ? 1 : 0)
311: + "\">" + EOL);
312: this .cellJustif = justification;
313: }
314:
315: public void tableRows_() {
316: markup("</table>");
317: }
318:
319: public void tableRow() {
320: markup("<tr valign=\"top\">" + EOL);
321: cellCount = 0;
322: }
323:
324: public void tableRow_() {
325: markup("</tr>" + EOL);
326: cellCount = 0;
327: }
328:
329: public void tableCell() {
330: tableCell(false);
331: }
332:
333: public void tableHeaderCell() {
334: tableCell(true);
335: }
336:
337: public void tableCell(boolean headerRow) {
338: String justif = null;
339:
340: if (cellJustif != null) {
341: switch (cellJustif[cellCount]) {
342: case AptParser.JUSTIFY_LEFT:
343: justif = "left";
344: break;
345: case AptParser.JUSTIFY_RIGHT:
346: justif = "right";
347: break;
348: case AptParser.JUSTIFY_CENTER:
349: default:
350: justif = "center";
351: break;
352: }
353: }
354:
355: if (justif != null) {
356: markup("<t" + (headerRow ? 'h' : 'd') + " align=\""
357: + justif + "\">");
358: } else {
359: markup("<t" + (headerRow ? 'h' : 'd') + ">");
360: }
361: }
362:
363: public void tableCell_() {
364: tableCell_(false);
365: }
366:
367: public void tableHeaderCell_() {
368: tableCell_(true);
369: }
370:
371: public void tableCell_(boolean headerRow) {
372: markup("</t" + (headerRow ? 'h' : 'd') + ">" + EOL);
373: ++cellCount;
374: }
375:
376: public void tableCaption() {
377: markup("<p><i>");
378: }
379:
380: public void tableCaption_() {
381: markup("</i></p>");
382: }
383:
384: public void anchor(String name) {
385: if (!headFlag) {
386: String id = StructureSink.linkToKey(name);
387: markup("<a id=\"" + id + "\" name=\"" + id + "\">");
388: }
389: }
390:
391: public void anchor_() {
392: if (!headFlag) {
393: markup("</a>");
394: }
395: }
396:
397: public void link(String name) {
398: if (!headFlag) {
399: markup("<a href=\"" + name + "\">");
400: }
401: }
402:
403: public void link_() {
404: if (!headFlag) {
405: markup("</a>");
406: }
407: }
408:
409: public void italic() {
410: if (!headFlag) {
411: markup("<i>");
412: }
413: }
414:
415: public void italic_() {
416: if (!headFlag) {
417: markup("</i>");
418: }
419: }
420:
421: public void bold() {
422: if (!headFlag) {
423: markup("<b>");
424: }
425: }
426:
427: public void bold_() {
428: if (!headFlag) {
429: markup("</b>");
430: }
431: }
432:
433: public void monospaced() {
434: if (!headFlag) {
435: markup("<tt>");
436: }
437: }
438:
439: public void monospaced_() {
440: if (!headFlag) {
441: markup("</tt>");
442: }
443: }
444:
445: public void lineBreak() {
446: if (headFlag) {
447: buffer.append(EOL);
448: } else {
449: markup("<br />");
450: }
451: }
452:
453: public void nonBreakingSpace() {
454: if (headFlag) {
455: buffer.append(' ');
456: } else {
457: markup(" ");
458: }
459: }
460:
461: public void text(String text) {
462: if (headFlag) {
463: buffer.append(text);
464: } else {
465: if (verbatimFlag) {
466: verbatimContent(text);
467: } else {
468: content(text);
469: }
470: }
471: }
472:
473: // ----------------------------------------------------------------------
474: //
475: // ----------------------------------------------------------------------
476:
477: protected void markup(String text) {
478: out.write(text, true);
479: }
480:
481: protected void content(String text) {
482: out.write(escapeHTML(text), false);
483: }
484:
485: protected void verbatimContent(String text) {
486: out.write(escapeHTML(text), true);
487: }
488:
489: public static String escapeHTML(String text) {
490: return HtmlTools.escapeHTML(text);
491: }
492:
493: public static String encodeURL(String text) {
494: return HtmlTools.encodeURL(text);
495: }
496:
497: public void flush() {
498: out.flush();
499: }
500:
501: public void close() {
502: out.close();
503: }
504: }
|