log.debug("Article file: " + articleFile.getPath());
SAXReader reader = new SAXReader();
Document articleDocument = reader.read(articleFile);
DocumentFactory factory = DocumentFactory.getInstance();
try {
//------------------------------------
// article file
//------------------------------------
StringTokenizer tokenizer = new StringTokenizer(articlePathParameter, "/");
String articleChannel = tokenizer.nextToken();
String articleSection = tokenizer.nextToken();
String articleYear = tokenizer.nextToken();
String articleDirectory = tokenizer.nextToken();
// create backup
backupFile(articlePath);
// on we go
XPath articleXPath = factory.createXPath(
"/NewsML/NewsItem/NewsComponent/ContentItem/DataContent/nitf/body/body.head");
List parentNodes = articleXPath.selectNodes(articleDocument);
Element parent = null;
if (parentNodes.size() > 0) {
parent = (Element) parentNodes.get(0);
log.debug("\n--------------------------" + "\n- Parent element: " +
parent.getName() + "\n--------------------------");
} else {
log.error("\n--------------------------" + "\n- Parent node not found!" +
"\n--------------------------");
}
Element dossierElement = parent.element("dossier");
String oldDossierId = "none";
if (dossierElement != null) {
oldDossierId = dossierElement.attribute("id").getStringValue();
}
boolean replace = !oldDossierId.equals(newDossierId);
// ----------------------------------------------------
// do we need to replace it at all?
// ----------------------------------------------------
if (replace) {
// ----------------------------------------------------
// does a previous dossier entry exist?
// ----------------------------------------------------
if (dossierElement != null) {
parent.remove(dossierElement);
// ----------------------------------------------------
// remove from old dossier file
// ----------------------------------------------------
String oldDossierPath = dossierBasePath + oldDossierId + File.separator +
"index.xml";
File oldDossierFile = new File(oldDossierPath);
Document oldDossierDocument = reader.read(oldDossierFile);
XPath dossierXPath = factory.createXPath("/dossier/articles");
List oldArticlesElements = dossierXPath.selectNodes(oldDossierDocument);
Element oldArticlesElement = null;
if (oldArticlesElements.size() > 0) {
oldArticlesElement = (Element) oldArticlesElements.get(0);
log.debug("\n--------------------------" +
"\n- Old Dossier Parent element: " + oldArticlesElement.getName() +
"\n--------------------------");
} else {
log.error("\n--------------------------" +
"\n- Old Dossier Parent node not found!" +
"\n--------------------------");
}
List articleElements = oldArticlesElement.elements("article");
Element elementToRemove = null;
for (Iterator i = articleElements.iterator(); i.hasNext();) {
Element articleElement = (Element) i.next();
if (articleElement.attribute("channel").getStringValue().equals(articleChannel) &&
articleElement.attribute("section").getStringValue().equals(articleSection) &&
articleElement.attribute("year").getStringValue().equals(articleYear) &&
articleElement.attribute("id").getStringValue().equals(articleDirectory)) {
elementToRemove = articleElement;
}
}
oldArticlesElement.remove(elementToRemove);
writeDocument(oldDossierPath, oldDossierDocument);
// ----------------------------------------------------
// removing done
// ----------------------------------------------------
}
if (!newDossierId.equals("none")) {
// ----------------------------------------------------
// insert new dossier element
// ----------------------------------------------------
dossierElement = factory.createElement("dossier");
dossierElement.add(factory.createAttribute(dossierElement, "id", newDossierId));
parent.add(dossierElement);
//------------------------------------
// new dossier file
//------------------------------------
// create backup
backupFile(newDossierPath);
File newDossierFile = new File(newDossierPath);
log.debug("New dossier file: " + newDossierFile.getPath());
Document newDossierDocument = reader.read(newDossierFile);
XPath dossierXPath = factory.createXPath("/dossier/articles");
List articlesElements = dossierXPath.selectNodes(newDossierDocument);
Element articlesElement = null;
if (articlesElements.size() > 0) {
articlesElement = (Element) articlesElements.get(0);
log.debug("\n--------------------------" + "\n- Parent element: " +
articlesElement.getName() + "\n--------------------------");
} else {
log.error("\n--------------------------" + "\n- Parent node not found!" +
"\n--------------------------");
}
Element articleElement = factory.createElement("article");
articleElement.add(factory.createAttribute(articleElement, "channel",
articleChannel));
articleElement.add(factory.createAttribute(articleElement, "section",
articleSection));
articleElement.add(factory.createAttribute(articleElement, "year", articleYear));
articleElement.add(factory.createAttribute(articleElement, "id",
articleDirectory));
articlesElement.add(articleElement);
writeDocument(newDossierPath, newDossierDocument);
}