The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
osisreferencelinks.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * osisreferencelinks.cpp - SWFilter descendant to toggle OSIS reference
4  * links with specified type/subType
5  *
6  * $Id: osisreferencelinks.cpp 2989 2013-10-09 08:22:21Z chrislit $
7  *
8  * Copyright 2001-2013 CrossWire Bible Society (http://www.crosswire.org)
9  * CrossWire Bible Society
10  * P. O. Box 2528
11  * Tempe, AZ 85280-2528
12  *
13  * This program is free software; you can redistribute it and/or modify it
14  * under the terms of the GNU General Public License as published by the
15  * Free Software Foundation version 2.
16  *
17  * This program is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  * General Public License for more details.
21  *
22  */
23 
24 #include <stdlib.h>
25 #include <osisreferencelinks.h>
26 #include <utilxml.h>
27 #include <swmodule.h>
28 
29 
31 
32 
33 namespace {
34 
35  static const StringList *oValues() {
36  static const SWBuf choices[3] = {"On", "Off", ""};
37  static const StringList oVals(&choices[0], &choices[2]);
38  return &oVals;
39  }
40 }
41 
42 
43 OSISReferenceLinks::OSISReferenceLinks(const char *optionName, const char *optionTip, const char *type, const char *subType, const char *defaultValue)
44  : SWOptionFilter(),
45  optionName(optionName),
46  optionTip(optionTip),
47  type(type),
48  subType(subType) {
49 
50  optName = this->optionName.c_str();
51  optTip = this->optionTip.c_str();
52  optValues = oValues();
53  setOptionValue(defaultValue);
54 }
55 
56 
58 }
59 
60 
61 char OSISReferenceLinks::processText(SWBuf &text, const SWKey *key, const SWModule *module) {
62  if (option) return 0;
63 
64  SWBuf token;
65  bool intoken = false;
66  bool stripThisToken = false;
67 
68  SWBuf orig = text;
69  const char *from = orig.c_str();
70 
71  for (text = ""; *from; ++from) {
72  if (*from == '<') {
73  intoken = true;
74  token = "";
75  continue;
76  }
77  else if (*from == '>') { // process tokens
78  intoken = false;
79  if (strncmp(token, "reference", 9) && strncmp(token.c_str(), "/reference", 10)) {
80  text.append('<');
81  text.append(token);
82  text.append('>');
83  }
84  else {
85  XMLTag tag;
86  tag = token;
87  if (!tag.isEndTag() && type == tag.getAttribute("type") && (!subType.size() || subType == tag.getAttribute("subType"))) {
88  stripThisToken = true;
89  continue;
90  }
91  else if (tag.isEndTag() && stripThisToken) {
92  stripThisToken = false;
93  continue;
94  }
95  text.append('<');
96  text.append(token);
97  text.append('>');
98  }
99  continue;
100  }
101 
102  if (intoken) { //copy token
103  token.append(*from);
104  }
105  else { //copy text which is not inside a token
106  text.append(*from);
107  }
108  }
109  return 0;
110 }
111 
113 
114 
#define SWORD_NAMESPACE_START
Definition: defs.h:39
Definition: swbuf.h:47
SWText * module
Definition: osis2mod.cpp:105
Definition: utilxml.h:38
static const StringList * oValues()
const char * c_str() const
Definition: swbuf.h:158
std::list< SWBuf > StringList
Definition: swmodule.cpp:91
SWBuf & append(const char *str, long max=-1)
Definition: swbuf.h:274
const char * optName
Definition: swoptfilter.h:51
unsigned long size() const
Definition: swbuf.h:185
static const char * choices[4]
const char * getAttribute(const char *attribName, int partNum=-1, char partSplit= '|') const
Definition: utilxml.cpp:230
bool isEndTag(const char *eID=0) const
Definition: utilxml.cpp:323
virtual void setOptionValue(const char *ival)
Definition: swoptfilter.cpp:52
const char * optTip
Definition: swoptfilter.h:52
const StringList * optValues
Definition: swoptfilter.h:53
#define SWORD_NAMESPACE_END
Definition: defs.h:40
Definition: swkey.h:77