001: /*
002: * This file is part of "SnipSnap Radeox Rendering Engine".
003: *
004: * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
005: * All Rights Reserved.
006: *
007: * Please visit http://radeox.org/ for updates and contact.
008: *
009: * --LICENSE NOTICE--
010: * Licensed under the Apache License, Version 2.0 (the "License");
011: * you may not use this file except in compliance with the License.
012: * You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: * --LICENSE NOTICE--
022: */
023: package org.radeox.macro.list;
024:
025: import java.io.IOException;
026: import java.io.Writer;
027: import java.util.ArrayList;
028: import java.util.Collection;
029: import java.util.HashMap;
030: import java.util.Iterator;
031: import java.util.List;
032: import java.util.Map;
033:
034: import org.radeox.util.Linkable;
035: import org.radeox.util.Nameable;
036:
037: /**
038: * Formats a list as AtoZ listing separated by the alphabetical characters.
039: *
040: * @author Matthias L. Jugel
041: * @version $Id: AtoZListFormatter.java 7707 2006-04-12 17:30:19Z
042: * ian@caret.cam.ac.uk $
043: */
044: public class AtoZListFormatter implements ListFormatter {
045: public String getName() {
046: return "atoz";
047: }
048:
049: /**
050: * Create an A to Z index
051: */
052: public void format(Writer writer, Linkable current,
053: String listComment, Collection c, String emptyText,
054: boolean showSize) throws IOException {
055: if (c.size() > 0) {
056: Iterator it = c.iterator();
057: Map atozMap = new HashMap();
058: List numberRestList = new ArrayList();
059: List otherRestList = new ArrayList();
060: while (it.hasNext()) {
061: Object object = it.next();
062: String name, indexChar;
063: if (object instanceof Nameable) {
064: name = ((Nameable) object).getName();
065: } else {
066: name = object.toString();
067: }
068: indexChar = name.substring(0, 1).toUpperCase();
069: if (object instanceof Linkable) {
070: name = ((Linkable) object).getLink();
071: }
072:
073: if (indexChar.charAt(0) >= 'A'
074: && indexChar.charAt(0) <= 'Z') {
075: if (!atozMap.containsKey(indexChar)) {
076: atozMap.put(indexChar, new ArrayList());
077: }
078: List list = (List) atozMap.get(indexChar);
079: list.add(name);
080: } else if (indexChar.charAt(0) >= '0'
081: && indexChar.charAt(0) <= '9') {
082: numberRestList.add(name);
083: } else {
084: otherRestList.add(name);
085: }
086: }
087:
088: writer
089: .write("<table width=\"100%\" class=\"index-top\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">");
090: for (int idxChar = 'A'; idxChar <= 'Z';) {
091: writer.write("<tr>");
092: for (int i = 0; i < 6 && idxChar + i <= 'Z'; i++) {
093: String ch = "" + (char) (idxChar + i);
094: writer.write("<th><b> <a href=\"");
095: writer.write(current.getLink());
096: writer.write("#idx" + ch + "\">");
097: writer.write(ch);
098: writer.write("</a></b></th>");
099: writer.write("<th>...</th><th>");
100: writer.write(""
101: + (atozMap.get(ch) == null ? 0
102: : ((List) atozMap.get(ch)).size()));
103: writer.write(" </th>");
104: }
105: idxChar += 6;
106: if (idxChar >= 'Z') {
107: writer.write("<th><b> <a href=\"");
108: writer.write(current.getLink());
109: writer.write("#idx0-9\">0-9</a></b></th>");
110: writer.write("<th>...</th><th>");
111: writer.write("" + numberRestList.size());
112: writer.write(" </th>");
113: writer.write("<th><b> <a href=\"");
114: writer.write(current.getLink());
115: writer.write("#idxAT\">@</a></b></th>");
116: writer.write("<th>...</th><th>");
117: writer.write("" + otherRestList.size());
118: writer.write(" </th>");
119: writer
120: .write("<th></th><th></th><th></th><th></th>");
121: writer
122: .write("<th></th><th></th><th></th><th></th>");
123: }
124: writer.write("</tr>");
125:
126: }
127: writer.write("</table>");
128:
129: writer.write("<div class=\"list-title\">");
130: writer.write(listComment);
131: if (showSize) {
132: writer.write(" (");
133: writer.write("" + c.size());
134: writer.write(")");
135: }
136: writer.write("</div>");
137: writer
138: .write("<table width=\"100%\" class=\"index\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">");
139: for (int ch = 'A'; ch <= 'Z'; ch += 2) {
140: String left = "" + (char) ch;
141: String right = "" + (char) (ch + 1);
142:
143: insertCharHeader(writer, left, right);
144: addRows(writer, (List) atozMap.get(left),
145: (List) atozMap.get(right));
146: }
147: insertCharHeader(writer, "0-9", "@");
148: addRows(writer, numberRestList, otherRestList);
149: writer.write("</table>");
150: } else {
151: writer.write(emptyText);
152: }
153: }
154:
155: private void addRows(Writer writer, List listLeft, List listRight)
156: throws IOException {
157: Iterator leftIt = listLeft != null ? listLeft.iterator()
158: : new EmptyIterator();
159: Iterator rightIt = listRight != null ? listRight.iterator()
160: : new EmptyIterator();
161:
162: while (leftIt.hasNext() || rightIt.hasNext()) {
163: String leftName = (String) (leftIt != null
164: && leftIt.hasNext() ? leftIt.next() : null);
165: String rightName = (String) (rightIt != null
166: && rightIt.hasNext() ? rightIt.next() : null);
167: insertRow(writer, leftName, rightName, false);
168: }
169: }
170:
171: private void insertCharHeader(Writer writer, String leftHeader,
172: String rightHeader) throws IOException {
173: writer.write("<tr><th>");
174: writer.write("<b><a name=\"idx");
175: writer.write("@".equals(leftHeader) ? "AT" : leftHeader);
176: writer.write("\"></a>");
177: writer.write(leftHeader);
178: writer.write("</b></th><th> </th><th>");
179: writer.write("<b><a name=\"idx");
180: writer.write("@".equals(rightHeader) ? "AT" : rightHeader);
181: writer.write("\"></a>");
182: writer.write(rightHeader);
183: writer.write("</b></th></tr>");
184: }
185:
186: private void insertRow(Writer writer, String left, String right,
187: boolean odd) throws IOException {
188: writer.write("<tr><td>");
189: if (left != null) {
190: writer.write(left);
191: }
192: writer.write("</td><td> </td><td>");
193: if (right != null) {
194: writer.write(right);
195: }
196: writer.write("</td></tr>");
197: }
198:
199: private class EmptyIterator implements Iterator {
200: public boolean hasNext() {
201: return false;
202: }
203:
204: public Object next() {
205: return null;
206: }
207:
208: public void remove() {
209: }
210: }
211: }
|