}
public static void parseBack(File dir) throws Exception {
ParsedIndex idx = parse(dir, "index.html", "index", "", ParsedIndex.class);
String lastupdate = idx.getUpdate();
Factbook factbook = obf.createFactbook();
factbook.setLastupdate(toXMLDate(lastupdate));
ParsedNews news = parse(dir, "news.html", "news", "", ParsedNews.class);
for (ParsedNews.News n : news.getNews()) {
Factbook.News nn = new Factbook.News();
nn.setDate(parseDate(n.getDate()));
nn.setValue(n.getValue());
factbook.getNews().add(nn);
}
Map<String, String> countryids = new HashMap<String, String>();
Map<String, String> countrynames = new HashMap<String, String>();
Map<String, List<String>> countryAliases = new HashMap<String, List<String>>();
ParsedCountries ctries = parse(dir, "countries.html", "countries", lastupdate, ParsedCountries.class);
for (ParsedCountries.Country country : ctries.getCountry()) {
countryids.put(country.getName(), country.getId());
countrynames.put(country.getId(), country.getName());
List<String> aliases = countryAliases.get(country.getId());
if (aliases == null) {
aliases = new ArrayList<String>();
countryAliases.put(country.getId(), aliases);
}
aliases.add(country.getAlias() == null ? country.getName() : country.getAlias());
}
ParsedFields flds = parse(dir, "fields.html", "fields", lastupdate, ParsedFields.class);
for (ParsedFields.Category cat : flds.getCategory()) {
Factbook.Category category = obf.createFactbookCategory();
category.setName(cat.getName());
factbook.getCategory().add(category);
category.setDescription(cat.getDescription());
for (ParsedFields.Category.Field fld : cat.getField()) {
Factbook.Category.Field field = obf.createFactbookCategoryField();
String fldname = fld.getName();
String fldid = fld.getId();
field.setId(fldid);
field.setName(fldname);
ParsedFieldDescription flddesc = parse(dir, "fd-" + fldid + ".html", "fielddesc", lastupdate, ParsedFieldDescription.class);
if (!flddesc.getName().equals(fldname) ||
!flddesc.getId().equals(fldid))
throw new RuntimeException(flddesc.getName() + "/" + flddesc.getId());
field.setDescription(flddesc.getDescription());
String unit = flddesc.getUnit();
if (unit != null) {
field.setUnit(unit);
}
if (flddesc.getRank() != null) {
ParsedRankOrder ro = parse(dir, "ro-" + fldid + ".html", "rankorder", lastupdate, ParsedRankOrder.class);
if (!ro.getName().equals(fldname) ||
!ro.getId().equals(fld.getId()))
throw new RuntimeException(ro.getId() + "//" + ro.getName());
for (ParsedRankOrder.Rank r : ro.getRank()) {
Factbook.Category.Field.Rank rank = obf.createFactbookCategoryFieldRank();
rank.setCountry(countryids.get(r.getCountry()));
if (r.getDateEarliest() != null)
rank.setDateEarliest(XMLGregorianCalendarImpl.parse(r.getDateEarliest()));
if (r.getDateLatest() != null)
rank.setDateLatest(XMLGregorianCalendarImpl.parse(r.getDateLatest()));
rank.setDateText(r.getDate());
rank.setDateEstimated(r.isDateEstimated());
String vl = r.getValue();
if (vl.startsWith("$ ")) {
vl = vl.substring(2);
field.setDollars(true);
}
rank.setNumber(new BigDecimal(vl));
field.getRank().add(rank);
}
field.setRankorder(ro.getReversed() != null ? -1 : 1);
}
category.getField().add(field);
}
}
ParsedRegions regions = parse(dir, "regions.html", "regions", lastupdate, ParsedRegions.class);
for (ParsedRegions.Region rgn : regions.getRegion())
{
Factbook.Region region = obf.createFactbookRegion();
region.setId(rgn.getId());
region.setShortId(rgn.getShortId());
region.setName(rgn.getName());
factbook.getRegion().add(region);
for (String id : rgn.getCountry()) {
String name = countrynames.get(id);
Factbook.Region.Country country = obf.createFactbookRegionCountry();
country.setId(id);
country.setName(name);
country.getAlias().addAll(countryAliases.get(id));
ParsedCountry ctry = parse(dir, id + ".html", "country", lastupdate, ParsedCountry.class, "countryid", id, "country", name);
if (ctry.getFlag() != null)
country.setFlag(1);
if (ctry.getAffiliation() != null)
country.setAffiliation(ctry.getAffiliation());
country.setLocator(ctry.getLocator());
country.setAnthem(ctry.getAnthem());
region.getCountry().add(country);
country.setLastupdate(toXMLDate(ctry.getLastupdate()));
country.setPhotocount(ctry.getPhotocount());
List<FieldContentType> fields = new ArrayList<FieldContentType>();
for (FieldContentType fld : ctry.getField()) {
fields.add(fld);
}
Collections.sort(fields, new Comparator<FieldContentType>() {
public int compare(FieldContentType o1, FieldContentType o2) {
return o1.getRef().compareTo(o2.getRef());
}
});
country.getField().addAll(fields);
}
}
ParsedFaq faq = parse(dir, "faq.html", "faq", lastupdate, ParsedFaq.class);
factbook.getFaqCategory().addAll(faq.getFaqCategory());
ParsedDefinitions defs = parse(dir, "definitions.html", "defs", lastupdate, ParsedDefinitions.class);
factbook.getDefinition().addAll(defs.getDefinition());
for (char ltr = 'a'; ltr <= 'g'; ltr++) {
ParsedAppendix appendix = parse(dir, "appendix-" + ltr + ".html", "appendix", lastupdate, ParsedAppendix.class);
Factbook.Appendix a = obf.createFactbookAppendix();
a.setLetter("" + ltr);
a.setName(appendix.getName());
a.setList(appendix.getList());
a.getTable().addAll(appendix.getTable());
factbook.getAppendix().add(a);
}
;
System.out.println("Writing out...");
FileOutputStream fos = new FileOutputStream(new File(dir, "../factbook2.xml"));
Marshaller m = ctx.createMarshaller();