Coverage Report - org.argouml.uml.generator.AbstractSection
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractSection
0%
0/86
0%
0/36
5.2
 
 1  
 /* $Id: AbstractSection.java 17868 2010-01-12 20:47:51Z linus $
 2  
  *****************************************************************************
 3  
  * Copyright (c) 2009 Contributors - see below
 4  
  * All rights reserved. This program and the accompanying materials
 5  
  * are made available under the terms of the Eclipse Public License v1.0
 6  
  * which accompanies this distribution, and is available at
 7  
  * http://www.eclipse.org/legal/epl-v10.html
 8  
  *
 9  
  * Contributors:
 10  
  *    tfmorris
 11  
  *****************************************************************************
 12  
  *
 13  
  * Some portions of this file was previously release using the BSD License:
 14  
  */
 15  
 
 16  
 // Copyright (c) 1996-2006 The Regents of the University of California. All
 17  
 // Rights Reserved. Permission to use, copy, modify, and distribute this
 18  
 // software and its documentation without fee, and without a written
 19  
 // agreement is hereby granted, provided that the above copyright notice
 20  
 // and this paragraph appear in all copies.  This software program and
 21  
 // documentation are copyrighted by The Regents of the University of
 22  
 // California. The software program and documentation are supplied "AS
 23  
 // IS", without any accompanying services from The Regents. The Regents
 24  
 // does not warrant that the operation of the program will be
 25  
 // uninterrupted or error-free. The end-user understands that the program
 26  
 // was developed for research purposes and is advised not to rely
 27  
 // exclusively on the program for any reason.  IN NO EVENT SHALL THE
 28  
 // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
 29  
 // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
 30  
 // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 31  
 // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 32  
 // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
 33  
 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 34  
 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 35  
 // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 36  
 // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
 37  
 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 38  
 
 39  
 package org.argouml.uml.generator;
 40  
 
 41  
 import java.io.BufferedReader;
 42  
 import java.io.EOFException;
 43  
 import java.io.FileReader;
 44  
 import java.io.FileWriter;
 45  
 import java.io.IOException;
 46  
 import java.util.HashMap;
 47  
 import java.util.Iterator;
 48  
 import java.util.Map;
 49  
 import java.util.Set;
 50  
 
 51  
 import org.apache.log4j.Logger;
 52  
 
 53  
 /**
 54  
  * Reading and writing preserved sections from the code.
 55  
  *
 56  
  * @author Marian Heddesheimer
 57  
  */
 58  
 public abstract class AbstractSection {
 59  
     /**
 60  
      * Logger.
 61  
      */
 62  0
     private static final Logger LOG =
 63  
         Logger.getLogger(AbstractSection.class);
 64  
 
 65  
     /**
 66  
      * System newline separator.
 67  
      */
 68  0
     private static final String LINE_SEPARATOR =
 69  
         System.getProperty("line.separator");
 70  
 
 71  
     private Map<String, String> mAry;
 72  
 
 73  
     /**
 74  
      * Creates a new instance of Section.
 75  
      */
 76  0
     public AbstractSection() {
 77  0
         mAry = new HashMap<String, String>();        
 78  0
     }
 79  
 
 80  
     /**
 81  
      * @param id the string to generate
 82  
      * @param indent the current indentation
 83  
      * @return the generated string
 84  
      */
 85  
     public static String generate(String id, String indent) {
 86  0
         return "";
 87  
     }
 88  
 
 89  
     /**
 90  
      * write TODO: Check if sections are not used within the file and
 91  
      * put them as comments at the end of the file.
 92  
      * Hint: use a second Map to compare with the used keys.
 93  
      *
 94  
     * @param filename the file name
 95  
      * @param indent the current indentation
 96  
      * @param outputLostSections true if lost sections are to be written
 97  
      */
 98  
     public void write(String filename, String indent,
 99  
                       boolean outputLostSections) {
 100  
         try {
 101  0
             FileReader f = new FileReader(filename);
 102  0
             BufferedReader fr = new BufferedReader(f);
 103  
             // TODO: This is using the default platform character encoding
 104  
             // specifying an encoding will produce more predictable results
 105  0
             FileWriter fw = new FileWriter(filename + ".out");
 106  0
             String line = "";
 107  0
             line = fr.readLine();
 108  0
             while (line != null) {
 109  0
                 String sectionId = getSectId(line);
 110  0
                 if (sectionId != null) {
 111  0
                     String content = mAry.get(sectionId);
 112  0
                     if (content != null) {
 113  0
                         fw.write(line + LINE_SEPARATOR);
 114  0
                         fw.write(content);
 115  
                         // read until the end section is found, discard
 116  
                         // generated content
 117  0
                         String endSectionId = null;
 118  
                         do {
 119  0
                             line = fr.readLine();
 120  0
                             if (line == null) {
 121  0
                                 throw new EOFException(
 122  
                                         "Reached end of file while looking "
 123  
                                         + "for the end of section with ID = \""
 124  
                                         + sectionId + "\"!");
 125  
                             }
 126  0
                             endSectionId = getSectId(line);
 127  0
                         } while (endSectionId == null);
 128  0
                         if (!endSectionId.equals(sectionId)) {
 129  0
                             LOG.error("Mismatch between sectionId (\""
 130  
                                     + sectionId + "\") and endSectionId (\""
 131  
                                     + endSectionId + "\")!");
 132  
                         }
 133  
                     }
 134  0
                     mAry.remove(sectionId);
 135  
                 }
 136  0
                 fw.write(line);
 137  0
                 line = fr.readLine();
 138  0
                 if (line != null) {
 139  0
                     fw.write(LINE_SEPARATOR);
 140  
                 }
 141  0
             }
 142  0
             if ((!mAry.isEmpty()) && (outputLostSections)) {
 143  0
                 fw.write("/* lost code following: " + LINE_SEPARATOR);
 144  0
                 Set mapEntries = mAry.entrySet();
 145  0
                 Iterator itr = mapEntries.iterator();
 146  0
                 while (itr.hasNext()) {
 147  0
                     Map.Entry entry = (Map.Entry) itr.next();
 148  0
                     fw.write(indent + "// section " + entry.getKey()
 149  
                              + " begin" + LINE_SEPARATOR);
 150  0
                     fw.write((String) entry.getValue());
 151  0
                     fw.write(indent + "// section " + entry.getKey()
 152  
                              + " end" + LINE_SEPARATOR);
 153  0
                 }
 154  0
                 fw.write("*/");
 155  
             }
 156  0
             fr.close();
 157  0
             fw.close();
 158  0
         } catch (IOException e) {
 159  0
             LOG.error("Error: " + e.toString());
 160  0
         }
 161  0
     }
 162  
 
 163  
     /**
 164  
      * @param filename the filename to read from
 165  
      */
 166  
     public void read(String filename) {
 167  
         try {
 168  
             // TODO: This is using the default platform character encoding
 169  
             // specifying an encoding will produce more predictable results
 170  0
             FileReader f = new FileReader(filename);
 171  0
             BufferedReader fr = new BufferedReader(f);
 172  
 
 173  0
             String line = "";
 174  0
             StringBuilder content = new StringBuilder();
 175  0
             boolean inSection = false;
 176  0
             while (line != null) {
 177  0
                 line = fr.readLine();
 178  0
                 if (line != null) {
 179  0
                     if (inSection) {
 180  0
                         String sectionId = getSectId(line);
 181  0
                         if (sectionId != null) {
 182  0
                             inSection = false;
 183  0
                             mAry.put(sectionId, content.toString());
 184  0
                             content = new StringBuilder();
 185  
                         } else {
 186  0
                             content.append(line + LINE_SEPARATOR);
 187  
                         }
 188  0
                     } else {
 189  0
                         String sectionId = getSectId(line);
 190  0
                         if (sectionId != null) {
 191  0
                             inSection = true;
 192  
                         }
 193  0
                     }
 194  
                 }
 195  
             }
 196  0
             fr.close();
 197  0
         } catch (IOException e) {
 198  0
             LOG.error("Error: " + e.toString());
 199  0
         }
 200  0
     }
 201  
 
 202  
     /**
 203  
      * @param line the given line
 204  
      * @return the section identifier
 205  
      */
 206  
     public static String getSectId(String line) {
 207  0
         final String begin = "// section ";
 208  0
         final String end1 = " begin";
 209  0
         final String end2 = " end";
 210  0
         int first = line.indexOf(begin);
 211  0
         int second = line.indexOf(end1);
 212  0
         if (second < 0) {
 213  0
             second = line.indexOf(end2);
 214  
         }
 215  0
         String s = null;
 216  0
         if ((first >= 0) && (second >= 0)) {
 217  0
             first = first + begin.length();
 218  0
             s = line.substring(first, second);
 219  
         }
 220  0
         return s;
 221  
     }
 222  
 }