1   /*
2    * LinearNode.java
3    *
4    * Copyright (c) 2002, The University of Sheffield.
5    *
6    * This file is part of GATE (see http://gate.ac.uk/), and is free
7    * software, licenced under the GNU Library General Public License,
8    * Version 2, June1991.
9    *
10   * A copy of this licence is included in the distribution in the file
11   * licence.html, and is also available at http://gate.ac.uk/gate/licence.html.
12   *
13   * borislav popov 02/2002
14   *
15   */
16  package gate.creole.gazetteer;
17  
18  import gate.creole.gazetteer.*;
19  
20  
21  /**Linear node specifies an entry of the type :
22   * list:major:minor:language */
23  public class LinearNode {
24  
25    /** the gazetteer list from the node */
26    private String list;
27    /** the minor type from the node */
28    private String minor;
29    /** the major type from the node */
30    private String major;
31    /** the languages member from the node */
32    private String language;
33  
34    /**
35     * Constructs a linear node given its elements
36     * @param aList the gazetteer list file name
37     * @param aMajor the major type
38     * @param aMinor the minor type
39     * @param aLanguage the language(s)
40     */
41    public LinearNode(String aList,String aMajor,String aMinor, String aLanguage) {
42      list = aList;
43      minor = aMinor;
44      major = aMajor;
45      language = aLanguage;
46    } // LinearNode construct
47  
48    /**
49     * Parses and create a linear node from a string
50     * @param node the linear node to be parsed
51     * @throws InvalidFormatException
52     */
53    public LinearNode (String node) throws InvalidFormatException  {
54      int firstColon = node.indexOf(':');
55      int secondColon = node.indexOf(':', firstColon + 1);
56      int thirdColon = node.indexOf(':', secondColon + 1);
57      if(firstColon == -1){
58        throw new InvalidFormatException("", "Line: " + node);
59      }
60      list = node.substring(0, firstColon);
61  
62      if(secondColon == -1){
63        major = node.substring(firstColon + 1);
64        minor = null;
65        language = null;
66      } else {
67        major = node.substring(firstColon + 1, secondColon);
68        if(thirdColon == -1) {
69          minor = node.substring(secondColon + 1);
70          language = null;
71        } else {
72          minor = node.substring(secondColon + 1, thirdColon);
73          language = node.substring(thirdColon + 1);
74        }
75      } // else
76    } // LinearNode concstruct
77  
78    /**Get the gazetteer list filename from the node
79     * @return the gazetteer list filename */
80    public String getList() {
81      return list;
82    }
83  
84    /**Sets the gazetteer list filename for the node
85     * @param aList  the gazetteer list filename*/
86    public void setList(String aList) {
87      list = aList;
88    }
89  
90    /** Gets the language of the node (the language is optional)
91     *  @return the language of the node */
92    public String getLanguage() {
93      return language;
94    }
95  
96    /** Sets the language of the node
97     *  @param aLanguage the language of the node */
98    public void setLanguage(String aLanguage) {
99      language = aLanguage;
100   }
101 
102   /** Gets the minor type
103    *  @return the minor type  */
104   public String getMinorType() {
105     return minor;
106   }
107 
108   /** Sets the minor type
109    *  @return the minor type */
110   public void setMinorType(String minorType) {
111     minor = minorType;
112   }
113 
114   /** Gets the major type
115    *  @return the major type*/
116   public String getMajorType() {
117     return major;
118   }
119 
120   /** Sets the major type
121    *  @param majorType the major type */
122   public void setMajorType(String majorType) {
123     major = majorType;
124   }
125 
126   /**
127    * Gets the string representation of this node
128    * @return the string representation of this node
129    */
130   public String toString() {
131     String result = list+':'+major;
132 
133     if ( (null!=minor)  && (0 != minor.length()))
134       result += ':'+minor;
135 
136     if ( (null!=language) && (0 != language.length())) {
137       if ((null==minor) || (0 == minor.length()) )
138         result +=':';
139       result += ':'+language;
140     }
141     return result;
142   }
143 
144   /**Checks this node vs another one for equality.
145    * @param o another node
146    * @return true if languages,list,major type and minor type match.*/
147   public boolean equals(Object o) {
148      boolean result = false;
149      if ( o instanceof LinearNode ) {
150       LinearNode node = (LinearNode) o;
151       result = true;
152 
153       if (null != this.getLanguage())
154         result &= this.getLanguage().equals(node.getLanguage());
155 
156       if ( null != this.getList())
157         result &= this.getList().equals(node.getList());
158 
159       if ( null!=this.getMajorType())
160         result &= this.getMajorType().equals(node.getMajorType());
161 
162       if ( null!= this.getMinorType())
163         result &= this.getMinorType().equals(node.getMinorType());
164      }
165      return result;
166   }
167 
168 } // class LinearNode