Re-implemented taxonomic tree using Common Navigator Framework.
[taxeditor.git] / taxeditor-store / src / main / java / eu / etaxonomy / taxeditor / store / parser / CdmParserUtil.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.store.parser;
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 import eu.etaxonomy.taxeditor.store.preference.PreferencesUtil;
21
22 /**
23 * @author p.ciardelli
24 * @created 22.12.2008
25 * @version 1.0
26 */
27 public class CdmParserUtil {
28 private static final Logger logger = Logger
29 .getLogger(CdmParserUtil.class);
30
31 private static INonViralNameParser nonViralNameParser;
32
33 private static INonViralNameParser getNonViralNameParser() {
34 if (nonViralNameParser == null) {
35 nonViralNameParser = NonViralNameParserImpl.NewInstance();
36 }
37 return nonViralNameParser;
38 }
39
40 public static void clearNonViralNameParser() {
41 nonViralNameParser = null;
42 }
43
44 /**
45 * Takes a raw string, returns a brand spanking new name object.
46 * <p>
47 * If no nomenclatural code is specified, the code specified in user preferences
48 * is used, i.e. if the preferred code is ICBN, a <code>BotanicalName</code> is returned;
49 * if it's ICZN, a <code>ZoologicalName</code> is returned.
50 * </p>
51 *
52 * @param fullReference
53 * @param nomCode
54 * @param rank
55 * @return
56 */
57 public static TaxonNameBase parseFullReference(String fullReference,
58 NomenclaturalCode nomCode, Rank rank) {
59
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 }