2 new revisions: Revision: 4f5e7213a341 Branch: rt2091-tPagesGui Author: Corey Knapp <cknapp.code@xxxxxxxxx> Date: Fri Feb 13 20:32:12 2015 UTC Log: TPages dialog supports saving and loading of xml files https://code.google.com/p/brailleblaster/source/detail?r=4f5e7213a341 Revision: 2cb703f2f83b Branch: rt2091-tPagesGui Author: Corey Knapp <cknapp.code@xxxxxxxxx> Date: Wed Feb 18 17:52:54 2015 UTC Log: Automatically fills in some of the basic info https://code.google.com/p/brailleblaster/source/detail?r=2cb703f2f83b ============================================================================== Revision: 4f5e7213a341 Branch: rt2091-tPagesGui Author: Corey Knapp <cknapp.code@xxxxxxxxx> Date: Fri Feb 13 20:32:12 2015 UTC Log: TPages dialog supports saving and loading of xml files https://code.google.com/p/brailleblaster/source/detail?r=4f5e7213a341 Modified: /src/main/org/brailleblaster/perspectives/braille/Manager.java /src/main/org/brailleblaster/tpages/TPagesDialog.java /src/main/org/brailleblaster/tpages/TPagesGenerator.java =======================================--- /src/main/org/brailleblaster/perspectives/braille/Manager.java Thu Feb 12 18:30:13 2015 UTC +++ /src/main/org/brailleblaster/perspectives/braille/Manager.java Fri Feb 13 20:32:12 2015 UTC
@@ -129,6 +129,7 @@ SearchDialog srch = null; TPagesDialog tpDialog = null; private Vector<String> ignoreList = new Vector<String>(); + private String lastTPage; //Constructor that sets things up for a new document. public Manager(WPManager wp, String docName) { @@ -1504,6 +1505,14 @@ return null; } + public String getLastTPage() { + return lastTPage; + } + + public void setLastTPage(String lastTPage) { + this.lastTPage = lastTPage; + } + public void addUndoEvent(EventFrame f){ queueManager.addUndoEvent(f); } =======================================--- /src/main/org/brailleblaster/tpages/TPagesDialog.java Fri Feb 13 15:16:10 2015 UTC +++ /src/main/org/brailleblaster/tpages/TPagesDialog.java Fri Feb 13 20:32:12 2015 UTC
@@ -18,8 +18,10 @@ import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Combo; import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Dialog; import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Group; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; @@ -34,6 +36,12 @@ private Display display; final int TEXT_BOX_WIDTH = 500; final int LABEL_WIDTH = 150; + HashMap<Text, String> uimap;+ Text titleText, gradeLevelText, subtitleText, seriesText, editionText, authorText, translatorText, publisherText, + pubLocationText, pubWebsiteText, copyDateText, copyText, reproText, isbn13Text, isbn10Text, printHistoryText,
+ transYearText, transText, tgsText, affiliationText; + Combo permissionCombo; + Button copyrightButton, copySymbolButton; TPagesGenerator tpGenerator; HashMap<String, String> xmlmap; @@ -45,13 +53,21 @@ public Object open(){ tpGenerator = new TPagesGenerator(); - if(!tpGenerator.checkForFile("filename")){ + /*if(!tpGenerator.checkForFile("filename")){ tpGenerator.createNewTPageXML(); } - tpGenerator.openTPageXML("filename"); + tpGenerator.openTPageXML("filename");*/ xmlmap = tpGenerator.getXmlMap(); + uimap = new HashMap<Text, String>(); createContents(); + if(m.getLastTPage()!=null){ + if(tpGenerator.checkForFile(m.getLastTPage())){ + openFromXml(m.getLastTPage()); + updateContents(); + } + } + shlTPages.open(); shlTPages.layout(); @@ -91,26 +107,26 @@ titleGroup.setLayout(titlePageLayout); createLabel(titleGroup, "Title", 1); - final Text titleText = createText(titleGroup, 1, "title"); + titleText = createText(titleGroup, 1, "title"); createLabel(titleGroup, "Grade Level", 1); - final Text gradeLevelText = createText(titleGroup, 1, "gradelevel"); + gradeLevelText = createText(titleGroup, 1, "gradelevel"); createLabel(titleGroup, "Subtitle", 1); - final Text subtitleText = createText(titleGroup, 1, "subtitle"); + subtitleText = createText(titleGroup, 1, "subtitle"); createLabel(titleGroup, "Series Name", 1); - final Text seriesText = createText(titleGroup, 1, "seriesname"); + seriesText = createText(titleGroup, 1, "seriesname"); createLabel(titleGroup, "Edition Name or Number", 1); - final Text editionText = createText(titleGroup, 1, "editionname"); + editionText = createText(titleGroup, 1, "editionname"); Group authorGroup = new Group(titleComposite, SWT.NONE); authorGroup.setText("Author"); authorGroup.setLayout(new GridLayout(2,false)); createLabel(authorGroup, "Author(s)", 1);- final Text authorText = new Text(authorGroup, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL); + authorText = new Text(authorGroup, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL);
GridData newData = newTpData(1); newData.widthHint = TEXT_BOX_WIDTH; newData.heightHint = 70; @@ -119,35 +135,36 @@ authorText.setLayoutData(newData); createLabel(authorGroup, "Translator", 1); - final Text translatorText = createText(authorGroup, 1, "translator"); + translatorText = createText(authorGroup, 1, "translator"); Group publisherGroup = new Group(titleComposite, SWT.NONE); publisherGroup.setText("Publisher"); publisherGroup.setLayout(new GridLayout(2,false)); createLabel(publisherGroup, "Permission", 1); - final Combo permissionCombo = new Combo(publisherGroup, SWT.READ_ONLY); + permissionCombo = new Combo(publisherGroup, SWT.READ_ONLY); permissionCombo.setLayoutData(newTpData(1));permissionCombo.setItems(new String[]{"Published by", "With permission of the publisher,"}); - if (xmlmap.get("pubpermission").equals("0")|| xmlmap.get("pubpermission").equals("1"))
- permissionCombo.select(Integer.parseInt(xmlmap.get("pubpermission"))); - else - permissionCombo.select(0); - + permissionCombo.select(0); + if(xmlmap.get("pubpermission")!=null){+ if (xmlmap.get("pubpermission").equals("0")|| xmlmap.get("pubpermission").equals("1"))
+ permissionCombo.select(Integer.parseInt(xmlmap.get("pubpermission"))); + } + createLabel(publisherGroup, "Publisher", 1); - final Text publisherText = createText(publisherGroup, 1, "publisher"); + publisherText = createText(publisherGroup, 1, "publisher"); createLabel(publisherGroup, "City and State", 1); - final Text pubLocationText = createText(publisherGroup, 1, "location"); + pubLocationText = createText(publisherGroup, 1, "location"); createLabel(publisherGroup, "Website", 1); - final Text pubWebsiteText = createText(publisherGroup, 1, "website"); + pubWebsiteText = createText(publisherGroup, 1, "website"); Group printGroup = new Group (titleComposite, SWT.NONE); printGroup.setText("Printing Info"); printGroup.setLayout(new GridLayout(2,false)); - final Button copyrightButton = new Button(printGroup, SWT.CHECK); + copyrightButton = new Button(printGroup, SWT.CHECK); copyrightButton.setText("Copyright");copyrightButton.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false, 1, 1));
if(xmlmap.get("copyrighted")!=null){ @@ -155,7 +172,7 @@ } else{ copyrightButton.setSelection(true); } - final Button copySymbolButton = new Button(printGroup, SWT.CHECK); + copySymbolButton = new Button(printGroup, SWT.CHECK); copySymbolButton.setText("Copyright Symbol");copySymbolButton.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false, 1, 1));
if(xmlmap.get("copyrightsymbol")!=null){ @@ -165,39 +182,39 @@ } createLabel(printGroup, "Copyright Date", 1); - final Text copyDateText = createText(printGroup, 1, "copyrightdate"); + copyDateText = createText(printGroup, 1, "copyrightdate"); createLabel(printGroup, "Copyright Text", 1); - final Text copyText = createText(printGroup, 1, "copyrighttext"); + copyText = createText(printGroup, 1, "copyrighttext"); createLabel(printGroup, "Reproduction Notice", 1); - final Text reproText = createText(printGroup, 1, "repronotice"); + reproText = createText(printGroup, 1, "repronotice");//reproText.setText("Further reproduction or distribution in other than a specialized format is prohibited.");
createLabel(printGroup,"ISBN-13", 1); - final Text isbn13Text = createText(printGroup, 1, "isbn13"); + isbn13Text = createText(printGroup, 1, "isbn13"); createLabel(printGroup, "ISBN-10", 1); - final Text isbn10Text = createText(printGroup, 1, "isbn10"); + isbn10Text = createText(printGroup, 1, "isbn10"); createLabel(printGroup, "Printing History", 1); - final Text printHistoryText = createText(printGroup, 1, "printhistory"); + printHistoryText = createText(printGroup, 1, "printhistory"); Group transcriberGroup = new Group (titleComposite, SWT.NONE); transcriberGroup.setText("Transcriber"); transcriberGroup.setLayout(new GridLayout(2, false)); createLabel(transcriberGroup, "Transcription Year", 1); - final Text transYearText = createText(transcriberGroup, 1, "year"); + transYearText = createText(transcriberGroup, 1, "year"); createLabel(transcriberGroup, "Transcriber", 1); - final Text transText = createText(transcriberGroup, 1, "transcriber"); + transText = createText(transcriberGroup, 1, "transcriber"); createLabel(transcriberGroup, "Tactile Graphics Specialist", 1); - final Text tgsText = createText(transcriberGroup, 1, "tgs"); + tgsText = createText(transcriberGroup, 1, "tgs"); createLabel(transcriberGroup, "Affiliation", 1);- final Text affiliationText = createText(transcriberGroup, 1, "affiliation");
+ affiliationText = createText(transcriberGroup, 1, "affiliation"); Group volumesGroup = new Group (titleComposite, SWT.NONE); volumesGroup.setText("Volumes"); @@ -222,7 +239,7 @@ ///////////////////////////// Button closeButton = new Button(shlTPages, SWT.PUSH);- GridData buttonData = new GridData(SWT.RIGHT, SWT.BEGINNING, false, false, 4, 1); + GridData buttonData = new GridData(SWT.RIGHT, SWT.BEGINNING, false, false, 3, 1);
buttonData.widthHint = 100; buttonData.heightHint = 30; closeButton.setText("Close"); @@ -238,43 +255,56 @@ public void widgetDefaultSelected(SelectionEvent arg0) { } + }); + + Button openButton = new Button(shlTPages, SWT.PUSH); + buttonData = new GridData(SWT.CENTER, SWT.BEGINNING, false, false, 1, 1); + buttonData.widthHint = 100; + buttonData.heightHint = 30; + openButton.setText("Open"); + openButton.setLayoutData(buttonData); + + openButton.addSelectionListener(new SelectionListener() { + @Override + public void widgetSelected(SelectionEvent e){ + FileDialog openFile = new FileDialog(shlTPages, SWT.OPEN); + openFile.setText("Open Transcriber-Generated Page"); + openFile.setFilterExtensions(new String[] {"*.xml"}); + openFromXml(openFile.open()); + updateContents(); + } + + @Override + public void widgetDefaultSelected(SelectionEvent arg0){ + + } }); Button generateButton = new Button(shlTPages, SWT.PUSH); buttonData = new GridData(SWT.LEFT, SWT.BEGINNING, false, false, 1, 1); buttonData.widthHint = 100; buttonData.heightHint = 30; - generateButton.setText("Generate"); + generateButton.setText("Save XML"); generateButton.setLayoutData(buttonData); generateButton.addSelectionListener(new SelectionListener() { @Override public void widgetSelected(SelectionEvent e) { - xmlmap.put("title", titleText.getText()); - xmlmap.put("gradelevel", gradeLevelText.getText()); - xmlmap.put("subtitle", subtitleText.getText()); - xmlmap.put("seriesname", seriesText.getText()); - xmlmap.put("editionname", editionText.getText()); authorText.setText(authorText.getText().replaceAll("\r\n", ";")); xmlmap.put("authors", authorText.getText()); - xmlmap.put("translator", translatorText.getText()); xmlmap.put("pubpermission", "" + permissionCombo.getSelectionIndex()); - xmlmap.put("publisher", publisherText.getText()); - xmlmap.put("location", pubLocationText.getText()); - xmlmap.put("website", pubWebsiteText.getText());xmlmap.put("copyrighted", String.valueOf(copyrightButton.getSelection())); xmlmap.put("copyrightsymbol", String.valueOf(copySymbolButton.getSelection()));
- xmlmap.put("copyrightdate", copyDateText.getText()); - xmlmap.put("copyrighttext", copyText.getText()); - xmlmap.put("repronotice", reproText.getText()); - xmlmap.put("isbn13", isbn13Text.getText()); - xmlmap.put("isbn10", isbn10Text.getText()); - xmlmap.put("printhistory", printHistoryText.getText()); - xmlmap.put("year", transYearText.getText()); - xmlmap.put("transcriber", transText.getText()); - xmlmap.put("tgs", tgsText.getText()); - xmlmap.put("affiliation", affiliationText.getText()); - tpGenerator.saveNewTPage("filename", xmlmap); + for(Map.Entry<Text, String> entry : uimap.entrySet()) + xmlmap.put(entry.getValue(), entry.getKey().getText()); + + FileDialog saveFile = new FileDialog(shlTPages, SWT.SAVE); + saveFile.setFilterExtensions(new String[] { "*.xml" }); + String result = saveFile.open(); + if(result!=null){ + m.setLastTPage(result); + tpGenerator.saveNewTPage(result, xmlmap); + } } @Override @@ -287,11 +317,24 @@ } - public GridData newTpData(int columns){ + private void updateContents(){ + for(Map.Entry<Text, String> entry : uimap.entrySet()) + entry.getKey().setText(xmlmap.get(entry.getValue())); + permissionCombo.select(Integer.parseInt(xmlmap.get("pubpermission"))); + copyrightButton.setSelection(xmlmap.get("copyrighted").equals("true"));+ copySymbolButton.setSelection(xmlmap.get("copyrightsymbol").equals("true"));
+ authorText.setText(xmlmap.get("authors").replaceAll(";", "\r\n")); + affiliationText.setText(xmlmap.get("affiliation")); + } + + private void openFromXml(String filepath){ + tpGenerator.openTPageXML(filepath); + } + private GridData newTpData(int columns){return new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false, columns, 1);
} - public Label createLabel(Composite comp, String text, int horizSpan){ + private Label createLabel(Composite comp, String text, int horizSpan){ Label newLabel = new Label(comp, SWT.NONE); newLabel.setText(text); GridData labelData = newTpData(horizSpan); @@ -300,7 +343,7 @@ return newLabel; } - public Text createText(Composite comp, int horizSpan, String xmlmapKey){ + private Text createText(Composite comp, int horizSpan, String xmlmapKey){ Text newText = new Text(comp, SWT.SINGLE | SWT.BORDER); GridData newData = newTpData(horizSpan); newData.widthHint = TEXT_BOX_WIDTH; @@ -310,6 +353,7 @@ newText.setText(xmlmap.get(xmlmapKey)); } } + uimap.put(newText, xmlmapKey); return newText; } } =======================================--- /src/main/org/brailleblaster/tpages/TPagesGenerator.java Fri Feb 13 15:16:10 2015 UTC +++ /src/main/org/brailleblaster/tpages/TPagesGenerator.java Fri Feb 13 20:32:12 2015 UTC
@@ -44,7 +44,7 @@ */ public boolean openTPageXML(String filename){ try{ - File file = new File("C:\\Users\\cknapp\\Desktop\\testingjavaxml.xml"); + File file = new File(filename);DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = docBuilder.parse(file); @@ -72,7 +72,7 @@ } public boolean checkForFile(String filename){ - File file = new File("C:\\Users\\cknapp\\Desktop\\testingjavaxml.xml"); + File file = new File(filename); if(file.exists()){ return true; } else { @@ -83,7 +83,7 @@ /** User is using T-Pages for the first time. This will at some point require a String argument for the filename
*/ - public boolean createNewTPageXML(){ + public boolean createNewTPageXML(String filename){ try{DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); @@ -139,7 +139,7 @@ Transformer transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(doc); //StreamResult result = new StreamResult(System.out);- StreamResult result = new StreamResult(new File("C:\\Users\\cknapp\\Desktop\\testingjavaxml.xml"));
+ StreamResult result = new StreamResult(new File(filename)); transformer.setOutputProperty(OutputKeys.INDENT, "yes");transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount";, "2");
transformer.transform(source, result); @@ -155,7 +155,9 @@ }public boolean saveNewTPage(String filename, HashMap<String, String> newXmlMap){
- File file = new File("C:\\Users\\cknapp\\Desktop\\testingjavaxml.xml"); + File file = new File(filename); + if(!checkForFile(filename)) + createNewTPageXML(filename); Set mapSet = newXmlMap.entrySet(); Iterator iterator = mapSet.iterator(); try{ @@ -170,7 +172,7 @@TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc);- StreamResult result = new StreamResult(new File("C:\\Users\\cknapp\\Desktop\\testingjavaxml.xml"));
+ StreamResult result = new StreamResult(new File(filename)); transformer.transform(source, result); } catch(ParserConfigurationException e){ ============================================================================== Revision: 2cb703f2f83b Branch: rt2091-tPagesGui Author: Corey Knapp <cknapp.code@xxxxxxxxx> Date: Wed Feb 18 17:52:54 2015 UTC Log: Automatically fills in some of the basic info https://code.google.com/p/brailleblaster/source/detail?r=2cb703f2f83b Modified: /src/main/org/brailleblaster/tpages/TPagesDialog.java /src/main/org/brailleblaster/tpages/TPagesGenerator.java =======================================--- /src/main/org/brailleblaster/tpages/TPagesDialog.java Fri Feb 13 20:32:12 2015 UTC +++ /src/main/org/brailleblaster/tpages/TPagesDialog.java Wed Feb 18 17:52:54 2015 UTC
@@ -14,6 +14,7 @@ import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.layout.RowData; import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Combo; @@ -24,6 +25,7 @@ import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Group; import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.MessageBox; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.TabFolder; import org.eclipse.swt.widgets.TabItem; @@ -53,10 +55,10 @@ public Object open(){ tpGenerator = new TPagesGenerator(); - /*if(!tpGenerator.checkForFile("filename")){ - tpGenerator.createNewTPageXML(); - } - tpGenerator.openTPageXML("filename");*/ + + if(m.getDocumentName()!=null)+ if(m.getDocumentName().substring(m.getDocumentName().length()-4).equals(".xml"))
+ tpGenerator.autoPopulate(m.getDocumentName()); xmlmap = tpGenerator.getXmlMap(); uimap = new HashMap<Text, String>(); createContents(); @@ -95,13 +97,18 @@ TabItem symbolsTab = new TabItem(folder, SWT.NONE); symbolsTab.setText("Special Symbols"); + TabItem transNotesTab = new TabItem(folder, SWT.NONE); + transNotesTab.setText("Transcriber's Notes"); + ///////////////////////////// ///////Title Page Tab//////// ///////////////////////////// - Composite titleComposite = new Composite(folder, SWT.NONE); + ScrolledComposite sc = new ScrolledComposite(folder, SWT.V_SCROLL); + sc.setLayout(new FillLayout()); + Composite titleComposite = new Composite(sc, SWT.NONE); GridLayout titlePageLayout = new GridLayout(2,false); titleComposite.setLayout(new RowLayout(SWT.VERTICAL)); -+ //titleComposite.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false, 1, 1));
Group titleGroup = new Group(titleComposite, SWT.NONE); titleGroup.setText("Title"); titleGroup.setLayout(titlePageLayout); @@ -189,7 +196,6 @@ createLabel(printGroup, "Reproduction Notice", 1); reproText = createText(printGroup, 1, "repronotice");- //reproText.setText("Further reproduction or distribution in other than a specialized format is prohibited.");
createLabel(printGroup,"ISBN-13", 1); isbn13Text = createText(printGroup, 1, "isbn13"); @@ -221,7 +227,12 @@ volumesGroup.setLayout(new GridLayout(2,false)); createLabel(volumesGroup, "Not yet implemented", 1); - titleTab.setControl(titleComposite); + sc.setContent(titleComposite); + sc.setExpandVertical(true); + sc.setExpandHorizontal(true); + sc.setMinHeight(500); + sc.setSize(800, 500); + titleTab.setControl(sc); ///////////////////////////// ///////////////////////////// @@ -233,11 +244,25 @@ Group symbolsGroup = new Group(symbolsComposite, SWT.NONE); symbolsGroup.setLayout(new GridLayout(1, false)); - createLabel(symbolsGroup, "lolidk", 1); + createLabel(symbolsGroup, "Not yet implemented", 1); symbolsTab.setControl(symbolsComposite); ///////////////////////////// + ///////////////////////////// + ////Transcriber Notes Tab//// + ///////////////////////////// + Composite transNotesComposite = new Composite(folder, SWT.NONE); + transNotesComposite.setLayout(new RowLayout(SWT.VERTICAL)); + + Group transNotesGroup = new Group(transNotesComposite, SWT.NONE); + transNotesGroup.setLayout(new GridLayout(1,false)); + + createLabel(transNotesGroup, "Not yet implemented", 1); + + transNotesTab.setControl(transNotesComposite); + ///////////////////////////// + Button closeButton = new Button(shlTPages, SWT.PUSH);GridData buttonData = new GridData(SWT.RIGHT, SWT.BEGINNING, false, false, 3, 1);
buttonData.widthHint = 100; @@ -270,8 +295,14 @@ FileDialog openFile = new FileDialog(shlTPages, SWT.OPEN); openFile.setText("Open Transcriber-Generated Page"); openFile.setFilterExtensions(new String[] {"*.xml"}); - openFromXml(openFile.open()); - updateContents(); + String filePath = openFile.open(); + if(filePath!=null){ + if(openFromXml(filePath)){ + updateContents(); + } else {+ createError("Error reading file. Was this file created by this dialog?");
+ } + } } @Override @@ -316,6 +347,13 @@ shlTPages.pack(true); } + + public void createError(String errorMessage){+ MessageBox errorDialog = new MessageBox(shlTPages, SWT.ICON_ERROR | SWT.OK);
+ errorDialog.setText("Error"); + errorDialog.setMessage(errorMessage); + errorDialog.open(); + } private void updateContents(){ for(Map.Entry<Text, String> entry : uimap.entrySet()) @@ -327,8 +365,12 @@ affiliationText.setText(xmlmap.get("affiliation")); } - private void openFromXml(String filepath){ - tpGenerator.openTPageXML(filepath); + private boolean openFromXml(String filepath){ + if(filepath!=null){ + if(tpGenerator.openTPageXML(filepath)) + return true; + } + return false; } private GridData newTpData(int columns){return new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false, columns, 1);
=======================================--- /src/main/org/brailleblaster/tpages/TPagesGenerator.java Fri Feb 13 20:32:12 2015 UTC +++ /src/main/org/brailleblaster/tpages/TPagesGenerator.java Wed Feb 18 17:52:54 2015 UTC
@@ -2,8 +2,10 @@ import java.io.File; import java.io.IOException; +import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; +import java.util.List; import java.util.Map; import java.util.Set; @@ -31,7 +33,8 @@ String programDataPath;String[] xmlElements = {"title", "gradelevel", "subtitle", "seriesname", "editionname", "authors", "translator", "pubpermission", "publisher", "location", "website", "copyrighted", "copyrightsymbol", "copyrightdate", - "copyrighttext", "repronotice", "isbn13", "isbn10", "printhistory", "year", "transcriber", "tgs", "affiliation"}; + "copyrighttext", "repronotice", "isbn13", "isbn10", "printhistory", "year", "transcriber", "tgs", "affiliation"}; + String pub, pubLoc, pubWeb, copyright, isbn13 = "", isbn10 = "", printHistory;
private HashMap<String, String> xmlmap; public TPagesGenerator(){ @@ -44,16 +47,20 @@ */ public boolean openTPageXML(String filename){ try{ - File file = new File(filename); -- DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
- Document doc = docBuilder.parse(file); - doc.getDocumentElement().normalize(); - - //NodeList nodes = doc.getChildNodes(); - //System.out.println("nodelist = " + nodes.getLength() + " nodes"); - for (int i = 0; i < xmlElements.length; i++){- xmlmap.put(xmlElements[i], doc.getElementsByTagName(xmlElements[i]).item(0).getTextContent());
+ if(checkForFile(filename)){ + File file = new File(filename); ++ DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+ Document doc = docBuilder.parse(file); + doc.getDocumentElement().normalize(); + if(!doc.getFirstChild().getNodeName().equals("tpage")){ + return false; + } + for (int i = 0; i < xmlElements.length; i++){+ xmlmap.put(xmlElements[i], doc.getElementsByTagName(xmlElements[i]).item(0).getTextContent());
+ } + } else { + return false; } @@ -138,7 +145,6 @@ TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(doc); - //StreamResult result = new StreamResult(System.out); StreamResult result = new StreamResult(new File(filename)); transformer.setOutputProperty(OutputKeys.INDENT, "yes");transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount";, "2");
@@ -192,6 +198,224 @@ return true; } + + /*+ * Attempts to find data in the book to automatically populate the text fields
+ */ + public void autoPopulate(String bookPath){ + File book = new File(bookPath); + try{+ DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+ Document doc = docBuilder.parse(book); + + Node fmNode = doc.getElementsByTagName("frontmatter").item(0); + + ///////Title + String title = findTitle(doc); + String subtitle = ""; + if(title.contains(":")){ //There is probably a subtitle + subtitle = title.substring(title.indexOf(":") + 2);+ while(subtitle.charAt(0)==' '){ // Remove spaces from beginning of subtitle
+ subtitle = subtitle.substring(1); + } + title = title.substring(0, title.indexOf(":")); + } + while(title.charAt(0)==' '){ + title = title.substring(1); + } + //////////Authors + String authors = findAuthors(doc); + if(authors.contains("by")){ + authors = authors.substring(authors.indexOf("by") + 2); + } + authors = authors.replace(", ", ";"); + authors = authors.replace("and ", ""); + while(authors.charAt(0)==' ' || authors.charAt(0)==';'){ + authors = authors.substring(1); + } + xmlmap.put("title", title); + xmlmap.put("subtitle", subtitle); + xmlmap.put("authors", authors); + + ///////Begin iterating through front matter + parseFrontMatter(fmNode); + xmlmap.put("isbn13", isbn13); + xmlmap.put("isbn10", isbn10); + + if(copyright!=null){ + if(copyright.contains("©")){ + xmlmap.put("copyrighted", "true"); + xmlmap.put("copyrightsymbol", "true"); + } + if(copyright.contains("©")){ + xmlmap.put("copyrighted", "true"); + xmlmap.put("copyrightsymbol", "true"); + } + else if(copyright.contains("Copyright")){ + xmlmap.put("copyrighted", "true"); + } + else { + xmlmap.put("copyrighted", "false"); + xmlmap.put("copyrightsymbol", "false"); + } + if(!copyright.equals("")){ + //Find copyright year and publisher + int tempInt = 0; + String copyYear = ""; + String copyPub = ""; + while(tempInt < copyright.length()){ + if(copyYear.equals("")){ + if(Character.isDigit(copyright.charAt(tempInt))){ + copyYear = copyright.substring(tempInt, tempInt + 4); + tempInt += 4; + } + } else { //Found the year, now look for publisher + if(copyright.charAt(tempInt)=='b'){ + if(copyright.charAt(tempInt+1)=='y'){ + tempInt += 3; + for(int i = tempInt; i < copyright.length(); i++){ + if(copyright.charAt(i)!='.') + copyPub += copyright.charAt(i); + else + i = copyright.length(); + } + break; + } + } + if(Character.isUpperCase(copyright.charAt(tempInt))){ + for(int i = tempInt; i < copyright.length(); i++){ + if(copyright.charAt(i)!='.') + copyPub += copyright.charAt(i); + else + i = copyright.length(); + } + break; + } + } + + tempInt++; + }+ if(copyYear.length()==4){ //If the copyright year isn't 4 digits, we were likely very off. Throw it all out!
+ xmlmap.put("copyrightdate", copyYear); + xmlmap.put("copyrighttext", copyPub); + } + } + } ++ xmlmap.put("repronotice", "Further reproduction or distribution in other than a specialized format is prohibited.");
+ + } catch(ParserConfigurationException e){ + e.printStackTrace(); + return; + } catch(IOException e){ + e.printStackTrace(); + return; + }catch (SAXException e){ + e.printStackTrace(); + return; + } + } ++ private List<Node> getAllChildren(Node parent){ // Could likely be made more efficient
+ List<Node> returnedNodes = new ArrayList<Node>(); + NodeList nodes = parent.getChildNodes(); + for(int i = 0; i < nodes.getLength(); i++){ + Node tempNode = nodes.item(i); + returnedNodes.add(tempNode); + if(tempNode.hasChildNodes()){ + List<Node> childNodes = new ArrayList<Node>(getAllChildren(tempNode)); + for(int q = 0; q < childNodes.size(); q++){ + returnedNodes.add(childNodes.get(q)); + } + } + } + return returnedNodes; + } + + private String findTitle(Document doc){ + String title = "";+ List<Node> nodeChildren = getAllChildren(doc.getElementsByTagName("doctitle").item(0));
+ for (int q = 0; q < nodeChildren.size(); q++){ + if(nodeChildren.get(q).getNodeName().equals("#text")) + title += " " + nodeChildren.get(q).getNodeValue(); + } + return title; + } + + private String findAuthors(Document doc){ + String authors = "";+ List<Node> nodeChildren = getAllChildren(doc.getElementsByTagName("docauthor").item(0));
+ for(int q = 0; q < nodeChildren.size(); q++){ + if(nodeChildren.get(q).getNodeName().equals("#text")) + authors += ";" + nodeChildren.get(q).getNodeValue(); + } + return authors; + } + + private void parseFrontMatter(Node parent){ + NodeList nodes = parent.getChildNodes(); + for(int i = 0; i < nodes.getLength(); i++){ + Node tempNode = nodes.item(i); + if(tempNode.getNodeName().equals("#text")){ + String nodeVal = tempNode.getNodeValue(); + //////Copyright+ if(nodeVal.contains("Copyright") || nodeVal.contains("©") || nodeVal.contains("©")){
+ copyright = nodeVal; + } + + //////ISBN + if(nodeVal.contains("ISBN-")){ + if(nodeVal.contains("ISBN-13")){+ for(int q = nodeVal.indexOf("ISBN-13") + 8; q < nodeVal.length(); q++){
+ if(Character.isDigit(nodeVal.charAt(q))||nodeVal.charAt(q) == '-'){ + isbn13 += nodeVal.charAt(q); + } + if(nodeVal.charAt(q)==' ' && !isbn13.equals("")){ + break; + } + } + } + if(nodeVal.contains("ISBN-10")){+ for(int q = nodeVal.indexOf("ISBN-10") + 8; q < nodeVal.length(); q++){
+ if(Character.isDigit(nodeVal.charAt(q))||nodeVal.charAt(q) == '-'){ + isbn10 += nodeVal.charAt(q); + } + if(nodeVal.charAt(q)==' ' && !isbn10.equals("")){ + break; + } + } + } + } else if (nodeVal.contains("ISBN")){ + String tempIsbn = ""; + int numbers = 0; + for(int q = nodeVal.indexOf("ISBN") + 4; q < nodeVal.length(); q++){ + if(Character.isDigit(nodeVal.charAt(q))){ + tempIsbn += nodeVal.charAt(q); + numbers++; + } + if(nodeVal.charAt(q)=='-'){ + tempIsbn += nodeVal.charAt(q); + } + if(nodeVal.charAt(q)==' '&&!tempIsbn.equals("")){ + break; + } + } + if(numbers==13){ + isbn13 = tempIsbn; + } + if(numbers==10){ + isbn10 = tempIsbn; + } + + } + } + + ////// + if(tempNode.hasChildNodes()){ + parseFrontMatter(tempNode); + } + } + } public HashMap<String, String> getXmlMap(){ return xmlmap;