Coverage Report - org.crosswire.jsword.book.filter.thml.SyncTag
 
Classes in this File Line Coverage Branch Coverage Complexity
SyncTag
0%
0/81
0%
0/40
14.5
 
 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.book.filter.thml;
 21  
 
 22  
 import java.util.List;
 23  
 
 24  
 import org.crosswire.jsword.book.Book;
 25  
 import org.crosswire.jsword.book.DataPolice;
 26  
 import org.crosswire.jsword.book.OSISUtil;
 27  
 import org.crosswire.jsword.passage.Key;
 28  
 import org.jdom2.Content;
 29  
 import org.jdom2.Element;
 30  
 import org.jdom2.Text;
 31  
 import org.xml.sax.Attributes;
 32  
 
 33  
 /**
 34  
  * THML Tag to process the sync element. A sync tag is always empty and
 35  
  * immediately follows what it marks. With types of Strong's and morph these are
 36  
  * to become w elements that surround the word that they modify. This requires
 37  
  * that we find the last text element and surround it with a w element. If the
 38  
  * last text element is already surrounded with a w element then this is added
 39  
  * to it. As a simplifying assumption, we will assume that the text element is
 40  
  * not contained by anything except perhaps by a w element.
 41  
  * 
 42  
  * @see gnu.lgpl.License The GNU Lesser General Public License for details.
 43  
  * @author Joe Walker
 44  
  */
 45  0
 public class SyncTag extends AbstractTag {
 46  
     /* (non-Javadoc)
 47  
      * @see org.crosswire.jsword.book.filter.thml.Tag#getTagName()
 48  
      */
 49  
     public String getTagName() {
 50  0
         return "sync";
 51  
     }
 52  
 
 53  
     @Override
 54  
     public Element processTag(Book book, Key key, Element ele, Attributes attrs) {
 55  
         // Strong's reference
 56  0
         String type = attrs.getValue("type");
 57  0
         String value = attrs.getValue("value");
 58  
 
 59  0
         if ("Strongs".equals(type)) {
 60  0
             List<Content> siblings = ele.getContent();
 61  0
             int size = siblings.size();
 62  0
             if (size == 0) {
 63  0
                 return null;
 64  
             }
 65  0
             Content lastEle = siblings.get(size - 1);
 66  0
             if (lastEle instanceof Text) {
 67  0
                 Element w = OSISUtil.factory().createW();
 68  0
                 w.setAttribute(OSISUtil.ATTRIBUTE_W_LEMMA, OSISUtil.LEMMA_STRONGS + value);
 69  0
                 siblings.set(size - 1, w);
 70  0
                 w.addContent(lastEle);
 71  0
             } else if (lastEle instanceof Element) {
 72  0
                 Element wEle = (Element) lastEle;
 73  0
                 if (wEle.getName().equals(OSISUtil.OSIS_ELEMENT_W)) {
 74  0
                     StringBuilder buf = new StringBuilder();
 75  0
                     String strongsAttr = wEle.getAttributeValue(OSISUtil.ATTRIBUTE_W_LEMMA);
 76  0
                     if (strongsAttr != null) {
 77  0
                         buf.append(strongsAttr);
 78  0
                         buf.append(' ');
 79  
                     }
 80  0
                     buf.append(OSISUtil.LEMMA_STRONGS);
 81  0
                     buf.append(value);
 82  0
                     wEle.setAttribute(OSISUtil.ATTRIBUTE_W_LEMMA, buf.toString());
 83  
                 }
 84  
             }
 85  0
             return null;
 86  
         }
 87  
 
 88  0
         if ("morph".equals(type)) {
 89  0
             List<Content> siblings = ele.getContent();
 90  0
             int size = siblings.size();
 91  0
             if (size == 0) {
 92  0
                 return null;
 93  
             }
 94  0
             Content lastEle = siblings.get(size - 1);
 95  0
             if (lastEle instanceof Text) {
 96  0
                 Element w = OSISUtil.factory().createW();
 97  0
                 w.setAttribute(OSISUtil.ATTRIBUTE_W_MORPH, OSISUtil.MORPH_ROBINSONS + value);
 98  0
                 siblings.set(size - 1, w);
 99  0
                 w.addContent(lastEle);
 100  0
             } else if (lastEle instanceof Element) {
 101  0
                 Element wEle = (Element) lastEle;
 102  0
                 if (wEle.getName().equals(OSISUtil.OSIS_ELEMENT_W)) {
 103  0
                     StringBuilder buf = new StringBuilder();
 104  0
                     String strongsAttr = wEle.getAttributeValue(OSISUtil.ATTRIBUTE_W_MORPH);
 105  0
                     if (strongsAttr != null) {
 106  0
                         buf.append(strongsAttr);
 107  0
                         buf.append(' ');
 108  
                     }
 109  0
                     buf.append(OSISUtil.MORPH_ROBINSONS);
 110  0
                     buf.append(value);
 111  0
                     wEle.setAttribute(OSISUtil.ATTRIBUTE_W_MORPH, buf.toString());
 112  
                 }
 113  
             }
 114  0
             return null;
 115  
         }
 116  
 
 117  0
         if ("lemma".equals(type)) {
 118  0
             List<Content> siblings = ele.getContent();
 119  0
             int size = siblings.size();
 120  0
             if (size == 0) {
 121  0
                 return null;
 122  
             }
 123  0
             Content lastEle = siblings.get(size - 1);
 124  0
             if (lastEle instanceof Text) {
 125  0
                 Element w = OSISUtil.factory().createW();
 126  0
                 w.setAttribute(OSISUtil.ATTRIBUTE_W_LEMMA, OSISUtil.LEMMA_MISC + value);
 127  0
                 siblings.set(size - 1, w);
 128  0
                 w.addContent(lastEle);
 129  0
             } else if (lastEle instanceof Element) {
 130  0
                 Element wEle = (Element) lastEle;
 131  0
                 if (wEle.getName().equals(OSISUtil.OSIS_ELEMENT_W)) {
 132  0
                     StringBuilder buf = new StringBuilder();
 133  0
                     String lemmaAttr = wEle.getAttributeValue(OSISUtil.ATTRIBUTE_W_LEMMA);
 134  0
                     if (lemmaAttr != null) {
 135  0
                         buf.append(lemmaAttr);
 136  0
                         buf.append(' ');
 137  
                     }
 138  0
                     buf.append(OSISUtil.LEMMA_MISC);
 139  0
                     buf.append(value);
 140  0
                     wEle.setAttribute(OSISUtil.ATTRIBUTE_W_LEMMA, buf.toString());
 141  
                 }
 142  
             }
 143  0
             return null;
 144  
         }
 145  
 
 146  0
         if ("Dict".equals(type)) {
 147  0
             Element div = OSISUtil.factory().createDiv();
 148  0
             div.setAttribute(OSISUtil.OSIS_ATTR_OSISID, "dict://" + value);
 149  
 
 150  0
             if (ele != null) {
 151  0
                 ele.addContent(div);
 152  
             }
 153  
 
 154  0
             return div;
 155  
         }
 156  
 
 157  0
         DataPolice.report(book, key, "sync tag has type=" + type + " when value=" + value);
 158  0
         return null;
 159  
     }
 160  
 }