package org.honours.greenstone; import java.util.List; import org.apache.log4j.Logger; import org.expeditee.items.Item; import org.expeditee.items.Picture; import org.expeditee.items.Text; import org.honours.collection.CollectionItem; import org.honours.collection.NoteLayer; public class CollectionContentEditor { static Logger logger = Logger.getLogger(org.honours.greenstone.CollectionContentEditor.class.getName()); private CollectionItem _collectionItem; private ArchiveFileEditor _archiveFileEditor; //private HtmlForDocXML _htmlForDocXML; public CollectionContentEditor(CollectionItem currCI, String _collectionPath) { _collectionItem = currCI; _archiveFileEditor = new ArchiveFileEditor(_collectionItem.getDocXML()); } public void editCollectionItems() { List items = _collectionItem.getFrame().getItems(); boolean noChange = true; ArchiveFileReader afr = new ArchiveFileReader(_collectionItem.getDocXML()); for(Item item : items){ if(item instanceof Text){ if(item.isAnnotation()){ //TODO: Deal with annotation items correctly. }else{ Text text = (Text)item; if(text.getData() != null) if(text.getData().size() > 0){ String elemText = afr.obtainElementText(text.getData().get(0)); if(!elemText.equals(null)){ if(elemText.equals(text.getText())) continue; else noChange = false; } } if(!noChange) _archiveFileEditor.editArchiveDocFile(text); } }else if(item instanceof Picture){ //TODO: Deal with picture items correctly. } } noChange = writeFrameID(afr,noChange); noChange = addNotes(); //addNotes(); if(!noChange) _archiveFileEditor.write(); } /** * Method for adding/editing the framID metadata value * @param afr * @param noChange */ private boolean writeFrameID(ArchiveFileReader afr, boolean noChange){ //Write code to write frame number. String frameID = afr.obtainElementText("frameID"); String currentFrameID = Integer.toString(_collectionItem.getFrame().getNumber()); Text frameIDtext = null; if(frameID == null){ //create a new frameID element frameIDtext = new Text(currentFrameID); frameIDtext.setData("frameID"); }else{ //check if frameID is the same as the current frame name/number. if(!frameID.equals(currentFrameID)){ frameIDtext = new Text(currentFrameID); frameIDtext.setData("frameID"); } } if(frameIDtext != null){ System.err.println("Setting frame metadata... " + frameIDtext.getText()); _archiveFileEditor.editArchiveDocFile(frameIDtext); noChange = false; } return noChange; } /** * Method to output any notes associated with an item to the * doc.xml file. */ private boolean addNotes() { //Add notes to docXML file. NoteLayer noteLayer = _collectionItem.getNoteLayer(); if(noteLayer != null){ StringBuffer notesStringBuffer = new StringBuffer(""); notesStringBuffer.append(""); notesStringBuffer.append(""); for(Text t : noteLayer.getFrame().getTextItems()){ notesStringBuffer.append(""); notesStringBuffer.append(""); notesStringBuffer.append(""); } notesStringBuffer.append(""); notesStringBuffer.append("
"); notesStringBuffer.append(t.getText() + "
"); notesStringBuffer.append("
"); if(!notesStringBuffer.toString().equals("")) { //Write a "Notes" metadata element to the doc.xml file. Text notesText = new Text(notesStringBuffer.toString()); notesText.setData("Notes"); _archiveFileEditor.editArchiveDocFile(notesText); } System.err.println("Writing out notes..."); return false; //false equates to a change. } return true; //true equates to "no change" } }