View Javadoc

1   /*
2    * Zemucan: A Syntax Assistant for DB2
3    * Copyright (C) 2009, 2010 Andres Gomez Casanova
4    *
5    * This file is part of Zemucan.
6    *
7    * Zemucan is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as published by
9    * the Free Software Foundation; either version 3 of the License, or
10   * (at your option) any later version.
11   *
12   * Zemucan is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public License
18   * along with this library; if not, see <http://www.gnu.org/licenses/>.
19   *
20   * Contact:
21   * a n g o c a  at  y a h o o  dot  c o m
22   * Cra. 45 No 61 - 31, Bogota, Colombia.
23   *
24   * Author:   $LastChangedBy: angoca $:
25   * Date:     $LastChangedDate: 2011-03-06 10:15:32 -0500 (dom, 06 mar 2011) $:
26   * Revision: $LastChangedRevision: 1913 $:
27   * URL:      $HeadURL: https://zemucan.svn.sourceforge.net/svnroot/zemucan/branches/zemucan_v1/source-code/grammarReaderApi/src/main/java/name/angoca/zemucan/grammarReader/api/AbstractTwoPhaseGrammarReader.java $:
28   */
29  package name.angoca.zemucan.grammarReader.api;
30  
31  import java.io.File;
32  import java.net.URISyntaxException;
33  import java.net.URL;
34  
35  import name.angoca.zemucan.AbstractZemucanException;
36  
37  /**
38   * Represents the grammar read done in two phases. First one is to detect nodes,
39   * and the second one to establish relations between them.
40   * <p>
41   * <b>Control Version</b>
42   * <p>
43   * <ul>
44   * <li>1.0.0 Class creation</li>
45   * </ul>
46   *
47   * @author Andres Gomez Casanova <a
48   *         href="mailto:a n g o c a at y a h o o dot c o m" >(AngocA)</a>
49   * @version 1.0.0 2010-10-26
50   */
51  public abstract class AbstractTwoPhaseGrammarReader extends
52          AbstractGrammarReader {
53  
54      /**
55       * File descriptor.
56       */
57      protected String fileDescriptor;
58  
59      /**
60       * Constructor.
61       */
62      public AbstractTwoPhaseGrammarReader() {
63          super();
64      }
65  
66      /**
67       * Scans the document where the nodes are described, and creates the objects
68       * that represents the nodes according to its type.
69       * <p>
70       * This phase only creates independent nodes. The second phase creates the
71       * relations between them.
72       *
73       * @throws AbstractZemucanException
74       */
75      abstract protected void firstPhase() throws AbstractZemucanException;
76  
77      /**
78       * Retrieves the set of file names described in the descriptor.
79       *
80       * @return Set of file names.
81       * @throws GeneralGrammarReaderProblemException
82       *             If there is a problem interpreting the URL that represents
83       *             the file names.
84       */
85      protected String[] getFileNamesFromDescriptor()
86              throws GeneralGrammarReaderProblemException {
87          assert this.fileDescriptor != null;
88  
89          String[] files = new String[0];
90          final ClassLoader loader = ClassLoader.getSystemClassLoader();
91          if (loader != null) {
92              final URL url = loader.getResource(this.fileDescriptor);
93              if ((url != null) && url.getProtocol().equals("file")) {
94                  File file = null;
95                  try {
96                      file = new File(url.toURI());
97                  } catch (final URISyntaxException exception) {
98                      throw new GeneralGrammarReaderProblemException(exception);
99                  }
100                 if (file.isDirectory()) {
101                     files = file.list();
102                 } // else The file represents one file.
103             }// else The url is not for a file.
104         } else {
105             assert false;
106         }
107         return files;
108     }
109 
110     /**
111      * Second phase of the process where the relations between nodes are
112      * established.
113      *
114      * @throws AbstractZemucanException
115      *             If there is a problem while regarding the nodes or
116      */
117     abstract protected void secondPhase() throws AbstractZemucanException;
118 
119 }