/** * HTMLTableStream: stream out a pretty HTML Table based on attributes set by the caller */ package IISSample; import java.io.*; public class HTMLTableStream { // General Constants private static final String STR_NOT_SET = null; private static final int INT_NOT_SET = 0xffffffff; private static final boolean BOOL_NOT_SET = false; // This seems useless, but it is for consistency. private static final String INTERNAL_ERROR_MSG = new String("HTMLTableStream: Internal error encountered."); // Table Alignment Constants private static final int ALIGN_BASE = 0xF0000000; private static final int ALIGN_BOUNDS = 6; public static final int ALIGN_LEFT = ALIGN_BASE + 1; public static final int ALIGN_CENTER = ALIGN_BASE + 2; public static final int ALIGN_RIGHT = ALIGN_BASE + 3; public static final int ALIGN_BLEED_LEFT = ALIGN_BASE + 4; public static final int ALIGN_BLEED_RIGHT = ALIGN_BASE + 5; public static final int ALIGN_JUSTIFY = ALIGN_BASE + 6; // Clear Constants private static final int CLEAR_BASE = 0xF1000000; private static final int CLEAR_BOUNDS = 4; public static final int CLEAR_NO = CLEAR_BASE + 1; public static final int CLEAR_LEFT = CLEAR_BASE + 2; public static final int CLEAR_RIGHT = CLEAR_BASE + 3; public static final int CLEAR_ALL = CLEAR_BASE + 4; // Frame Type Constants private static final int FRAME_BASE = 0xF2000000; private static final int FRAME_BOUNDS = 9; public static final int FRAME_BORDER = FRAME_BASE + 1; public static final int FRAME_VOID = FRAME_BASE + 2; public static final int FRAME_ABOVE = FRAME_BASE + 3; public static final int FRAME_BELOW = FRAME_BASE + 4; public static final int FRAME_HSIDES = FRAME_BASE + 5; public static final int FRAME_LHS = FRAME_BASE + 6; public static final int FRAME_RHS = FRAME_BASE + 7; public static final int FRAME_VSIDES = FRAME_BASE + 8; public static final int FRAME_BOX = FRAME_BASE + 9; // Rules Constants private static final int RULES_BASE = 0xF3000000; private static final int RULES_BOUNDS = 5; public static final int RULES_NONE = RULES_BASE + 1; public static final int RULES_GROUPS = RULES_BASE + 2; public static final int RULES_ROWS = RULES_BASE + 3; public static final int RULES_COLS = RULES_BASE + 4; public static final int RULES_ALL = RULES_BASE + 5; // Vertical Alignment Constants private static final int VALIGN_BASE = 0xF4000000; private static final int VALIGN_BOUNDS = 4; public static final int VALIGN_TOP = VALIGN_BASE + 1; public static final int VALIGN_MIDDLE = VALIGN_BASE + 2; public static final int VALIGN_BOTTOM = VALIGN_BASE + 3; public static final int VALIGN_BASELINE = VALIGN_BASE + 4; // Internal use streams private StreamTokenizer tokIn = null; private Reader readIn = null; // Internal use variables private int m_iAlignment = INT_NOT_SET; private String m_strBackground = STR_NOT_SET; private String m_strBackgroundColor = STR_NOT_SET; private int m_iBorderWidth = INT_NOT_SET; private String m_strBorderColor = STR_NOT_SET; private String m_strBorderColorDark = STR_NOT_SET; private String m_strBorderColorLight = STR_NOT_SET; private int m_iCellPadding = INT_NOT_SET; private int m_iCellSpacing = INT_NOT_SET; private int m_iClear = INT_NOT_SET; private String m_strClass = STR_NOT_SET; private int m_iColumns = INT_NOT_SET; private int m_iFrameType = INT_NOT_SET; private int m_iID = INT_NOT_SET; private boolean m_fNoWrap = BOOL_NOT_SET; private int m_iRules = INT_NOT_SET; private String m_strStyle = STR_NOT_SET; private int m_iVertAlignment = INT_NOT_SET; private String m_strWidth = STR_NOT_SET; // Constructor public HTMLTableStream(InputStream in) { readIn = new BufferedReader(new InputStreamReader(in)); tokIn = new StreamTokenizer(readIn); tokIn.eolIsSignificant(true); tokIn.quoteChar((int)','); } public void setDelimiter(char c) { tokIn.quoteChar((int) c); } public void setAlign(int iAlign) { int iAlignVal = iAlign & (~ALIGN_BASE); if ( (iAlignVal != 0) && (iAlignVal <= ALIGN_BOUNDS) ) { m_iAlignment = iAlign; } else { throw new IllegalArgumentException("Align constant out of bounds."); } } private String TestString(String s) { if (s == null) { throw new IllegalArgumentException("Null string."); } else if (s.equals("")) { throw new IllegalArgumentException("Zero length string."); } else return s; } private int TestNonNegative(int i) { if (i < 0) { throw new IllegalArgumentException("Negative integer."); } else return i; } public void setBackground(String strBackground) { m_strBackground = TestString(strBackground); } public void setBackgroundColor(String strBackgroundColor) { m_strBackgroundColor = TestString(strBackgroundColor); } public void setBorderWidth(int iWidth) { m_iBorderWidth = TestNonNegative(iWidth); } public void setBorderColor(String strBorderColor) { m_strBorderColor = TestString(strBorderColor); } public void setBorderColorDark(String strBorderColorDark) { m_strBorderColorDark = TestString(strBorderColorDark); } public void setBorderColorLight(String strBorderColorLight) { m_strBorderColorLight = TestString(strBorderColorLight); } public void setCellPadding(int iCellPadding) { m_iCellPadding = TestNonNegative(iCellPadding); } public void setCellSpacing(int iCellSpacing) { m_iCellSpacing = TestNonNegative(iCellSpacing); } public void setClear(int iClear) { int iClearVal = iClear & (~CLEAR_BASE); if ( (iClearVal != 0) && (iClearVal <= CLEAR_BOUNDS) ) { m_iClear = iClear; } else { throw new IllegalArgumentException("Clear constant out of bounds."); } } public void setClass(String iClass) { m_strClass = TestString(iClass); } public void setCols(int iCols) { m_iColumns = TestNonNegative(iCols); } public void setFrameType(int iFrameType) { int iFrameTypeVal = iFrameType & (~FRAME_BASE); if ( (iFrameTypeVal != 0) && (iFrameTypeVal <= FRAME_BOUNDS) ) { m_iFrameType = iFrameType; } else { throw new IllegalArgumentException("Frame type constant out of bounds."); } } public void setID(int iID) { m_iID = iID; // Don't care what the value is here. } public void setNoWrap(boolean fNoWrap) { m_fNoWrap = fNoWrap; } public void setRules(int iRules) { int iRulesVal = iRules & (~RULES_BASE); if ( (iRulesVal != 0) && (iRulesVal <= RULES_BOUNDS) ) { m_iRules = iRules; } else { throw new IllegalArgumentException("Rules constant out of bounds."); } } public void setStyle(String strStyle) { m_strStyle = TestString(strStyle); } public void setVerticalAlign(int iVAlign) { int iVAlignVal = iVAlign & (~VALIGN_BASE); if ( (iVAlignVal != 0) && (iVAlignVal <= VALIGN_BOUNDS) ) { m_iVertAlignment = iVAlign; } else { throw new IllegalArgumentException("VAlign constant out of bounds."); } } public void setWidth(int iWidth) { m_strWidth = Integer.toString(iWidth); } public void setWidth(String strWidth) { m_strWidth = TestString(strWidth); } public void OutputTable(OutputStream out) { OutputStreamWriter osw = new OutputStreamWriter(out); try { osw.write("\n"); osw.flush(); } catch (IOException ioe) { return; } // Do the parsing from the input stream. int ttype; try { ttype = tokIn.nextToken(); osw.write(""); while (ttype != StreamTokenizer.TT_EOF) { if (ttype == StreamTokenizer.TT_EOL) { osw.write("\n"); osw.flush(); } if (ttype == StreamTokenizer.TT_NUMBER) { osw.write("" + tokIn.nval + ""); } if (ttype == StreamTokenizer.TT_WORD) { osw.write("" + tokIn.sval + ""); } ttype = tokIn.nextToken(); } osw.write(""); osw.close(); } catch (IOException ioe) { return; } } }