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