Moving editor sources back into trunk
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / editor / name / CdmParserController.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.taxeditor.editor.name;
11
12 import org.apache.log4j.Logger;
13
14 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
15 import eu.etaxonomy.cdm.model.name.NonViralName;
16 import eu.etaxonomy.cdm.model.name.Rank;
17 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
18 import eu.etaxonomy.cdm.strategy.parser.INonViralNameParser;
19 import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
20
21 /**
22 * @author p.ciardelli
23 * @created 22.12.2008
24 * @version 1.0
25 */
26 public class CdmParserController {
27 private static final Logger logger = Logger
28 .getLogger(CdmParserController.class);
29
30 private static INonViralNameParser nonViralNameParser;
31
32 private static INonViralNameParser getNonViralNameParser() {
33 if (nonViralNameParser == null) {
34 nonViralNameParser = NonViralNameParserImpl.NewInstance();
35 }
36 return nonViralNameParser;
37 }
38
39 public static void clearNonViralNameParser() {
40 nonViralNameParser = null;
41 }
42
43 /**
44 * Takes a raw string, returns a brand spanking new name object.
45 * <p>
46 * If no nomenclatural code is specified, the code specified in user preferences
47 * is used, i.e. if the preferred code is ICBN, a <code>BotanicalName</code> is returned;
48 * if it's ICZN, a <code>ZoologicalName</code> is returned.
49 * </p>
50 *
51 * @param fullReference
52 * @param nomCode
53 * @param rank
54 * @return
55 */
56 public static TaxonNameBase parseFullReference(String fullReference,
57 NomenclaturalCode nomCode, Rank rank) {
58
59 // FIXME whatever needs to be fixed here
60 // if (nomCode == null) {
61 // nomCode = PreferencesUtil.getPreferredNomenclaturalCode();
62 // }
63
64 TaxonNameBase name = getNonViralNameParser().parseReferencedName(fullReference,
65 nomCode, rank);
66
67 if (name.hasProblem()) {
68 name.setFullTitleCache(fullReference);
69 }
70
71 return name;
72 }
73
74 /**
75 * Takes an existing name object and a string, returns the same name
76 * object with the fields parsed from the string.
77 *
78 * @param nameToBeFilled
79 * @param fullReference
80 * @param rank
81 * @param makeEmpty
82 */
83 public static void parseFullReference(NonViralName nameToBeFilled, String fullReference) {
84
85 Rank rank = null;
86 boolean makeEmpty = true;
87
88 // If the name already has a rank, make sure it is passed to the parser
89 if (nameToBeFilled.getRank() != null) {
90 rank = nameToBeFilled.getRank();
91 }
92
93 getNonViralNameParser().parseReferencedName(nameToBeFilled, fullReference, rank, makeEmpty);
94
95 if (nameToBeFilled.hasProblem()) {
96 nameToBeFilled.setFullTitleCache(fullReference);
97 }
98 }
99
100 /**
101 * @param nameToBeFilled
102 * @param fullNameString
103 * @param rank
104 * @param makeEmpty
105 */
106 public static void parseFullName(NonViralName nameToBeFilled,
107 String fullNameString, Rank rank, boolean makeEmpty) {
108 getNonViralNameParser().parseFullName(nameToBeFilled, fullNameString,
109 rank, makeEmpty);
110 }
111
112 }