CVS Update: xmlpull-api-v1/src/java/serializer_api/org/xmlpull/v1/serializer
Aleksander Andrzej Slominski <[email protected]>
| Newsgroups | gmane.text.xml.xmlpull.devel |
|---|---|
| Message-ID | <[email protected]> |
aslom 02/07/08 12:31:10
Modified: src/java/api/org/xmlpull/v1 XmlPullParserFactory.java
src/java/samples Roundtrip.java
Added: src/java/api/org/xmlpull/v1 XmlSerializer.java
Removed: src/java/serializer_api/org/xmlpull/v1/serializer
XmlSerializer.java
Log:
merged back XmlSerializer to main package org.xmlpull.v1
added XmlSerialzier creation to XMLPULL factory : newSerializer method
Revision Changes Path
1.14 +64 -13 xmlpull-api-v1/src/java/api/org/xmlpull/v1/XmlPullParserFactory.java
Index: XmlPullParserFactory.java
===================================================================
RCS file: /l/extreme/cvspub/xmlpull-api-v1/src/java/api/org/xmlpull/v1/XmlPullParserFactory.java,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -b -t -w -r1.13 -r1.14
--- XmlPullParserFactory.java 2002/04/21 22:19:35 1.13
+++ XmlPullParserFactory.java 2002/07/08 17:31:09 1.14
@@ -55,8 +55,11 @@
protected Vector parserClasses;
- protected String parserClassesLocation;
+ protected String classNamesLocation;
+ protected Vector serializerClasses;
+
+
// features are kept there
protected Hashtable features = new Hashtable();
@@ -159,7 +162,7 @@
public XmlPullParser newPullParser() throws XmlPullParserException {
if (parserClasses.size() == 0) throw new XmlPullParserException
- ("No valid parser classes found in "+parserClassesLocation);
+ ("No valid parser classes found in "+classNamesLocation);
StringBuffer issues = new StringBuffer ();
@@ -188,7 +191,46 @@
throw new XmlPullParserException ("could not create parser: "+issues);
}
+
/**
+ * Creates a new instance of a XML Serializer
+ * using the currently configured factory parameters.
+ *
+ * @return A new instance of a XML Serializer.
+ * @throws XmlPullParserException if a parser cannot be created which satisfies the
+ * requested configuration.
+ */
+
+ public XmlSerializer newSerializer() throws XmlPullParserException {
+
+ if (serializerClasses.size() == 0) throw new XmlPullParserException
+ ("No valid serializer classes found in "+classNamesLocation);
+
+ StringBuffer issues = new StringBuffer ();
+
+ for (int i = 0; i < serializerClasses.size (); i++) {
+ Class ppClass = (Class) serializerClasses.elementAt (i);
+ try {
+ XmlSerializer ser = (XmlSerializer) ppClass.newInstance();
+
+// for (Enumeration e = features.keys (); e.hasMoreElements ();) {
+// String key = (String) e.nextElement();
+// Boolean value = (Boolean) features.get(key);
+// if(value != null && value.booleanValue()) {
+// ser.setFeature(key, true);
+// }
+// }
+ return ser;
+
+ } catch(Exception ex) {
+ issues.append (ppClass.getName () + ": "+ ex.toString ()+"; ");
+ }
+ }
+
+ throw new XmlPullParserException ("could not create serializer: "+issues);
+ }
+
+ /**
* Create a new instance of a PullParserFactory that can be used
* to create XML pull parsers (see class description for more
* details).
@@ -206,7 +248,7 @@
if (context == null) context = "".getClass ();
- String parserClassesLocation = null;
+ String classNamesLocation = null;
if (classNames == null || classNames.length() == 0 || "DEFAULT".equals(classNames)) {
@@ -231,14 +273,15 @@
catch (Exception e) {
throw new XmlPullParserException (null, null, e);
}
- parserClassesLocation = RESOURCE_NAME+" that contained '"+classNames+"'";
+ classNamesLocation = "resource "+RESOURCE_NAME+" that contained '"+classNames+"'";
} else {
- parserClassesLocation =
+ classNamesLocation =
"parameter classNames to newInstance() that contained '"+classNames+"'";
}
XmlPullParserFactory factory = null;
Vector parserClasses = new Vector ();
+ Vector serializerClasses = new Vector ();
int pos = 0;
while (pos < classNames.length ()) {
@@ -258,25 +301,33 @@
catch (Exception e) {}
if (candidate != null) {
- if (instance instanceof XmlPullParser)
+ boolean recognized = false;
+ if (instance instanceof XmlPullParser) {
parserClasses.addElement (candidate);
- else if (instance instanceof XmlPullParserFactory) {
- if (factory == null)
+ recognized = true;
+ }
+ if (instance instanceof XmlSerializer) {
+ serializerClasses.addElement (candidate);
+ recognized = true;
+ }
+ if (instance instanceof XmlPullParserFactory) {
+ if (factory == null) {
factory = (XmlPullParserFactory) instance;
}
- else throw new XmlPullParserException ("incompatible class: "+name);
+ recognized = true;
}
-
+ if (!recognized) {
+ throw new XmlPullParserException ("incompatible class: "+name);
+ }
+ }
pos = cut + 1;
}
if (factory == null) factory = new XmlPullParserFactory ();
factory.parserClasses = parserClasses;
- factory.parserClassesLocation = parserClassesLocation;
+ factory.classNamesLocation = classNamesLocation;
return factory;
}
}
-
-
1.1 xmlpull-api-v1/src/java/api/org/xmlpull/v1/XmlSerializer.java
Index: XmlSerializer.java
===================================================================
package org.xmlpull.v1;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
/**
* Define an interface to serialziation of XML Infoset.
* This interface abstracts away if serialized XML is XML 1.0 comaptible text or
* other formats of XML 1.0 serializations (such as binary XML for example with WBXML).
*
* <p><b>PLEASE NOTE:</b> This interface is not part of the XmlPull 1.0 API (yet). It
* is just included as basis for discussion. It may change in any way.
*
*/
public interface XmlSerializer {
/**
* FEATURE: disable or enable serializer output indentation
* for elements on that on or below current depth
* (as defined by matching endTag() to current in-scope startTag()).
* If supported in the implementation then this featue MUST be scoped
* depending on startTag() and endTag() scope.
*
* <p><strong>NOTE:</strong> can be changed during parsing!
* <p><strong>NOTE:</strong> may be ignored by serializers
* (for example when serializer produces binary XML output like WBXML)
*
* @see #getFeature
* @see #setFeature
*/
public static final String FEATURE_INDENT_OUTPUT =
"http://xmlpull.org/v1/doc/features.html#indent-output";
/**
* Set feature identified by name (recommended to be URI for uniqueness).
* If feature is not recocgnized then IllegalArgumentException MUST be thrown.
*/
public void setFeature(String name,
boolean state) throws IllegalArgumentException;
/**
* Return the current value of the feature with given name.
* <p><strong>NOTE:</strong> unknown features are <string>always</strong> returned as false
*
* @param name The name of feature to be retrieved.
* @return The value of named feature.
* @exception IllegalArgumentException if feature string is null
*/
public boolean getFeature(String name);
/**
* Set the value of a property.
*
* The property name is any fully-qualified URI.
*/
public void setProperty(String name,
Object value) throws IllegalArgumentException;
/**
* Look up the value of a property.
*
* The property name is any fully-qualified URI. I
* <p><strong>NOTE:</strong> unknown properties are <string>always</strong> returned as null
*
* @param name The name of property to be retrieved.
* @return The value of named property.
*/
public Object getProperty(String name);
/**
* Set to use binary output stream with given encoding.
*/
public void setOutput (OutputStream os, String encoding) throws IOException;
/**
* sets the output to the given writer;
* insert big warning here -- no information about encoding is available
*/
public void setOutput (Writer writer) throws IOException;
public void startDocument (String encoding, Boolean standalone) throws IOException;
public void endDocument () throws IOException;
/**
* Binds the given prefix to the given namespace.
* valid for the next element including child elements.
*
* <p><b>NOTE:</b> this method MUST be called direclty before startTag()
* and if aything but startTag() or setPrefix() is called next there will be exception.
* <p><b>NOTE:</b> prefixes "xml" and "xmlns" are already bound
*/
public void setPrefix (String prefix, String namespace) throws IOException;
/**
* Return namespace that corresponds to given prefix
* If there is no prefix bound to this namespace return null if
* generatePrefix is false otherwise return null.
*/
public String getPrefix (String namespace, boolean generatePrefix);
/**
* Writes a start tag with the given namespace and name.
* If there is no prefix defined for the given namespace,
* a prefix will be defined automatically.
* The explicit prefixes for namespaces can be established by calling setPrefix()
* immediately before this method.
* If namespace is empty string no namespace prefix is printed but just name.
*/
public void startTag (String namespace, String name) throws IOException;
/**
* Writes an attribute. calls to attribute must follow a call to
* startTag() immediately. if there is no prefix defined for the
* given namespace, a prefix will be defined automatically.
* If namespace is nul no namespace prefix is printed but just name.
*/
public void attribute (String namespace, String name,
String value) throws IOException;
/**
* This method is called explicitly after startTag() and attribute()
* to close XML start tag. Can be called directly to enforce
* serializer to write completely start tag. No more attributes
* is allowed to be added after this call.
*/
//public void closeStartTag () throws IOException;
// use text("") instead
/**
* Write end tag. Repetition of namespace and name is just for avoiding errors
* background: in kXML I just had endTag, and non matching tags were
* very difficult to find...
* If namespace is nul no namespace prefix is printed but just name.
*/
public void endTag (String namespace, String name) throws IOException;
/** Writes text, where special XML chars are escaped automatically */
public void text (String text) throws IOException;
public void text (char [] buf, int start, int len) throws IOException;
/**
* write CDSECT, ENTITY_REF, IGNORABLE_WHITESPACE,
* PROCESSING_INSTRUCTION, COMMENT, and DOCDECL Some types may be
* silently ignored in WBXML (XXX should we make a distinction
* here, which may be ignored, and which events cause an
* exception???? XXX)
*/
public void cdsect (String text) throws IOException;
public void entityRef (String text) throws IOException;
public void processingInstruction (String text) throws IOException;
public void comment (String text) throws IOException;
public void docdecl (String text) throws IOException;
public void ignorableWhitespace (String text) throws IOException;
/**
* writes all pending output to the stream,
* if startTag() or attribute() was caled then start tag is closed
* and flush() is called on underlying output stream.
*/
public void flush () throws IOException;
}
1.4 +1 -1 xmlpull-api-v1/src/java/samples/Roundtrip.java
Index: Roundtrip.java
===================================================================
RCS file: /l/extreme/cvspub/xmlpull-api-v1/src/java/samples/Roundtrip.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -b -t -w -r1.3 -r1.4
--- Roundtrip.java 2002/06/23 21:12:13 1.3
+++ Roundtrip.java 2002/07/08 17:31:09 1.4
@@ -7,7 +7,7 @@
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
-import org.xmlpull.v1.serializer.XmlSerializer;
+import org.xmlpull.v1.XmlSerializer;
// TODO hide this impl. detail with facotry
import org.xmlpull.mxp1_serializer.MXSerializer;