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 as published by
5    * the Free Software Foundation. This program is distributed in the hope
6    * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7    * 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   * Copyright: 2005 - 2012
18   *     The copyright to this program is held by it's authors.
19   *
20   * ID: $Id: ScripRefTag.java 2221 2012-01-25 21:32:57Z dmsmith $
21   */
22  package org.crosswire.jsword.book.filter.thml;
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.crosswire.jsword.passage.NoSuchKeyException;
29  import org.crosswire.jsword.passage.Passage;
30  import org.jdom.Element;
31  import org.xml.sax.Attributes;
32  
33  /**
34   * THML Tag to process the scripRef element.
35   * 
36   * @see gnu.lgpl.License for license details.<br>
37   *      The copyright to this program is held by it's authors.
38   * @author Joe Walker [joe at eireneh dot com]
39   */
40  public class ScripRefTag extends AbstractTag {
41      /* (non-Javadoc)
42       * @see org.crosswire.jsword.book.filter.thml.Tag#getTagName()
43       */
44      public String getTagName() {
45          return "scripRef";
46      }
47  
48      @Override
49      public Element processTag(Book book, Key key, Element ele, Attributes attrs) {
50          Element reference = null;
51  
52          String refstr = attrs.getValue("passage");
53          if (refstr != null) {
54              Passage ref = null;
55              try {
56                  ref = (Passage) book.getKey(refstr);
57              } catch (NoSuchKeyException ex) {
58                  DataPolice.report(book, key, "Unparsable passage: (" + refstr + ") due to " + ex.getMessage());
59              }
60  
61              // If we don't have a Passage then use the original string
62              String osisname = ref != null ? ref.getOsisRef() : refstr;
63              reference = OSISUtil.factory().createReference();
64              reference.setAttribute(OSISUtil.OSIS_ATTR_REF, osisname);
65          } else {
66              // The reference will be filled in by processContent
67              reference = OSISUtil.factory().createReference();
68          }
69  
70          if (ele != null) {
71              ele.addContent(reference);
72          }
73  
74          return reference;
75      }
76  
77      @Override
78      public void processContent(Book book, Key key, Element ele) {
79          String refstr = ele.getValue();
80          try {
81              if (ele.getAttribute(OSISUtil.OSIS_ATTR_REF) == null) {
82                  Passage ref = (Passage) book.getKey(refstr);
83                  String osisname = ref.getOsisRef();
84                  ele.setAttribute(OSISUtil.OSIS_ATTR_REF, osisname);
85              }
86          } catch (NoSuchKeyException ex) {
87              DataPolice.report(book, key, "scripRef has no passage attribute, unable to guess: (" + refstr + ") due to " + ex.getMessage());
88          }
89      }
90  }
91