[Yanel-dev] DOM Helper classes.

Guillaume Déflache guillaume.deflache at wyona.com
Mon Sep 29 15:31:17 CEST 2008


Alec Bickerton schrieb:
> Are there any similar helper classes in the yanel core that include the
> functionality shown below.
>
> In short :
> - Create a new DOM object
> - Create a text element
> - Convert a DOM into a String.
>
> Not xml related but useful.
> - Stream data
>
> If not, then I would like to include these in a new package under
> org.wyona.util...
>
> WDYT?
>
>
> Code below..
>
>     private void streamData( InputStream is, OutputStream os, int
> packetSize  ) throws IOException {
>         final byte[ ] packet =3D new byte[ packetSize ];
>         int c =3D 0;
>         while( ( c =3D is.read( packet )) !=3D -1  ){
>             os.write( packet );
>         }
>     }
>   =


Looks like =

http://commons.apache.org/io/api-1.4/org/apache/commons/io/IOUtils.html#cop=
y(java.io.InputStream, =

java.io.OutputStream) to me, with the burden of thinking of a buffer =

size, but I wonder: do we already (or want to) use Jakarta Commons IO?


>     public static final String toString( Document document, boolean
> isFragment, boolean indent, String charset ) {
>   =


According to the name of this function, this should be for debugging =

purposes only, right? Then IMHO we do not need the isFragment =

parameter/feature, as a document cannot be a fragment anyway.


>         if( document =3D=3D null )
>             return null;
>         StringWriter strWtr =3D new StringWriter();
>         StreamResult strResult =3D new StreamResult(strWtr);
>         TransformerFactory tfac =3D TransformerFactory.newInstance();
>         try {
>             Transformer t =3D tfac.newTransformer();
>             if( isFragment )
>                 t.setOutputProperty( OutputKeys.OMIT_XML_DECLARATION, =

> YES );
>             if( charset =3D=3D null)
>                 t.setOutputProperty(OutputKeys.ENCODING, =

> DEFAULT_ENCODING);
>             else
>                 t.setOutputProperty(OutputKeys.ENCODING, charset );
>             t.setOutputProperty(OutputKeys.INDENT, (indent) ? YES : NO);
>             t.setOutputProperty(OutputKeys.METHOD, "xml"); //xml, =

> html, text
>
> t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
>             t.transform( new DOMSource(document.getDocumentElement()),
> strResult);
>         } catch (Exception e) {
>           // todo handle this more specifically
>         }
>   =


I beg you to never do that!!! How would we notice the exception happened?


>         return strResult.getWriter().toString();
>     }
>
>     public static final Document  createDOM( String rootName ) throws
> ParserConfigurationException {
>         DocumentBuilderFactory dbfac =3D =

> DocumentBuilderFactory.newInstance();
>         DocumentBuilder docBuilder =3D dbfac.newDocumentBuilder();
>         Document doc =3D docBuilder.newDocument();
>         Element root =3D doc.createElement( rootName );
>         doc.appendChild(root);
>         return doc;
>     }
>
>     public static final Element createTextElement( Document doc, String
> name, String value, HashMap<String, String> attribs ){
>         Element child =3D doc.createElement( name );
>         if( attribs !=3Dnull ){
>             Iterator<Entry<String,String>> iter =3D
> attribs.entrySet().iterator();
>             while( iter.hasNext() ){
>                 Entry<String, String> attr =3D iter.next();
>                 child.setAttribute( attr.getKey(), attr.getValue() );
>             }
>         }
>         Text text =3D doc.createTextNode( value );
>         child.appendChild(text);
>         return child;
>     }
>   =


Looks fine to me, we might need *NS variants of these functions too.
Perhaps createDOM could be renamed to createDocument for consistency =

with the DOM API.
Also I suggest wrapping ParserConfigurationException into a =

RuntimeException else using the function will be tedious.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://wyona.com/pipermail/yanel-development/attachments/20080929/fa7c=
97a5/attachment.htm


More information about the Yanel-development mailing list