Clover coverage report - DrJava Test Coverage (drjava-20110828-r5448)
Coverage timestamp: Sun Aug 28 2011 03:13:33 CDT
file stats: LOC: 171   Methods: 5
NCLOC: 79   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DrJavaBook.java 70% 88.2% 100% 86.4%
coverage coverage
 1    /*BEGIN_COPYRIGHT_BLOCK
 2    *
 3    * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu)
 4    * All rights reserved.
 5    *
 6    * Redistribution and use in source and binary forms, with or without
 7    * modification, are permitted provided that the following conditions are met:
 8    * * Redistributions of source code must retain the above copyright
 9    * notice, this list of conditions and the following disclaimer.
 10    * * Redistributions in binary form must reproduce the above copyright
 11    * notice, this list of conditions and the following disclaimer in the
 12    * documentation and/or other materials provided with the distribution.
 13    * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
 14    * names of its contributors may be used to endorse or promote products
 15    * derived from this software without specific prior written permission.
 16    *
 17    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18    * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 19    * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 20    * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 21    * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 22    * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 23    * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 24    * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 25    * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 26    * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 27    * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28    *
 29    * This software is Open Source Initiative approved Open Source Software.
 30    * Open Source Initative Approved is a trademark of the Open Source Initiative.
 31    *
 32    * This file is part of DrJava. Download the current version of this project
 33    * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
 34    *
 35    * END_COPYRIGHT_BLOCK*/
 36   
 37    package edu.rice.cs.drjava.model.print;
 38   
 39    import java.util.*;
 40    import java.awt.print.*;
 41    import java.awt.*;
 42    import java.awt.font.*;
 43    import java.text.*;
 44   
 45    /**
 46    * The DrJavaBook class is DrJava's implementation of a Pageable object. It
 47    * serves as the control class for printing, and is responsible for
 48    * preparing the print job of previewing or printing given the String
 49    * representation of the document.
 50    *
 51    * @version $Id: DrJavaBook.java 5236 2010-04-27 01:43:36Z mgricken $
 52    */
 53    public class DrJavaBook implements Pageable {
 54   
 55    private ArrayList<PagePrinter> _pagePrinters;
 56    private PageFormat _format;
 57    private String _fileName;
 58   
 59    public static final Font PRINT_FONT = new Font("Monospaced", Font.PLAIN, 9);
 60    public static final Font FOOTER_FONT = new Font("Monospaced", Font.PLAIN, 8);
 61    public static final Font LINE_FONT = new Font("Monospaced", Font.ITALIC, 8);
 62    public float LINE_NUM_WIDTH;
 63   
 64    private static FontRenderContext DEFAULT_FRC = new FontRenderContext(null, false, true);
 65   
 66    /** Constructs a DrJavaBook which a given content text, filename, and pageformat. */
 67  3 public DrJavaBook(String text, String fileName, PageFormat format) {
 68  3 _pagePrinters = new ArrayList<PagePrinter>();
 69  3 _format = format;
 70  3 _fileName = fileName;
 71   
 72  3 TextLayout textl = new TextLayout("XXX ", LINE_FONT, DEFAULT_FRC);
 73  3 LINE_NUM_WIDTH = textl.getAdvance();
 74   
 75  3 setUpPagePrinters(text);
 76    }
 77   
 78    /** Method which creates all of the individual Printable objects
 79    * given a String text.
 80    * @param text The text of the document.
 81    */
 82  3 private void setUpPagePrinters(String text) {
 83  3 int linenum = 0;
 84  3 int reallinenum = 1;
 85  3 String thisText;
 86  3 FontRenderContext frc = new FontRenderContext(null, false, true);
 87   
 88    // determine the number of lines per page
 89  3 TextLayout textl = new TextLayout("X", PRINT_FONT, frc);
 90  3 float lineHeight = textl.getLeading() + textl.getAscent();
 91  3 int linesPerPage = (int) (_format.getImageableHeight() / lineHeight) - 1;
 92   
 93  3 HashMap<TextAttribute,Object> map = new HashMap<TextAttribute,Object>(); // Added parameterization <TextAttribute, Object>.
 94  3 map.put(TextAttribute.FONT, PRINT_FONT);
 95   
 96  3 char[] carriageReturn = {(char) 10};
 97  3 String lineSeparator = new String(carriageReturn);
 98   
 99  3 try {
 100  3 thisText = text.substring(0, text.indexOf(lineSeparator));
 101  0 text = text.substring(text.indexOf(lineSeparator) + 1);
 102    }
 103    catch (StringIndexOutOfBoundsException e) {
 104  3 thisText = text;
 105  3 text = "";
 106    }
 107   
 108  3 int page = 0;
 109  3 PagePrinter thisPagePrinter = new PagePrinter(page, _fileName, this);
 110  3 _pagePrinters.add(thisPagePrinter);
 111   
 112    // loop over each of the *real* lines in the document
 113  3 while (! (thisText.equals("") && (text.equals("")))) {
 114  0 if (thisText.equals("")) thisText = " ";
 115   
 116  3 AttributedCharacterIterator charIterator = (new AttributedString(thisText, map)).getIterator();
 117  3 LineBreakMeasurer measurer = new LineBreakMeasurer(charIterator, frc);
 118   
 119  3 boolean isCarryLine = false;
 120   
 121    // loop over each of the broken lines in the real line
 122  3 while (measurer.getPosition() < charIterator.getEndIndex()) {
 123  3 TextLayout pageNumber = new TextLayout(" ", LINE_FONT, DEFAULT_FRC);
 124   
 125  3 if (! isCarryLine)
 126  3 pageNumber = new TextLayout("" + reallinenum, LINE_FONT, DEFAULT_FRC);
 127   
 128    // add this TextLayout to the PagePrinter
 129  3 thisPagePrinter.add(measurer.nextLayout((float) _format.getImageableWidth() - LINE_NUM_WIDTH), pageNumber);
 130   
 131  3 linenum++;
 132    // Create a new PagePrinter, if necessary
 133  3 if (linenum == (linesPerPage * (page+1)))
 134    {
 135  0 page++;
 136  0 thisPagePrinter = new PagePrinter(page, _fileName, this);
 137  0 _pagePrinters.add(thisPagePrinter);
 138    }
 139   
 140  3 isCarryLine = true;
 141    }
 142   
 143  3 reallinenum++;
 144   
 145    // Get next *real* line
 146  3 try {
 147  3 thisText = text.substring(0, text.indexOf(lineSeparator));
 148  0 text = text.substring(text.indexOf(lineSeparator) + 1);
 149    } catch (StringIndexOutOfBoundsException e) {
 150  3 thisText = text;
 151  3 text = "";
 152    }
 153    }
 154    }
 155   
 156    /** @return The number of pages in this print job. */
 157  1 public int getNumberOfPages() { return _pagePrinters.size(); }
 158   
 159    /** Returns the PageFormat for this print job.
 160    * @param pageIndex The page number
 161    * @return the PageFormat of this print job.
 162    */
 163  1 public PageFormat getPageFormat(int pageIndex) { return _format; }
 164   
 165    /** Returns the Printable object for a given page.
 166    * @param pageIndex The page number.
 167    * @return The Printable object for the given page.
 168    */
 169  2 public Printable getPrintable(int pageIndex) { return _pagePrinters.get(pageIndex); }
 170   
 171    }