View Javadoc

1   /**
2   The contents of this file are subject to the Mozilla Public License Version 1.1 
3   (the "License"); you may not use this file except in compliance with the License. 
4   You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
5   Software distributed under the License is distributed on an "AS IS" basis, 
6   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
7   specific language governing rights and limitations under the License. 
8   
9   The Original Code is "ProfileName.java".  Description: 
10  "This Class represents the name of a conformance class. It is used to generate
11  names for classes, accessors for those classes, member variable to hold those
12  classes, etc. It is also responsable for tracking the names of all siblings
13  within a package, to ensure that no two classes have the same name."
14  
15  The Initial Developer of the Original Code is University Health Network. Copyright (C) 
16  2001.  All Rights Reserved. 
17  
18  Contributor(s): James Agnew
19                  Paul Brohman
20                  Mitch Delachevrotiere
21                  Shawn Dyck
22    				Cory Metcalf
23    				
24  Alternatively, the contents of this file may be used under the terms of the 
25  GNU General Public License (the  ?GPL?), in which case the provisions of the GPL are 
26  applicable instead of those above.  If you wish to allow use of your version of this 
27  file only under the terms of the GPL and not to allow others to use your version 
28  of this file under the MPL, indicate your decision by deleting  the provisions above 
29  and replace  them with the notice and other provisions required by the GPL License.  
30  If you do not delete the provisions above, a recipient may use your version of 
31  this file under either the MPL or the GPL. 
32  
33  */
34  package ca.uhn.hl7v2.conf.classes.generator.builders;
35  
36  import java.util.HashMap;
37  
38  import ca.uhn.hl7v2.sourcegen.SourceGenerator;
39  
40  /** This Class represents the name of a conformance class. It is used to generate
41   * names for classes, accessors for those classes, member variable to hold those
42   * classes, etc. It is also responsable for tracking the names of all siblings
43   * within a package, to ensure that no two classes have the same name.
44   * @author <table><tr>James Agnew</tr>
45   *                <tr>Paul Brohman</tr>
46   *                <tr>Mitch Delachevrotiere</tr>
47   *                <tr>Shawn Dyck</tr>
48   * 				  <tr>Cory Metcalf</tr></table>
49   */
50  public final class ProfileName {
51  
52     /** Integer value representing the <code>profileName</code> value for Component */
53     public static final int PS_COMP = 4;
54  
55     /** Integer value representing the <code>profileName</code> value for Field */
56     public static final int PS_FIELD = 3;
57  
58     /** Integer value representing the <code>profileName</code> value for Message */
59     public static final int PS_MSG = 0;
60  
61     /** Integer value representing the <code>profileName</code> value for Segment */
62     public static final int PS_SEG = 2;
63  
64     /** Integer value representing the <code>profileName</code> value for SegGroup */
65     public static final int PS_SEGG = 1;
66  
67     /** Integer value representing the <code>profileName</code> value for SubComponent */
68     public static final int PS_SUBC = 5;
69  
70     private static final String[] PS_TYPES = { "Msg", "SegGrp", "Seg", "Field", "Comp", "SubComp" };
71     protected HashMap<String, Object> nameMap; // Map containing the name of the parent and all children at any given level
72     private String parentName; // The name of the parent name assosiated with this ProfileName
73  
74     private String profileName; // The name that this ProfileName represents
75     private int profileStructureType; // The HL7 structure level that this ProfileName represents
76  
77     /** Creates a new instance of ProfileName 
78      * @param profileName the name of the profile
79      * @param profileStructureType The Profile Structure Type for this Name. The
80      * Profile Structure Type is prepended to each class in the class hierarchy
81      * both for clarity and to avoid name collisions.
82      */
83     public ProfileName(String profileName, int profileStructureType) {
84        this(profileName, profileStructureType, new HashMap<String, Object>(50), profileName);
85     }
86  
87     /** Creates a new instance of ProfileName 
88      * @param profileName the name of the profile
89      * @param profileStructureType The Profile Structure Type for this Name. The
90      * Profile Structure Type is prepended to each class in the class hierarchy
91      * both for clarity and to avoid name collisions.
92      * @param nameMap a list of all the children in ProfileName
93      * @param parentName the name of the parent to the child 
94      */
95     private ProfileName(String profileName, int profileStructureType, HashMap<String, Object> nameMap, String parentName) {
96        this.profileName = new String(profileName);
97        this.profileStructureType = profileStructureType;
98        this.nameMap = nameMap;
99        this.parentName = parentName;
100 
101       // TODO: These are workarounds.. These should probably be resolved somehow.
102       if (profileName.equals("Acknowledgment Code"))
103          this.profileName = "Acknowledgement Code";
104       if (this.parentName != null && this.parentName.equals("NK1") && profileName.equals("Name"))
105          this.profileName = "NKName";
106 
107       // Append a number to the name if there is already a ProfileName with this particular name
108       int i = 1;
109       while (nameMap.containsKey(this.profileStructureType + this.profileName)) {
110          i++;
111          this.profileName = new String(profileName + i);
112       }
113 
114       // Store the new name in the hashmap 
115       nameMap.put(this.profileStructureType + this.profileName, null);
116    }
117 
118    /** This method clears the name map for this Profile Name, meaning that new names
119     * entered after the map was cleared will not collide with names entered
120     * before. This should be used when a ProfileName object is passed to a child
121     * builder class.
122     * @return ProfileName Returns the ProfileName object
123     */
124    public ProfileName clearNameMap() {
125       this.nameMap = new HashMap<String, Object>(50);
126       nameMap.put(this.profileStructureType + this.profileName, null);
127       this.parentName = this.profileName;
128       return this;
129    }
130 
131    /** Compares two ProfileName objects by comparing their <code>getAccessorName()</code>
132     * method return value.
133     * @return true if the names are equivalent
134     */
135    public boolean equals(ProfileName n) {
136       if (getAccessorName().equals(n.getAccessorName()))
137          return true;
138       else
139          return false;
140    }
141 
142    /** Returns the accessor name
143     * @return the Accessor name
144     */
145    public String getAccessorName() {
146       // SegGroup names are a special case because makeAccessorName() chews out the
147       // underscores they have to contain.
148       if (profileStructureType == PS_SEGG)
149          return "get" + this.profileName;
150       else
151          return "get" + (SourceGenerator.makeAccessorName(this.profileName, this.parentName));
152    }
153 
154    /** Returns the Class name
155     * @return the Class name
156     */
157    public String getClassName() {
158       String name = getAccessorName().substring(3, 4).toUpperCase() + getAccessorName().substring(4);
159       return PS_TYPES[profileStructureType] + name;
160    }
161 
162    /** Returns the Member name
163     * @return the Member name
164     */
165    public String getMemberName() {
166       String memberName = getAccessorName().substring(3, 4).toLowerCase() + getAccessorName().substring(4);
167       if (!Character.isJavaIdentifierStart(memberName.charAt(0)))
168          memberName = "_" + memberName;
169 
170       return memberName;
171    }
172 
173    /** Returns the original name
174     * @return the original name
175     */
176    public String getOriginalName() {
177       return this.profileName;
178    }
179 
180    /** Returns the Package name
181     * @return the Package name
182     */
183    public String getPackageName() {
184       return getAccessorName().substring(3) + "children";
185    }
186 
187    /** creates a new instance of ProfileName
188     * @return the new ProfileName
189     */
190    public ProfileName newInstance(String profileName, int profileStructureType) {
191       ProfileName newPN = new ProfileName(profileName, profileStructureType, this.nameMap, this.profileName);
192       return newPN;
193    }
194 
195 }