Desktop tool for browsing account info from EVE-Online
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProcessingInstruction.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*--
  2. $Id: ProcessingInstruction.java,v 1.47 2007/11/10 05:28:59 jhunter Exp $
  3. Copyright (C) 2000-2007 Jason Hunter & Brett McLaughlin.
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. 1. Redistributions of source code must retain the above copyright
  9. notice, this list of conditions, and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions, and the disclaimer that follows
  12. these conditions in the documentation and/or other materials
  13. provided with the distribution.
  14. 3. The name "JDOM" must not be used to endorse or promote products
  15. derived from this software without prior written permission. For
  16. written permission, please contact <request_AT_jdom_DOT_org>.
  17. 4. Products derived from this software may not be called "JDOM", nor
  18. may "JDOM" appear in their name, without prior written permission
  19. from the JDOM Project Management <request_AT_jdom_DOT_org>.
  20. In addition, we request (but do not require) that you include in the
  21. end-user documentation provided with the redistribution and/or in the
  22. software itself an acknowledgement equivalent to the following:
  23. "This product includes software developed by the
  24. JDOM Project (http://www.jdom.org/)."
  25. Alternatively, the acknowledgment may be graphical using the logos
  26. available at http://www.jdom.org/images/logos.
  27. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  28. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  29. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  30. DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
  31. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  33. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  34. USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  35. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  36. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  38. SUCH DAMAGE.
  39. This software consists of voluntary contributions made by many
  40. individuals on behalf of the JDOM Project and was originally
  41. created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
  42. Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
  43. on the JDOM Project, please see <http://www.jdom.org/>.
  44. */
  45. package org.jdom;
  46. import java.util.*;
  47. /**
  48. * An XML processing instruction. Methods allow the user to obtain the target of
  49. * the PI as well as its data. The data can always be accessed as a String or,
  50. * if the data appears akin to an attribute list, can be retrieved as name/value
  51. * pairs.
  52. *
  53. * @version $Revision: 1.47 $, $Date: 2007/11/10 05:28:59 $
  54. * @author Brett McLaughlin
  55. * @author Jason Hunter
  56. * @author Steven Gould
  57. */
  58. public class ProcessingInstruction extends Content {
  59. private static final String CVS_ID =
  60. "@(#) $RCSfile: ProcessingInstruction.java,v $ $Revision: 1.47 $ $Date: 2007/11/10 05:28:59 $ $Name: jdom_1_1 $";
  61. /** The target of the PI */
  62. protected String target;
  63. /** The data for the PI as a String */
  64. protected String rawData;
  65. /** The data for the PI in name/value pairs */
  66. protected Map mapData;
  67. /**
  68. * Default, no-args constructor for implementations
  69. * to use if needed.
  70. */
  71. protected ProcessingInstruction() { }
  72. /**
  73. * This will create a new <code>ProcessingInstruction</code>
  74. * with the specified target and data.
  75. *
  76. * @param target <code>String</code> target of PI.
  77. * @param data <code>Map</code> data for PI, in
  78. * name/value pairs
  79. * @throws IllegalTargetException if the given target is illegal
  80. * as a processing instruction name.
  81. */
  82. public ProcessingInstruction(String target, Map data) {
  83. setTarget(target);
  84. setData(data);
  85. }
  86. /**
  87. * This will create a new <code>ProcessingInstruction</code>
  88. * with the specified target and data.
  89. *
  90. * @param target <code>String</code> target of PI.
  91. * @param data <code>String</code> data for PI.
  92. * @throws IllegalTargetException if the given target is illegal
  93. * as a processing instruction name.
  94. */
  95. public ProcessingInstruction(String target, String data) {
  96. setTarget(target);
  97. setData(data);
  98. }
  99. /**
  100. * This will set the target for the PI.
  101. *
  102. * @param newTarget <code>String</code> new target of PI.
  103. * @return <code>ProcessingInstruction</code> - this PI modified.
  104. */
  105. public ProcessingInstruction setTarget(String newTarget) {
  106. String reason;
  107. if ((reason = Verifier.checkProcessingInstructionTarget(newTarget))
  108. != null) {
  109. throw new IllegalTargetException(newTarget, reason);
  110. }
  111. target = newTarget;
  112. return this;
  113. }
  114. /**
  115. * Returns the XPath 1.0 string value of this element, which is the
  116. * data of this PI.
  117. *
  118. * @return the data of this PI
  119. */
  120. public String getValue() {
  121. return rawData;
  122. }
  123. /**
  124. * This will retrieve the target of the PI.
  125. *
  126. * @return <code>String</code> - target of PI.
  127. */
  128. public String getTarget() {
  129. return target;
  130. }
  131. /**
  132. * This will return the raw data from all instructions.
  133. *
  134. * @return <code>String</code> - data of PI.
  135. */
  136. public String getData() {
  137. return rawData;
  138. }
  139. /**
  140. * This will return a <code>List</code> containing the names of the
  141. * "attribute" style pieces of name/value pairs in this PI's data.
  142. *
  143. * @return <code>List</code> - the <code>List</code> containing the
  144. * "attribute" names.
  145. */
  146. public List getPseudoAttributeNames() {
  147. Set mapDataSet = mapData.entrySet();
  148. List nameList = new ArrayList();
  149. for (Iterator i = mapDataSet.iterator(); i.hasNext();) {
  150. String wholeSet = (i.next()).toString();
  151. String attrName = wholeSet.substring(0,(wholeSet.indexOf("=")));
  152. nameList.add(attrName);
  153. }
  154. return nameList;
  155. }
  156. /**
  157. * This will set the raw data for the PI.
  158. *
  159. * @param data <code>String</code> data of PI.
  160. * @return <code>ProcessingInstruction</code> - this PI modified.
  161. */
  162. public ProcessingInstruction setData(String data) {
  163. String reason = Verifier.checkProcessingInstructionData(data);
  164. if (reason != null) {
  165. throw new IllegalDataException(data, reason);
  166. }
  167. this.rawData = data;
  168. this.mapData = parseData(data);
  169. return this;
  170. }
  171. /**
  172. * This will set the name/value pairs within the passed
  173. * <code>Map</code> as the pairs for the data of
  174. * this PI. The keys should be the pair name
  175. * and the values should be the pair values.
  176. *
  177. * @param data new map data to use
  178. * @return <code>ProcessingInstruction</code> - modified PI.
  179. */
  180. public ProcessingInstruction setData(Map data) {
  181. String temp = toString(data);
  182. String reason = Verifier.checkProcessingInstructionData(temp);
  183. if (reason != null) {
  184. throw new IllegalDataException(temp, reason);
  185. }
  186. this.rawData = temp;
  187. this.mapData = data;
  188. return this;
  189. }
  190. /**
  191. * This will return the value for a specific
  192. * name/value pair on the PI. If no such pair is
  193. * found for this PI, null is returned.
  194. *
  195. * @param name <code>String</code> name of name/value pair
  196. * to lookup value for.
  197. * @return <code>String</code> - value of name/value pair.
  198. */
  199. public String getPseudoAttributeValue(String name) {
  200. return (String)mapData.get(name);
  201. }
  202. /**
  203. * This will set a pseudo attribute with the given name and value.
  204. * If the PI data is not already in a pseudo-attribute format, this will
  205. * replace the existing data.
  206. *
  207. * @param name <code>String</code> name of pair.
  208. * @param value <code>String</code> value for pair.
  209. * @return <code>ProcessingInstruction</code> this PI modified.
  210. */
  211. public ProcessingInstruction setPseudoAttribute(String name, String value) {
  212. String reason = Verifier.checkProcessingInstructionData(name);
  213. if (reason != null) {
  214. throw new IllegalDataException(name, reason);
  215. }
  216. reason = Verifier.checkProcessingInstructionData(value);
  217. if (reason != null) {
  218. throw new IllegalDataException(value, reason);
  219. }
  220. this.mapData.put(name, value);
  221. this.rawData = toString(mapData);
  222. return this;
  223. }
  224. /**
  225. * This will remove the pseudo attribute with the specified name.
  226. *
  227. * @param name name of pseudo attribute to remove
  228. * @return <code>boolean</code> - whether the requested
  229. * instruction was removed.
  230. */
  231. public boolean removePseudoAttribute(String name) {
  232. if ((mapData.remove(name)) != null) {
  233. rawData = toString(mapData);
  234. return true;
  235. }
  236. return false;
  237. }
  238. /**
  239. * This will convert the Map to a string representation.
  240. *
  241. * @param mapData <code>Map</code> PI data to convert
  242. * @return a string representation of the Map as appropriate for a PI
  243. */
  244. private String toString(Map mapData) {
  245. StringBuffer rawData = new StringBuffer();
  246. Iterator i = mapData.keySet().iterator();
  247. while (i.hasNext()) {
  248. String name = (String)i.next();
  249. String value = (String)mapData.get(name);
  250. rawData.append(name)
  251. .append("=\"")
  252. .append(value)
  253. .append("\" ");
  254. }
  255. // Remove last space, if we did any appending
  256. if (rawData.length() > 0) {
  257. rawData.setLength(rawData.length() - 1);
  258. }
  259. return rawData.toString();
  260. }
  261. /**
  262. * This will parse and load the instructions for the PI.
  263. * This is separated to allow it to occur once and then be reused.
  264. */
  265. private Map parseData(String rawData) {
  266. // The parsing here is done largely "by hand" which means the code
  267. // gets a little tricky/messy. The following conditions should
  268. // now be handled correctly:
  269. // <?pi href="http://hi/a=b"?> Reads OK
  270. // <?pi href = 'http://hi/a=b' ?> Reads OK
  271. // <?pi href\t = \t'http://hi/a=b'?> Reads OK
  272. // <?pi href = "http://hi/a=b"?> Reads OK
  273. // <?pi?> Empty Map
  274. // <?pi id=22?> Empty Map
  275. // <?pi id='22?> Empty Map
  276. Map data = new HashMap();
  277. // System.out.println("rawData: " + rawData);
  278. // The inputData variable holds the part of rawData left to parse
  279. String inputData = rawData.trim();
  280. // Iterate through the remaining inputData string
  281. while (!inputData.trim().equals("")) {
  282. //System.out.println("parseData() looking at: " + inputData);
  283. // Search for "name =", "name=" or "name1 name2..."
  284. String name = "";
  285. String value = "";
  286. int startName = 0;
  287. char previousChar = inputData.charAt(startName);
  288. int pos = 1;
  289. for (; pos<inputData.length(); pos++) {
  290. char currentChar = inputData.charAt(pos);
  291. if (currentChar == '=') {
  292. name = inputData.substring(startName, pos).trim();
  293. // Get the boundaries on the quoted string
  294. // We use boundaries so we know where to start next
  295. int[] bounds = extractQuotedString(
  296. inputData.substring(pos+1));
  297. // A null value means a parse error and we return empty!
  298. if (bounds == null) {
  299. return new HashMap();
  300. }
  301. value = inputData.substring(bounds[0]+pos+1,
  302. bounds[1]+pos+1);
  303. pos += bounds[1] + 1; // skip past value
  304. break;
  305. }
  306. else if (Character.isWhitespace(previousChar)
  307. && !Character.isWhitespace(currentChar)) {
  308. startName = pos;
  309. }
  310. previousChar = currentChar;
  311. }
  312. // Remove the first pos characters; they have been processed
  313. inputData = inputData.substring(pos);
  314. // System.out.println("Extracted (name, value) pair: ("
  315. // + name + ", '" + value+"')");
  316. // If both a name and a value have been found, then add
  317. // them to the data Map
  318. if (name.length() > 0 && value != null) {
  319. //if (data.containsKey(name)) {
  320. // A repeat, that's a parse error, so return a null map
  321. //return new HashMap();
  322. //}
  323. //else {
  324. data.put(name, value);
  325. //}
  326. }
  327. }
  328. return data;
  329. }
  330. /**
  331. * This is a helper routine, only used by parseData, to extract a
  332. * quoted String from the input parameter, rawData. A quoted string
  333. * can use either single or double quotes, but they must match up.
  334. * A singly quoted string can contain an unbalanced amount of double
  335. * quotes, or vice versa. For example, the String "JDOM's the best"
  336. * is legal as is 'JDOM"s the best'.
  337. *
  338. * @param rawData the input string from which a quoted string is to
  339. * be extracted.
  340. * @return the first quoted string encountered in the input data. If
  341. * no quoted string is found, then the empty string, "", is
  342. * returned.
  343. * @see #parseData
  344. */
  345. private static int[] extractQuotedString(String rawData) {
  346. // Remembers whether we're actually in a quoted string yet
  347. boolean inQuotes = false;
  348. // Remembers which type of quoted string we're in
  349. char quoteChar = '"';
  350. // Stores the position of the first character inside
  351. // the quoted string (i.e. the start of the return string)
  352. int start = 0;
  353. // Iterate through the input string looking for the start
  354. // and end of the quoted string
  355. for (int pos=0; pos < rawData.length(); pos++) {
  356. char currentChar = rawData.charAt(pos);
  357. if (currentChar=='"' || currentChar=='\'') {
  358. if (!inQuotes) {
  359. // We're entering a quoted string
  360. quoteChar = currentChar;
  361. inQuotes = true;
  362. start = pos+1;
  363. }
  364. else if (quoteChar == currentChar) {
  365. // We're leaving a quoted string
  366. inQuotes = false;
  367. return new int[] { start, pos };
  368. }
  369. // Otherwise we've encountered a quote
  370. // inside a quote, so just continue
  371. }
  372. }
  373. return null;
  374. }
  375. /**
  376. * This returns a <code>String</code> representation of the
  377. * <code>ProcessingInstruction</code>, suitable for debugging. If the XML
  378. * representation of the <code>ProcessingInstruction</code> is desired,
  379. * {@link org.jdom.output.XMLOutputter#outputString(ProcessingInstruction)}
  380. * should be used.
  381. *
  382. * @return <code>String</code> - information about the
  383. * <code>ProcessingInstruction</code>
  384. */
  385. public String toString() {
  386. return new StringBuffer()
  387. .append("[ProcessingInstruction: ")
  388. .append(new org.jdom.output.XMLOutputter().outputString(this))
  389. .append("]")
  390. .toString();
  391. }
  392. /**
  393. * This will return a clone of this <code>ProcessingInstruction</code>.
  394. *
  395. * @return <code>Object</code> - clone of this
  396. * <code>ProcessingInstruction</code>.
  397. */
  398. public Object clone() {
  399. ProcessingInstruction pi = (ProcessingInstruction) super.clone();
  400. // target and rawdata are immutable and references copied by
  401. // Object.clone()
  402. // Create a new Map object for the clone (since Map isn't Cloneable)
  403. if (mapData != null) {
  404. pi.mapData = parseData(rawData);
  405. }
  406. return pi;
  407. }
  408. }