Coverage Report - org.crosswire.jsword.examples.DictToOsis
 
Classes in this File Line Coverage Branch Coverage Complexity
DictToOsis
0%
0/38
0%
0/6
1.8
 
 1  
 /**
 2  
  * Distribution License:
 3  
  * JSword is free software; you can redistribute it and/or modify it under
 4  
  * the terms of the GNU Lesser General Public License, version 2.1 or later
 5  
  * as published by the Free Software Foundation. This program is distributed
 6  
  * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 7  
  * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 8  
  * See the GNU Lesser General Public License for more details.
 9  
  *
 10  
  * The License is available on the internet at:
 11  
  *      http://www.gnu.org/copyleft/lgpl.html
 12  
  * or by writing to:
 13  
  *      Free Software Foundation, Inc.
 14  
  *      59 Temple Place - Suite 330
 15  
  *      Boston, MA 02111-1307, USA
 16  
  *
 17  
  * © CrossWire Bible Society, 2005 - 2016
 18  
  *
 19  
  */
 20  
 package org.crosswire.jsword.examples;
 21  
 
 22  
 import java.io.FileOutputStream;
 23  
 import java.io.IOException;
 24  
 import java.io.OutputStreamWriter;
 25  
 import java.io.Writer;
 26  
 import java.text.FieldPosition;
 27  
 import java.text.MessageFormat;
 28  
 
 29  
 import org.crosswire.common.xml.SAXEventProvider;
 30  
 import org.crosswire.common.xml.XMLProcess;
 31  
 import org.crosswire.common.xml.XMLUtil;
 32  
 import org.crosswire.jsword.book.Book;
 33  
 import org.crosswire.jsword.book.BookData;
 34  
 import org.crosswire.jsword.book.BookException;
 35  
 import org.crosswire.jsword.book.BookMetaData;
 36  
 import org.crosswire.jsword.book.Books;
 37  
 import org.crosswire.jsword.passage.Key;
 38  
 import org.xml.sax.SAXException;
 39  
 
 40  
 /**
 41  
  * Start of a mechanism to extract a Dictionary module to OSIS.
 42  
  * 
 43  
  * @see gnu.lgpl.License The GNU Lesser General Public License for details.
 44  
  * @author DM Smith
 45  
  */
 46  0
 public class DictToOsis {
 47  
     /**
 48  
      * The name of a Bible to find
 49  
      */
 50  
     private static final String BOOK_NAME = "WebstersDict";
 51  
 
 52  
     /**
 53  
      * @param args
 54  
      * @throws BookException 
 55  
      * @throws IOException 
 56  
      */
 57  
     public static void main(String[] args) throws BookException, IOException {
 58  0
         new DictToOsis().dump(BOOK_NAME);
 59  0
     }
 60  
 
 61  
     public void dump(String name) throws BookException, IOException {
 62  0
         Books books = Books.installed();
 63  0
         Book book = books.getBook(name);
 64  0
         BookMetaData bmd = book.getBookMetaData();
 65  0
         StringBuffer buf = new StringBuffer();
 66  
 
 67  0
         Key keys = book.getGlobalKeyList();
 68  
 
 69  0
         buildDocumentOpen(buf, bmd);
 70  
 
 71  
         // Get a verse iterator
 72  0
         for (Key key : keys) {
 73  0
             BookData bdata = new BookData(book, key);
 74  0
             SAXEventProvider osissep = bdata.getSAXEventProvider();
 75  
             try {
 76  0
                 buildEntryOpen(buf, key.getName(), XMLUtil.writeToString(osissep));
 77  0
             } catch (SAXException e) {
 78  0
                 e.printStackTrace(System.err);
 79  0
             }
 80  0
         }
 81  
 
 82  0
         buildDocumentClose(buf);
 83  
 
 84  0
         Writer writer = null;
 85  
         try {
 86  0
             writer = new OutputStreamWriter(new FileOutputStream(bmd.getInitials() + ".xml"), "UTF-8");
 87  0
             writer.write(buf.toString());
 88  
         } finally {
 89  0
             if (writer != null) {
 90  0
                 writer.close();
 91  
             }
 92  
         }
 93  0
         XMLProcess parser = new XMLProcess();
 94  
         // parser.getFeatures().setFeatureStates("-s", "-f", "-va", "-dv");
 95  0
         parser.parse(bmd.getInitials() + ".xml");
 96  0
     }
 97  
 
 98  
     private void buildDocumentOpen(StringBuffer buf, BookMetaData bmd) {
 99  0
         MessageFormat msgFormat = new MessageFormat(
 100  
                 "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<osis\n  xmlns=\"http://www.bibletechnologies.net/2003/OSIS/namespace\"\n  xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n  xsi:schemaLocation=\"http://www.bibletechnologies.net/2003/OSIS/namespace osisCore.2.1.xsd\">\n<osisText osisIDWork=\"{0}\" osisRefWork=\"defaultReferenceScheme\" xml:lang=\"en\">\n  <header>\n    <work osisWork=\"{0}\">\n      <title>{1}</title>\n      <identifier type=\"OSIS\">Dict.{0}</identifier>\n      <refSystem>Dict.{0}</refSystem>\n    </work>\n    <work osisWork=\"defaultReferenceScheme\">\n      <refSystem>Dict.{0}</refSystem>\n    </work>\n  </header>\n<div>\n");
 101  0
         msgFormat.format(new Object[] {
 102  
                 bmd.getInitials(), bmd.getName()
 103  
         }, buf, pos);
 104  0
     }
 105  
 
 106  
     private void buildDocumentClose(StringBuffer buf) {
 107  0
         buf.append("</div>\n</osisText>\n</osis>\n");
 108  0
     }
 109  
 
 110  
     private void buildEntryOpen(StringBuffer buf, String entryName, String entryDef) {
 111  0
         String tmp = entryName;
 112  0
         if (tmp.indexOf(' ') != -1) {
 113  0
             tmp = "x";
 114  
         }
 115  0
         MessageFormat msgFormat = new MessageFormat(
 116  
                 "<div type=\"entry\" osisID=\"{0}\" canonical=\"true\"><seg type=\"x-form\"><seg type=\"x-orth\">{0}</seg></seg><seg type=\"x-def\">{1}</seg></div>\n");
 117  0
         msgFormat.format(new Object[] {
 118  
                 tmp, entryDef
 119  
         }, buf, pos);
 120  0
     }
 121  
 
 122  0
     private static FieldPosition pos = new FieldPosition(0);
 123  
 }