1 /* Ametros Dive Computer
2 * Copyright (C) 2010 Geoff Johnstone
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net
.ametros
.dive
.parser
;
20 import net
.ametros
.dive
.data
.Ascent
;
21 import net
.ametros
.dive
.data
.Dive
;
22 import net
.ametros
.dive
.data
.Dives
;
23 import net
.ametros
.dive
.data
.Gas
;
24 import net
.ametros
.dive
.data
.ObjectFactory
;
25 import net
.ametros
.dive
.data
.Physiology
;
26 import net
.ametros
.dive
.data
.Stop
;
27 import net
.ametros
.dive
.data
.Water
;
30 import java
.io
.FileInputStream
;
31 import java
.io
.InputStream
;
32 import java
.io
.IOException
;
33 import java
.util
.HashMap
;
34 import java
.util
.List
;
36 import java
.util
.regex
.Matcher
;
37 import java
.util
.regex
.Pattern
;
40 /** Parses dive plans. */
41 public abstract class AbstractParser
implements Parser
43 private static final Map
<String
, Gas
> gasMap
= new HashMap
<String
, Gas
>();
46 for (Gas gas
: Gas
.values())
48 String str
= gas
.toString().toLowerCase();
49 gasMap
.put (str
, gas
);
50 gasMap
.put (str
.replace ('_', '/'), gas
);
51 gasMap
.put (str
.replace ('_', ' '), gas
);
52 gasMap
.put (str
.replace ('_', '-'), gas
);
53 gasMap
.put (str
.replace ("_", "--"), gas
);
55 if (str
.startsWith ("tx_"))
57 str
= str
.substring (3);
58 gasMap
.put (str
.replace ('_', '/'), gas
);
59 gasMap
.put (str
.replace ('_', ' '), gas
);
60 gasMap
.put (str
.replace ('_', '-'), gas
);
61 gasMap
.put (str
.replace ("_", "--"), gas
);
63 else if (str
.startsWith ("nx_"))
65 gasMap
.put ("nx" + str
.substring (3), gas
);
66 gasMap
.put (str
.substring (3) + "% nitrox", gas
);
71 protected final ObjectFactory of
= new ObjectFactory();
76 protected abstract void doParse (File file
) throws Exception
;
80 public Dives
parse (File file
) throws Exception
84 dives
= of
.createDives();
95 protected Gas
parseGas (String in
)
97 final String lower
= in
.toLowerCase();
98 if (gasMap
.containsKey (lower
))
99 return gasMap
.get (lower
);
101 return Gas
.valueOf (in
);
105 protected Dive
createDive (int depth
, int duration
, Gas gas
)
107 final Dive dive
= of
.createDive();
108 dive
.setAscent (of
.createAscent());
109 dive
.setDepth (depth
);
110 dive
.setDuration (duration
);
112 dive
.setPhysiology (createPhysiology());
113 dive
.setWater (createWater());
118 private int getDefault (String property
, int value
)
120 final String str
= System
.getProperty (property
);
121 return (null == str
) ? value
: Integer
.parseInt (str
);
125 protected Gas
getDefaultGas()
127 final String str
= System
.getProperty ("ametros.dive.defaultGas");
128 return (null == str
) ? Gas
.TX_18_45
: parseGas (str
);
132 protected Water
getDefaultWater()
134 final Water water
= of
.createWater();
135 water
.setDensity (getDefault ("ametros.dive.defaultDensity", 1025));
140 protected Water
createWater()
142 return getDefaultWater();
146 protected Physiology
getDefaultPhysiology()
148 final Physiology phys
= of
.createPhysiology();
149 phys
.setSacr (getDefault ("ametros.dive.defaultSacr", 20));
150 phys
.setSaca (getDefault ("ametros.dive.defaultSaca", 30));
155 protected Physiology
createPhysiology()
157 return getDefaultPhysiology();
161 protected void addDives (Dive
[] dives
)
164 for (Dive dive
: dives
)
169 protected void addDives (Iterable
<Dive
> dives
)
172 for (Dive dive
: dives
)
177 protected void addDive (Dive dive
)
179 dives
.getDives().add (dive
);
183 /** Replaces non-breaking spaces with ' '; trims leading/trailing
186 protected String
normaliseWhitespace (char[] in
)
188 final char[] out
= new char[in
.length
];
190 for (int i
= 0; i
< in
.length
; ++i
)
191 out
[i
] = ('\u00a0' == in
[i
]) ?
' ' : in
[i
];
193 return new String (out
).trim();