Update of /cvsroot/xdoclet/xdoclet/core/src/xdoclet
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv21644/src/xdoclet

Modified Files:
        XmlSubTask.java 
Log Message:
Optionally reformat generated XML

Index: XmlSubTask.java
===================================================================
RCS file: /cvsroot/xdoclet/xdoclet/core/src/xdoclet/XmlSubTask.java,v
retrieving revision 1.25
retrieving revision 1.26
diff -C2 -r1.25 -r1.26
*** XmlSubTask.java     30 Mar 2005 21:07:44 -0000      1.25
--- XmlSubTask.java     13 Jul 2005 14:59:29 -0000      1.26
***************
*** 5,14 ****
  package xdoclet;
  
  import java.net.URL;
! import java.util.ArrayList;
! import java.util.Collection;
! import java.util.Iterator;
  
  import org.apache.commons.logging.Log;
  
  import xdoclet.tagshandler.IdTagsHandler;
--- 5,24 ----
  package xdoclet;
  
+ import java.io.File;
+ import java.io.FileWriter;
+ import java.io.IOException;
+ import java.io.Writer;
  import java.net.URL;
! 
! import javax.xml.parsers.DocumentBuilder;
! import javax.xml.parsers.DocumentBuilderFactory;
! import javax.xml.parsers.ParserConfigurationException;
  
  import org.apache.commons.logging.Log;
+ import org.apache.xml.serialize.DOMSerializer;
+ import org.apache.xml.serialize.OutputFormat;
+ import org.apache.xml.serialize.XMLSerializer;
+ import org.w3c.dom.Document;
+ import org.xml.sax.SAXException;
  
  import xdoclet.tagshandler.IdTagsHandler;
***************
*** 45,48 ****
--- 55,60 ----
      private URL     schemaURL = null;
  
+     private boolean xmlPrettified = false;
+ 
      /**
       * Gets the UseIds attribute of the XmlSubTask object.
***************
*** 126,129 ****
--- 138,149 ----
  
      /**
+      * @return   true if XML output is to be reformatted
+      */
+     public boolean isXmlPrettified()
+     {
+         return xmlPrettified;
+     }
+ 
+     /**
       * If this attribute is set to true, XDoclet will generate id attributes 
in the XML document. Note that this is only
       * available in some subtasks.
***************
*** 171,174 ****
--- 191,202 ----
  
      /**
+      * @param xmlPrettified  Set to true to reformat XML output
+      */
+     public void setXmlPrettified(boolean xmlPrettified)
+     {
+         this.xmlPrettified = xmlPrettified;
+     }
+ 
+     /**
       * Describe what the method does
       *
***************
*** 277,283 ****
          Log log = LogUtil.getLog(XmlSubTask.class, "engineFinished");
  
!         log.debug("isValidateXML()=" + isValidateXML());
          if (shouldValidate()) {
!             XmlValidator.getInstance().validate(getEngine().getOutput());
          }
      }
--- 305,322 ----
          Log log = LogUtil.getLog(XmlSubTask.class, "engineFinished");
  
!         File output = getEngine().getOutput();
! 
!         if (log.isDebugEnabled()) {
!             log.debug("xml generated output file = " + output);
!             log.debug("isXmlPrettified()=" + isXmlPrettified());
!             log.debug("isValidateXML()=" + isValidateXML());
!         }
! 
          if (shouldValidate()) {
!             XmlValidator.getInstance().validate(output);
!         }
! 
!         if (isXmlPrettified()) {
!             prettifyXml(output);
          }
      }
***************
*** 292,294 ****
--- 331,380 ----
          return isValidateXML() && ((getPublicId() != null && getDtdURL() != 
null) || getSchemaURL() != null);
      }
+ 
+     /**
+      * Reformat the specified XML file.
+      *
+      * @param file               The file to reformat
+      * @throws XDocletException
+      */
+     private void prettifyXml(File file) throws XDocletException
+     {
+         Log log = LogUtil.getLog(XmlSubTask.class, "prettifyXml");
+ 
+         Document document = null;
+ 
+         if (log.isDebugEnabled()) {
+             log.debug("reformatting xml file: " + file);
+         }
+ 
+         try {
+             DocumentBuilderFactory factory = 
DocumentBuilderFactory.newInstance();
+ 
+             DocumentBuilder builder = factory.newDocumentBuilder();
+ 
+             document = builder.parse(file);
+         }
+         catch (IOException e) {
+             throw new XDocletException(e, "unable to parse generated xml file 
" + file);
+         }
+         catch (ParserConfigurationException e) {
+             throw new XDocletException(e, "unable to get XML DocumentBuilder 
class");
+         }
+         catch (SAXException e) {
+             throw new XDocletException(e, "XML processing error during 
reformatting");
+         }
+ 
+         try {
+             Writer writer = new FileWriter(file);
+             OutputFormat format = new OutputFormat(document, "UTF-8", true);
+             DOMSerializer serializer = new XMLSerializer(writer, format);
+ 
+             serializer.serialize(document);
+         }
+         catch (IOException e) {
+             throw new XDocletException(e, "error while writing reformatted 
XML");
+         }
+ 
+     }
+ 
  }



-------------------------------------------------------
This SF.Net email is sponsored by the 'Do More With Dual!' webinar happening
July 14 at 8am PDT/11am EDT. We invite you to explore the latest in dual
core and dual graphics technology at this free one hour event hosted by HP, 
AMD, and NVIDIA.  To register visit http://www.hp.com/go/dualwebinar
_______________________________________________
xdoclet-devel mailing list
xdoclet-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/xdoclet-devel

Reply via email to