package dnb.analyze.filetree;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import dnb.data.CatalogNumber;
import dnb.data.impl.CatalogNumberHibernateImpl;
class LabelcodeMatcher {
private static final Pattern LBLCODE1 = Pattern.compile("(\\p{Alpha}{2,})(\\d{1,})(\\p{Alpha}*\\d*)");
private static final Pattern LBLCODE2 = Pattern.compile("(\\p{Alpha}{2,})\\s*(\\d{1,})(\\p{Alpha}*\\d*)");
private static final Pattern LBLCODE3 = Pattern.compile("(\\p{Alnum}+\\p{Alpha}+)\\s*(\\d{1,})(\\p{Alpha}*\\d*)");
static CatalogNumberHibernateImpl findLabelcode2(String n) {
Matcher m = LBLCODE2.matcher(n);
if (m.find()) {
return new CatalogNumberHibernateImpl(m.group(1), Integer.parseInt(m.group(2)), m.group(3));
}
return null;
}
static CatalogNumber findLabelcode3(String n) {
Matcher m = LBLCODE3.matcher(n);
if (m.find()) {
return new CatalogNumberHibernateImpl(m.group(1), Integer.parseInt(m.group(2)), m.group(3));
}
return null;
}
static CatalogNumberHibernateImpl findLabelcode(String n) {
Matcher m = LBLCODE1.matcher(n);
List<String> cands = new ArrayList<String>();
while(m.find()) {
cands.add(m.group());
}
// simple impl.: get the longest entry
String ret = null;
for (String s : cands) {
if (ret == null || ret.length() < s.length()) {
ret = s;
}
}
// rebuild matcher for capturing
if (ret == null) {
return null;
}
m = LBLCODE1.matcher(ret);
m.find();
return new CatalogNumberHibernateImpl(m.group(1), Integer.parseInt(m.group(2)), m.group(3));
}
}