Coverage Report - org.argouml.persistence.MemberFilePersister
 
Classes in this File Line Coverage Branch Coverage Complexity
MemberFilePersister
6%
1/15
0%
0/8
2.333
 
 1  
 /* $Id: MemberFilePersister.java 17832 2010-01-12 19:02:29Z 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-2009 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.persistence;
 40  
 
 41  
 import java.io.BufferedReader;
 42  
 import java.io.File;
 43  
 import java.io.FileInputStream;
 44  
 import java.io.FileNotFoundException;
 45  
 import java.io.IOException;
 46  
 import java.io.InputStream;
 47  
 import java.io.InputStreamReader;
 48  
 import java.io.OutputStream;
 49  
 import java.io.PrintWriter;
 50  
 import java.net.URL;
 51  
 
 52  
 import org.argouml.application.api.Argo;
 53  
 import org.argouml.kernel.Project;
 54  
 import org.argouml.kernel.ProjectMember;
 55  
 import org.xml.sax.InputSource;
 56  
 
 57  
 /**
 58  
  * A base class file persister for project members.
 59  
  * @author Bob Tarling
 60  
  */
 61  900
 abstract class MemberFilePersister {
 62  
     /**
 63  
      * Load a project member from an InputStream.
 64  
      *
 65  
      * @param project the project to persist
 66  
      * @param inputStream the inputStream to parse to load the member.
 67  
      * @throws OpenException on any parsing errors.
 68  
      */
 69  
     public abstract void load(Project project, InputStream inputStream)
 70  
         throws OpenException;
 71  
 
 72  
     /**
 73  
      * Load a project member from a URL.
 74  
      *
 75  
      * @param project the project to persist
 76  
      * @param url the URL to open and parse to load the member.
 77  
      * @throws OpenException on any parsing errors.
 78  
      */
 79  
     public abstract void load(Project project, URL url)
 80  
         throws OpenException;
 81  
 
 82  
     /**
 83  
      * Load a project member from a SAX InputSource.
 84  
      *
 85  
      * @param project the project to persist
 86  
      * @param inputSource the InputSource to load from
 87  
      * @throws OpenException on any parsing errors.
 88  
      * @since 0.29.1
 89  
      */
 90  
     public abstract void load(Project project, InputSource inputSource)
 91  
         throws OpenException;
 92  
     
 93  
     
 94  
     /**
 95  
      * Gets the tag name which is the root tag for this member.
 96  
      * @return tag name.
 97  
      */
 98  
     public abstract String getMainTag();
 99  
 
 100  
 
 101  
     /**
 102  
      * Save the project member as XML to the given output stream.
 103  
      * 
 104  
      * @param member
 105  
      *            The project member to save.
 106  
      * @param stream
 107  
      *            The OutputStream to write the contents to.
 108  
      * @throws SaveException
 109  
      *             if the save fails
 110  
      * @since 0.25.4
 111  
      */
 112  
     public abstract void save(
 113  
             ProjectMember member,
 114  
             OutputStream stream) throws SaveException;
 115  
     
 116  
     
 117  
     /**
 118  
      * Send an existing file of XML to the PrintWriter.
 119  
      * @param writer the PrintWriter.
 120  
      * @param file the File
 121  
      * @throws SaveException on any errors.
 122  
      */
 123  
     protected void addXmlFileToWriter(PrintWriter writer, File file)
 124  
         throws SaveException {
 125  
         try {
 126  0
             BufferedReader reader =
 127  
                 new BufferedReader(
 128  
                         new InputStreamReader(
 129  
                                 new FileInputStream(file), 
 130  
                                 Argo.getEncoding()));
 131  
 
 132  
             // Skip the <?xml... first line
 133  0
             String line = reader.readLine();
 134  0
             while (line != null && (line.startsWith("<?xml ")
 135  
                     || line.startsWith("<!DOCTYPE "))) {
 136  0
                 line = reader.readLine();
 137  
             }
 138  
 
 139  0
             while (line != null) {
 140  0
                 (writer).println(line);
 141  0
                 line = reader.readLine();
 142  
             }
 143  0
             reader.close();
 144  0
         } catch (FileNotFoundException e) {
 145  0
             throw new SaveException(e);
 146  0
         } catch (IOException e) {
 147  0
             throw new SaveException(e);
 148  0
         }
 149  0
     }
 150  
 
 151  
 }