IXmlSerializer.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /* -*- c-basic-offset: 4; indent-tabs-mode: nil; -*- //------100-columns-wide------>|*/
  2. // for license please see accompanying XmlPull license file (available also at http://www.xmlpull.org/)
  3. package org.xmlpull.v1;
  4. import java.io.IOException;
  5. import java.io.OutputStream;
  6. import java.io.Writer;
  7. /**
  8. * Define an interface to serialziation of XML Infoset.
  9. * This interface abstracts away if serialized XML is XML 1.0 comaptible text or
  10. * other formats of XML 1.0 serializations (such as binary XML for example with WBXML).
  11. *
  12. * <p><b>PLEASE NOTE:</b> This interface will be part of XmlPull 1.2 API.
  13. * It is included as basis for discussion. It may change in any way.
  14. *
  15. * <p>Exceptions that may be thrown are: IOException or runtime exception
  16. * (more runtime exceptions can be thrown but are not declared and as such
  17. * have no semantics defined for this interface):
  18. * <ul>
  19. * <li><em>IllegalArgumentException</em> - for almost all methods to signal that
  20. * argument is illegal
  21. * <li><em>IllegalStateException</em> - to signal that call has good arguments but
  22. * is not expected here (violation of contract) and for features/properties
  23. * when requesting setting unimplemented feature/property
  24. * (UnsupportedOperationException would be better but it is not in MIDP)
  25. * </ul>
  26. *
  27. * <p><b>NOTE:</b> writing CDSECT, ENTITY_REF, IGNORABLE_WHITESPACE,
  28. * PROCESSING_INSTRUCTION, COMMENT, and DOCDECL in some implementations
  29. * may not be supported (for example when serializing to WBXML).
  30. * In such case IllegalStateException will be thrown and it is recommened
  31. * to use an optional feature to signal that implementation is not
  32. * supporting this kind of output.
  33. */
  34. public interface IXmlSerializer {
  35. /**
  36. * Set feature identified by name (recommended to be URI for uniqueness).
  37. * Some well known optional features are defined in
  38. * <a href="http://www.xmlpull.org/v1/doc/features.html">
  39. * http://www.xmlpull.org/v1/doc/features.html</a>.
  40. *
  41. * If feature is not recocgnized or can not be set
  42. * then IllegalStateException MUST be thrown.
  43. *
  44. * @exception IllegalStateException If the feature is not supported or can not be set
  45. */
  46. void setFeature(String name,
  47. boolean state)
  48. throws IllegalArgumentException, IllegalStateException;
  49. /**
  50. * Return the current value of the feature with given name.
  51. * <p><strong>NOTE:</strong> unknown properties are <strong>always</strong> returned as null
  52. *
  53. * @param name The name of feature to be retrieved.
  54. * @return The value of named feature.
  55. * @exception IllegalArgumentException if feature string is null
  56. */
  57. boolean getFeature(String name);
  58. /**
  59. * Set the value of a property.
  60. * (the property name is recommened to be URI for uniqueness).
  61. * Some well known optional properties are defined in
  62. * <a href="http://www.xmlpull.org/v1/doc/properties.html">
  63. * http://www.xmlpull.org/v1/doc/properties.html</a>.
  64. *
  65. * If property is not recocgnized or can not be set
  66. * then IllegalStateException MUST be thrown.
  67. *
  68. * @exception IllegalStateException if the property is not supported or can not be set
  69. */
  70. void setProperty(String name,
  71. Object value)
  72. throws IllegalArgumentException, IllegalStateException;
  73. /**
  74. * Look up the value of a property.
  75. *
  76. * The property name is any fully-qualified URI. I
  77. * <p><strong>NOTE:</strong> unknown properties are <string>always</strong> returned as null
  78. *
  79. * @param name The name of property to be retrieved.
  80. * @return The value of named property.
  81. */
  82. Object getProperty(String name);
  83. /**
  84. * Set to use binary output stream with given encoding.
  85. */
  86. void setOutput (OutputStream os, String encoding)
  87. throws IOException, IllegalArgumentException, IllegalStateException;
  88. /**
  89. * Set the output to the given writer.
  90. * <p><b>WARNING</b> no information about encoding is available!
  91. */
  92. void setOutput (Writer writer)
  93. throws IOException, IllegalArgumentException, IllegalStateException;
  94. /**
  95. * Write &lt;&#63;xml declaration with encoding (if encoding not null)
  96. * and standalone flag (if standalone not null)
  97. * This method can only be called just after setOutput.
  98. */
  99. void startDocument (String encoding, Boolean standalone)
  100. throws IOException, IllegalArgumentException, IllegalStateException;
  101. /**
  102. * Finish writing. All unclosed start tags will be closed and output
  103. * will be flushed. After calling this method no more output can be
  104. * serialized until next call to setOutput()
  105. */
  106. void endDocument ()
  107. throws IOException, IllegalArgumentException, IllegalStateException;
  108. /**
  109. * Binds the given prefix to the given namespace.
  110. * This call is valid for the next element including child elements.
  111. * The prefix and namespace MUST be always declared even if prefix
  112. * is not used in element (startTag() or attribute()) - for XML 1.0
  113. * it must result in declaring <code>xmlns:prefix='namespace'</code>
  114. * (or <code>xmlns:prefix="namespace"</code> depending what character is used
  115. * to quote attribute value).
  116. *
  117. * <p><b>NOTE:</b> this method MUST be called directly before startTag()
  118. * and if anything but startTag() or setPrefix() is called next there will be exception.
  119. * <p><b>NOTE:</b> prefixes "xml" and "xmlns" are already bound
  120. * and can not be redefined see:
  121. * <a href="http://www.w3.org/XML/xml-names-19990114-errata#NE05">Namespaces in XML Errata</a>.
  122. * <p><b>NOTE:</b> to set default namespace use as prefix empty string.
  123. *
  124. * @param prefix must be not null (or IllegalArgumentException is thrown)
  125. * @param namespace must be not null
  126. */
  127. void setPrefix (String prefix, String namespace)
  128. throws IOException, IllegalArgumentException, IllegalStateException;
  129. /**
  130. * Return namespace that corresponds to given prefix
  131. * If there is no prefix bound to this namespace return null
  132. * but if generatePrefix is false then return generated prefix.
  133. *
  134. * <p><b>NOTE:</b> if the prefix is empty string "" and defualt namespace is bound
  135. * to this prefix then empty string ("") is returned.
  136. *
  137. * <p><b>NOTE:</b> prefixes "xml" and "xmlns" are already bound
  138. * will have values as defined
  139. * <a href="http://www.w3.org/TR/REC-xml-names/">Namespaces in XML specification</a>
  140. */
  141. String getPrefix (String namespace, boolean generatePrefix)
  142. throws IllegalArgumentException;
  143. /**
  144. * Returns the current depth of the element.
  145. * Outside the root element, the depth is 0. The
  146. * depth is incremented by 1 when startTag() is called.
  147. * The depth is decremented after the call to endTag()
  148. * event was observed.
  149. *
  150. * <pre>
  151. * &lt;!-- outside --&gt; 0
  152. * &lt;root&gt; 1
  153. * sometext 1
  154. * &lt;foobar&gt; 2
  155. * &lt;/foobar&gt; 2
  156. * &lt;/root&gt; 1
  157. * &lt;!-- outside --&gt; 0
  158. * </pre>
  159. */
  160. int getDepth();
  161. /**
  162. * Returns the namespace URI of the current element as set by startTag().
  163. *
  164. * <p><b>NOTE:</b> that measn in particaulr that: <ul>
  165. * <li>if there was startTag("", ...) then getNamespace() returns ""
  166. * <li>if there was startTag(null, ...) then getNamespace() returns null
  167. * </ul>
  168. *
  169. * @return namespace set by startTag() that is currently in scope
  170. */
  171. String getNamespace ();
  172. /**
  173. * Returns the name of the current element as set by startTag().
  174. * It can only be null before first call to startTag()
  175. * or when last endTag() is called to close first startTag().
  176. *
  177. * @return namespace set by startTag() that is currently in scope
  178. */
  179. String getName();
  180. /**
  181. * Writes a start tag with the given namespace and name.
  182. * If there is no prefix defined for the given namespace,
  183. * a prefix will be defined automatically.
  184. * The explicit prefixes for namespaces can be established by calling setPrefix()
  185. * immediately before this method.
  186. * If namespace is null no namespace prefix is printed but just name.
  187. * If namespace is empty string then serialzier will make sure that
  188. * default empty namespace is declared (in XML 1.0 xmlns='')
  189. * or throw IllegalStateException if default namespace is already bound
  190. * to non-empty string.
  191. */
  192. IXmlSerializer startTag (String namespace, String name)
  193. throws IOException, IllegalArgumentException, IllegalStateException;
  194. /**
  195. * Write an attribute. Calls to attribute() MUST follow a call to
  196. * startTag() immediately. If there is no prefix defined for the
  197. * given namespace, a prefix will be defined automatically.
  198. * If namespace is null or empty string
  199. * no namespace prefix is printed but just name.
  200. */
  201. IXmlSerializer attribute (String namespace, String name, String value)
  202. throws IOException, IllegalArgumentException, IllegalStateException;
  203. /**
  204. * Write end tag. Repetition of namespace and name is just for avoiding errors.
  205. * <p><b>Background:</b> in kXML endTag had no arguments, and non matching tags were
  206. * very difficult to find...
  207. * If namespace is null no namespace prefix is printed but just name.
  208. * If namespace is empty string then serialzier will make sure that
  209. * default empty namespace is declared (in XML 1.0 xmlns='').
  210. */
  211. IXmlSerializer endTag (String namespace, String name)
  212. throws IOException, IllegalArgumentException, IllegalStateException;
  213. // /**
  214. // * Writes a start tag with the given namespace and name.
  215. // * <br />If there is no prefix defined (prefix == null) for the given namespace,
  216. // * a prefix will be defined automatically.
  217. // * <br />If explicit prefixes is passed (prefix != null) then it will be used
  218. // *and namespace declared if not already declared or
  219. // * throw IllegalStateException the same prefix was already set on this
  220. // * element (setPrefix()) and was bound to different namespace.
  221. // * <br />If namespace is null then prefix must be null too or IllegalStateException is thrown.
  222. // * <br />If namespace is null then no namespace prefix is printed but just name.
  223. // * <br />If namespace is empty string then serializer will make sure that
  224. // * default empty namespace is declared (in XML 1.0 xmlns='')
  225. // * or throw IllegalStateException if default namespace is already bound
  226. // * to non-empty string.
  227. // */
  228. // IXmlSerializer startTag (String prefix, String namespace, String name)
  229. // throws IOException, IllegalArgumentException, IllegalStateException;
  230. //
  231. // /**
  232. // * Write an attribute. Calls to attribute() MUST follow a call to
  233. // * startTag() immediately.
  234. // * <br />If there is no prefix defined (prefix == null) for the given namespace,
  235. // * a prefix will be defined automatically.
  236. // * <br />If explicit prefixes is passed (prefix != null) then it will be used
  237. // * and namespace declared if not already declared or
  238. // * throw IllegalStateException the same prefix was already set on this
  239. // * element (setPrefix()) and was bound to different namespace.
  240. // * <br />If namespace is null then prefix must be null too or IllegalStateException is thrown.
  241. // * <br />If namespace is null then no namespace prefix is printed but just name.
  242. // * <br />If namespace is empty string then serializer will make sure that
  243. // * default empty namespace is declared (in XML 1.0 xmlns='')
  244. // * or throw IllegalStateException if default namespace is already bound
  245. // * to non-empty string.
  246. // */
  247. // IXmlSerializer attribute (String prefix, String namespace, String name, String value)
  248. // throws IOException, IllegalArgumentException, IllegalStateException;
  249. //
  250. // /**
  251. // * Write end tag. Repetition of namespace, prefix, and name is just for avoiding errors.
  252. // * <br />If namespace or name arguments are different from corresponding startTag call
  253. // * then IllegalArgumentException is thrown, if prefix argument is not null and is different
  254. // * from corresponding starTag then IllegalArgumentException is thrown.
  255. // * <br />If namespace is null then prefix must be null too or IllegalStateException is thrown.
  256. // * <br />If namespace is null then no namespace prefix is printed but just name.
  257. // * <br />If namespace is empty string then serializer will make sure that
  258. // * default empty namespace is declared (in XML 1.0 xmlns='').
  259. // * <p><b>Background:</b> in kXML endTag had no arguments, and non matching tags were
  260. // * very difficult to find...</p>
  261. // */
  262. // ALEK: This is really optional as prefix in end tag MUST correspond to start tag but good for error checking
  263. // IXmlSerializer endTag (String prefix, String namespace, String name)
  264. // throws IOException, IllegalArgumentException, IllegalStateException;
  265. /**
  266. * Writes text, where special XML chars are escaped automatically
  267. */
  268. IXmlSerializer text (String text)
  269. throws IOException, IllegalArgumentException, IllegalStateException;
  270. /**
  271. * Writes text, where special XML chars are escaped automatically
  272. */
  273. IXmlSerializer text (char [] buf, int start, int len)
  274. throws IOException, IllegalArgumentException, IllegalStateException;
  275. void cdsect (String text)
  276. throws IOException, IllegalArgumentException, IllegalStateException;
  277. void entityRef (String text) throws IOException,
  278. IllegalArgumentException, IllegalStateException;
  279. void processingInstruction (String text)
  280. throws IOException, IllegalArgumentException, IllegalStateException;
  281. void comment (String text)
  282. throws IOException, IllegalArgumentException, IllegalStateException;
  283. void docdecl (String text)
  284. throws IOException, IllegalArgumentException, IllegalStateException;
  285. void ignorableWhitespace (String text)
  286. throws IOException, IllegalArgumentException, IllegalStateException;
  287. /**
  288. * Write all pending output to the stream.
  289. * If method startTag() or attribute() was called then start tag is closed (final &gt;)
  290. * before flush() is called on underlying output stream.
  291. *
  292. * <p><b>NOTE:</b> if there is need to close start tag
  293. * (so no more attribute() calls are allowed) but without flushinging output
  294. * call method text() with empty string (text("")).
  295. *
  296. */
  297. void flush ()
  298. throws IOException;
  299. }