Coverage Report - org.argouml.persistence.ArgoParser
 
Classes in this File Line Coverage Branch Coverage Complexity
ArgoParser
0%
0/174
0%
0/41
2.189
 
 1  
 /* $Id: ArgoParser.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-2008 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.Reader;
 42  
 import java.util.ArrayList;
 43  
 import java.util.List;
 44  
 
 45  
 import org.apache.log4j.Logger;
 46  
 import org.argouml.kernel.Project;
 47  
 import org.argouml.kernel.ProjectSettings;
 48  
 import org.argouml.notation.NotationSettings;
 49  
 import org.argouml.uml.diagram.DiagramSettings;
 50  
 import org.xml.sax.InputSource;
 51  
 import org.xml.sax.SAXException;
 52  
 
 53  
 
 54  
 /**
 55  
  * Parser for ArgoUML project description file (.argo)
 56  
  */
 57  
 class ArgoParser extends SAXParserBase {
 58  
 
 59  
     /**
 60  
      * Logger.
 61  
      */
 62  0
     private static final Logger LOG = Logger.getLogger(ArgoParser.class);
 63  
 
 64  
     private Project project;
 65  
 
 66  
     private ProjectSettings ps;
 67  
     
 68  
     private DiagramSettings diagramDefaults;
 69  
     
 70  
     private NotationSettings notationSettings;
 71  
 
 72  0
     private ArgoTokenTable tokens = new ArgoTokenTable();
 73  
 
 74  0
     private List<String> memberList = new ArrayList<String>();
 75  
 
 76  
     /**
 77  
      * The constructor.
 78  
      *
 79  
      */
 80  
     public ArgoParser() {
 81  0
         super();
 82  0
     }
 83  
 
 84  
     /**
 85  
      * @param theProject the project to populate
 86  
      * @param source the input source
 87  
      * @throws SAXException on error when parsing xml
 88  
      */
 89  
     public void readProject(Project theProject, InputSource source)
 90  
         throws SAXException {
 91  
 
 92  0
         if (source == null) {
 93  0
             throw new IllegalArgumentException(
 94  
                     "An InputSource must be supplied");
 95  
         }
 96  
 
 97  0
         preRead(theProject);
 98  
 
 99  
         try {
 100  0
             parse(source);
 101  0
         } catch (SAXException e) {
 102  0
             logError(source.toString(), e);
 103  0
             throw e;
 104  0
         }
 105  0
     }
 106  
 
 107  
     /**
 108  
      * @param theProject the project to populate
 109  
      * @param reader the reader
 110  
      * @throws SAXException on error when parsing xml
 111  
      */
 112  
     public void readProject(Project theProject, Reader reader)
 113  
             throws SAXException {
 114  
 
 115  0
         if (reader == null) {
 116  0
             throw new IllegalArgumentException(
 117  
                     "A reader must be supplied");
 118  
         }
 119  
 
 120  0
         preRead(theProject);
 121  
 
 122  
         try {
 123  0
             parse(reader);
 124  0
         } catch (SAXException e) {
 125  0
             logError(reader.toString(), e);
 126  0
             throw e;
 127  0
         }
 128  0
     }
 129  
 
 130  
     private void preRead(Project theProject) {
 131  0
         LOG.info("=======================================");
 132  0
         LOG.info("== READING PROJECT " + theProject);
 133  0
         project = theProject;
 134  0
         ps = project.getProjectSettings();
 135  0
         diagramDefaults = ps.getDefaultDiagramSettings();
 136  0
         notationSettings = ps.getNotationSettings();
 137  0
     }
 138  
 
 139  
     private void logError(String projectName, SAXException e) {
 140  0
         LOG.error("Exception reading project================", e);
 141  0
         LOG.error(projectName);
 142  0
     }
 143  
 
 144  
     /**
 145  
      * Get the project to which the URI is to be parsed.
 146  
      * @return the project
 147  
      */
 148  
     public Project getProject() {
 149  0
         return project;
 150  
     }
 151  
 
 152  
     /**
 153  
      * Set the project to which the URI is to be parsed.
 154  
      * @param newProj the project
 155  
      */
 156  
     public void setProject(Project newProj) {
 157  0
         project = newProj;
 158  0
         ps = project.getProjectSettings();
 159  0
     }
 160  
 
 161  
     /*
 162  
      * @see org.argouml.persistence.SAXParserBase#handleStartElement(
 163  
      *         org.argouml.persistence.XMLElement)
 164  
      */
 165  
     public void handleStartElement(XMLElement e) throws SAXException {
 166  
         if (DBG) {
 167  
             LOG.debug("NOTE: ArgoParser handleStartTag:" + e.getName());
 168  
         }
 169  0
         switch (tokens.toToken(e.getName(), true)) {
 170  
         case ArgoTokenTable.TOKEN_ARGO:
 171  0
             handleArgo(e);
 172  0
             break;
 173  
         case ArgoTokenTable.TOKEN_DOCUMENTATION:
 174  0
             handleDocumentation(e);
 175  0
             break;
 176  
         case ArgoTokenTable.TOKEN_SETTINGS:
 177  0
             handleSettings(e);
 178  0
             break;
 179  
         default:
 180  
             if (DBG) {
 181  
                 LOG.warn("WARNING: unknown tag:" + e.getName());
 182  
             }
 183  
             break;
 184  
         }
 185  0
     }
 186  
 
 187  
     /*
 188  
      * @see org.argouml.persistence.SAXParserBase#handleEndElement(
 189  
      *         org.argouml.persistence.XMLElement)
 190  
      */
 191  
     @SuppressWarnings("deprecation")
 192  
     public void handleEndElement(XMLElement e) throws SAXException {
 193  
         if (DBG) {
 194  
             LOG.debug("NOTE: ArgoParser handleEndTag:" + e.getName() + ".");
 195  
         }
 196  0
         switch (tokens.toToken(e.getName(), false)) {
 197  
         case ArgoTokenTable.TOKEN_MEMBER:
 198  0
             handleMember(e);
 199  0
             break;
 200  
         case ArgoTokenTable.TOKEN_AUTHORNAME:
 201  0
             handleAuthorName(e);
 202  0
             break;
 203  
         case ArgoTokenTable.TOKEN_AUTHOREMAIL:
 204  0
             handleAuthorEmail(e);
 205  0
             break;
 206  
         case ArgoTokenTable.TOKEN_VERSION:
 207  0
             handleVersion(e);
 208  0
             break;
 209  
         case ArgoTokenTable.TOKEN_DESCRIPTION:
 210  0
             handleDescription(e);
 211  0
             break;
 212  
         case ArgoTokenTable.TOKEN_SEARCHPATH:
 213  0
             handleSearchpath(e);
 214  0
             break;
 215  
         case ArgoTokenTable.TOKEN_HISTORYFILE:
 216  0
             handleHistoryfile(e);
 217  0
             break;
 218  
         case ArgoTokenTable.TOKEN_NOTATIONLANGUAGE:
 219  0
             handleNotationLanguage(e);
 220  0
             break;
 221  
         case ArgoTokenTable.TOKEN_SHOWBOLDNAMES:
 222  0
             handleShowBoldNames(e);
 223  0
             break;
 224  
         case ArgoTokenTable.TOKEN_USEGUILLEMOTS:
 225  0
             handleUseGuillemots(e);
 226  0
             break;
 227  
         case ArgoTokenTable.TOKEN_SHOWVISIBILITY:
 228  0
             handleShowVisibility(e);
 229  0
             break;
 230  
         case ArgoTokenTable.TOKEN_SHOWMULTIPLICITY:
 231  0
             handleShowMultiplicity(e);
 232  0
             break;
 233  
         case ArgoTokenTable.TOKEN_SHOWINITIALVALUE:
 234  0
             handleShowInitialValue(e);
 235  0
             break;
 236  
         case ArgoTokenTable.TOKEN_SHOWPROPERTIES:
 237  0
             handleShowProperties(e);
 238  0
             break;
 239  
         case ArgoTokenTable.TOKEN_SHOWTYPES:
 240  0
             handleShowTypes(e);
 241  0
             break;
 242  
         case ArgoTokenTable.TOKEN_SHOWSTEREOTYPES:
 243  0
             handleShowStereotypes(e);
 244  0
             break;
 245  
         case ArgoTokenTable.TOKEN_SHOWSINGULARMULTIPLICITIES:
 246  0
             handleShowSingularMultiplicities(e);
 247  0
             break;
 248  
         case ArgoTokenTable.TOKEN_DEFAULTSHADOWWIDTH:
 249  0
             handleDefaultShadowWidth(e);
 250  0
             break;
 251  
         case ArgoTokenTable.TOKEN_FONTNAME:
 252  0
             handleFontName(e);
 253  0
             break;
 254  
         case ArgoTokenTable.TOKEN_FONTSIZE:
 255  0
             handleFontSize(e);
 256  0
             break;
 257  
         case ArgoTokenTable.TOKEN_GENERATION_OUTPUT_DIR:
 258  
             // ignored - it shouldn't have been in the project in the 1st place
 259  0
             break;
 260  
         case ArgoTokenTable.TOKEN_SHOWASSOCIATIONNAMES:
 261  0
             handleShowAssociationNames(e);
 262  0
             break;
 263  
         case ArgoTokenTable.TOKEN_HIDEBIDIRECTIONALARROWS:
 264  0
             handleHideBidirectionalArrows(e);
 265  0
             break;
 266  
         case ArgoTokenTable.TOKEN_ACTIVE_DIAGRAM:
 267  0
             handleActiveDiagram(e);
 268  0
             break;
 269  
         default:
 270  
             if (DBG) {
 271  
                 LOG.warn("WARNING: unknown end tag:" + e.getName());
 272  
             }
 273  
             break;
 274  
         }
 275  0
     }
 276  
 
 277  
     /*
 278  
      * @see org.argouml.persistence.SAXParserBase#isElementOfInterest(String)
 279  
      */
 280  
     @Override
 281  
     protected boolean isElementOfInterest(String name) {
 282  0
         return tokens.contains(name);
 283  
     }
 284  
 
 285  
     /**
 286  
      * @param e the element
 287  
      */
 288  
     protected void handleArgo(@SuppressWarnings("unused") XMLElement e) {
 289  
         /* do nothing */
 290  0
     }
 291  
 
 292  
     /**
 293  
      * @param e the element
 294  
      */
 295  
     protected void handleDocumentation(
 296  
             @SuppressWarnings("unused") XMLElement e) {
 297  
         /* do nothing */
 298  0
     }
 299  
 
 300  
     /**
 301  
      * @param e the element
 302  
      */
 303  
     protected void handleSettings(@SuppressWarnings("unused") XMLElement e) {
 304  
         /* do nothing */
 305  0
     }
 306  
 
 307  
     /**
 308  
      * @param e the element
 309  
      */
 310  
     protected void handleAuthorName(XMLElement e) {
 311  0
         String authorname = e.getText().trim();
 312  0
         project.setAuthorname(authorname);
 313  0
     }
 314  
 
 315  
     /**
 316  
      * @param e the element
 317  
      */
 318  
     protected void handleAuthorEmail(XMLElement e) {
 319  0
         String authoremail = e.getText().trim();
 320  0
         project.setAuthoremail(authoremail);
 321  0
     }
 322  
 
 323  
     /**
 324  
      * @param e the element
 325  
      */
 326  
     protected void handleVersion(XMLElement e) {
 327  0
         String version = e.getText().trim();
 328  0
         project.setVersion(version);
 329  0
     }
 330  
 
 331  
     /**
 332  
      * @param e the element
 333  
      */
 334  
     protected void handleDescription(XMLElement e) {
 335  0
         String description = e.getText().trim();
 336  0
         project.setDescription(description);
 337  0
     }
 338  
 
 339  
     /**
 340  
      * @param e the element
 341  
      */
 342  
     protected void handleSearchpath(XMLElement e) {
 343  0
         String searchpath = e.getAttribute("href").trim();
 344  0
         project.addSearchPath(searchpath);
 345  0
     }
 346  
 
 347  
     /**
 348  
      * @param e the element
 349  
      * @throws SAXException on any error parsing the member XML.
 350  
      */
 351  
     protected void handleMember(XMLElement e) throws SAXException {
 352  0
         if (e == null) {
 353  0
             throw new SAXException("XML element is null");
 354  
         }
 355  0
         String type = e.getAttribute("type");
 356  0
         memberList.add(type);
 357  0
     }
 358  
 
 359  
     /**
 360  
      * @param e the element
 361  
      */
 362  
     protected void handleHistoryfile(XMLElement e) {
 363  0
         if (e.getAttribute("name") == null) {
 364  0
             return;
 365  
         }
 366  0
         String historyfile = e.getAttribute("name").trim();
 367  0
         project.setHistoryFile(historyfile);
 368  0
     }
 369  
 
 370  
     /**
 371  
      * @param e the element
 372  
      */
 373  
     protected void handleNotationLanguage(XMLElement e) {
 374  0
         String language = e.getText().trim();
 375  0
         boolean success = ps.setNotationLanguage(language);
 376  
         /* TODO: Here we should e.g. show the user a message that
 377  
          * the loaded project was using a Notation that is not
 378  
          * currently available and a fall back on the default Notation
 379  
          * was done. Maybe this can be implemented in the
 380  
          * PersistenceManager? */
 381  0
     }
 382  
 
 383  
     /**
 384  
      * @param e the element
 385  
      */
 386  
     protected void handleShowBoldNames(XMLElement e) {
 387  0
         String ug = e.getText().trim();
 388  0
         diagramDefaults.setShowBoldNames(Boolean.parseBoolean(ug));
 389  0
     }
 390  
 
 391  
     /**
 392  
      * @param e the element
 393  
      */
 394  
     protected void handleUseGuillemots(XMLElement e) {
 395  0
         String ug = e.getText().trim();
 396  0
         ps.setUseGuillemots(ug);
 397  0
     }
 398  
 
 399  
     /**
 400  
      * @param e the element
 401  
      */
 402  
     protected void handleShowVisibility(XMLElement e) {
 403  0
         String showVisibility = e.getText().trim();
 404  0
         notationSettings.setShowVisibilities(
 405  
                 Boolean.parseBoolean(showVisibility));
 406  0
     }
 407  
 
 408  
     /**
 409  
      * @param e the element
 410  
      */
 411  
     protected void handleShowMultiplicity(XMLElement e) {
 412  0
         String showMultiplicity = e.getText().trim();
 413  0
         notationSettings.setShowMultiplicities(
 414  
                 Boolean.parseBoolean(showMultiplicity));
 415  0
     }
 416  
 
 417  
     /**
 418  
      * @param e the element
 419  
      */
 420  
     protected void handleShowInitialValue(XMLElement e) {
 421  0
         String showInitialValue = e.getText().trim();
 422  0
         notationSettings.setShowInitialValues(
 423  
                 Boolean.parseBoolean(showInitialValue));
 424  0
     }
 425  
 
 426  
     /**
 427  
      * @param e the element
 428  
      */
 429  
     protected void handleShowProperties(XMLElement e) {
 430  0
         String showproperties = e.getText().trim();
 431  0
         notationSettings.setShowProperties(
 432  
                 Boolean.parseBoolean(showproperties));
 433  0
     }
 434  
 
 435  
     /**
 436  
      * @param e the element
 437  
      */
 438  
     protected void handleShowTypes(XMLElement e) {
 439  0
         String showTypes = e.getText().trim();
 440  0
         notationSettings.setShowTypes(Boolean.parseBoolean(showTypes));
 441  0
     }
 442  
 
 443  
     /**
 444  
      * @param e the element
 445  
      */
 446  
     protected void handleShowStereotypes(XMLElement e) {
 447  0
         String showStereotypes = e.getText().trim();
 448  0
         ps.setShowStereotypes(Boolean.parseBoolean(showStereotypes));
 449  0
     }
 450  
 
 451  
     /**
 452  
      * @param e the element
 453  
      */
 454  
     protected void handleShowSingularMultiplicities(XMLElement e) {
 455  0
         String showSingularMultiplicities = e.getText().trim();
 456  0
         notationSettings.setShowSingularMultiplicities(
 457  
                 Boolean.parseBoolean(showSingularMultiplicities));
 458  0
     }
 459  
 
 460  
     /**
 461  
      * @param e the element
 462  
      */
 463  
     protected void handleDefaultShadowWidth(XMLElement e) {
 464  0
         String dsw = e.getText().trim();
 465  0
         diagramDefaults.setDefaultShadowWidth(Integer.parseInt(dsw));
 466  0
     }
 467  
 
 468  
     /**
 469  
      * @param e the element
 470  
      */
 471  
     protected void handleFontName(XMLElement e) {
 472  0
         String dsw = e.getText().trim();
 473  0
         diagramDefaults.setFontName(dsw);
 474  0
     }
 475  
 
 476  
     /**
 477  
      * @param e the element
 478  
      */
 479  
     protected void handleFontSize(XMLElement e) {
 480  0
         String dsw = e.getText().trim();
 481  
         try {
 482  0
             diagramDefaults.setFontSize(Integer.parseInt(dsw));
 483  0
         } catch (NumberFormatException e1) {
 484  0
             LOG.error("NumberFormatException while parsing Font Size", e1);
 485  0
         }
 486  0
     }
 487  
 
 488  
     /**
 489  
      * @param e the element
 490  
      */
 491  
     protected void handleShowAssociationNames(XMLElement e) {
 492  0
         String showAssociationNames = e.getText().trim();
 493  0
         notationSettings.setShowAssociationNames(
 494  
                 Boolean.parseBoolean(showAssociationNames));
 495  0
     }
 496  
 
 497  
     /**
 498  
      * @param e the element
 499  
      */
 500  
     protected void handleHideBidirectionalArrows(XMLElement e) {
 501  0
         String hideBidirectionalArrows = e.getText().trim();
 502  
         // NOTE: For historical reasons true == hide, so we need to invert
 503  
         // the sense of this
 504  0
         diagramDefaults.setShowBidirectionalArrows(!
 505  
                 Boolean.parseBoolean(hideBidirectionalArrows));
 506  0
     }
 507  
     
 508  
     
 509  
     protected void handleActiveDiagram(XMLElement e) {
 510  
         /* At this stage during loading, the diagrams are 
 511  
          * not created yet - so we have to store this name for later use. */
 512  0
         project.setSavedDiagramName(e.getText().trim());
 513  0
     }
 514  
 
 515  
     /**
 516  
      * Get the number of diagram members read.
 517  
      * @return the number of diagram members read.
 518  
      */
 519  
     public List<String> getMemberList() {
 520  0
         return memberList;
 521  
     }
 522  
 }