You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by ad...@apache.org on 2009/04/26 05:21:41 UTC

svn commit: r768634 - /ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/SaveLabelsToXmlFile.java

Author: adrianc
Date: Sun Apr 26 03:21:40 2009
New Revision: 768634

URL: http://svn.apache.org/viewvc?rev=768634&view=rev
Log:
Cleaned up SaveLabelsToXmlFile.java code, converted to use new UtilXml methods.

This is an example of how to eliminate the use of the deprecated OutputFormat class.

Modified:
    ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/SaveLabelsToXmlFile.java

Modified: ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/SaveLabelsToXmlFile.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/SaveLabelsToXmlFile.java?rev=768634&r1=768633&r2=768634&view=diff
==============================================================================
--- ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/SaveLabelsToXmlFile.java (original)
+++ ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/SaveLabelsToXmlFile.java Sun Apr 26 03:21:40 2009
@@ -20,13 +20,13 @@
 
 import java.io.File;
 import java.io.FileOutputStream;
+import java.io.IOException;
 import java.net.URI;
 import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
 
 import org.apache.commons.lang.StringEscapeUtils;
-import org.apache.xml.serialize.OutputFormat;
 import org.ofbiz.base.util.Debug;
 import org.ofbiz.base.util.FileUtil;
 import org.ofbiz.base.util.UtilProperties;
@@ -50,40 +50,38 @@
     public static Map<String, Object> saveLabelsToXmlFile(DispatchContext dctx, Map<String, ? extends Object> context) {
         Locale locale = (Locale)context.get("locale");
         String labelFileName = (String)context.get("labelFileName");
-
+        String apacheLicenseText = null;
+        try {
+            apacheLicenseText = getApacheLicenseText();
+        } catch (IOException e) {
+            Debug.logWarning(e, "Unable to read Apache License text file", module);
+        }
         try {
             LabelManagerFactory.getLabelManagerFactory(dctx.getDelegator().getDelegatorName());
             Map<String, LabelInfo> labels = LabelManagerFactory.getLabels();
             Map<String, String> fileNamesFound = LabelManagerFactory.getFileNamesFound();
             Set<String> labelsList = LabelManagerFactory.getLabelsList();
             Set<String> localesFound = LabelManagerFactory.getLocalesFound();
-
             for (String fileName : fileNamesFound.keySet()) {
                 if (UtilValidate.isNotEmpty(labelFileName) && !(labelFileName.equalsIgnoreCase(fileName))) {
                     continue;
                 }
-
-                String uri = (String)fileNamesFound.get(fileName);
+                String uri = fileNamesFound.get(fileName);
                 Document resourceDocument = UtilXml.makeEmptyXmlDocument("resource");
                 Element resourceElem = resourceDocument.getDocumentElement();
                 resourceElem.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
-
                 for (String labelKey : labelsList) {
-                    LabelInfo labelInfo = (LabelInfo)labels.get(labelKey);
-
+                    LabelInfo labelInfo = labels.get(labelKey);
                     if (!(labelInfo.getFileName().equalsIgnoreCase(fileName))) {
                         continue;
                     }
-
                     Element propertyElem = UtilXml.addChildElement(resourceElem, "property", resourceDocument);
                     propertyElem.setAttribute("key", StringEscapeUtils.unescapeHtml(labelInfo.getLabelKey()));
-
                     if (UtilValidate.isNotEmpty(labelInfo.getLabelKeyComment())) {
                         Comment labelKeyComment = resourceDocument.createComment(StringEscapeUtils.unescapeHtml(labelInfo.getLabelKeyComment()));
                         Node parent = propertyElem.getParentNode();
                         parent.insertBefore(labelKeyComment, propertyElem);
                     }
-
                     for (String localeFound : localesFound) {
                         LabelValue labelValue = labelInfo.getLabelValue(localeFound);
                         String valueString = null;
@@ -105,40 +103,18 @@
                         }
                     }
                 }
-
                 if (UtilValidate.isNotEmpty(uri)) {
                     File outFile = new File(new URI(uri));
                     FileOutputStream fos = new FileOutputStream(outFile);
-                    OutputFormat format = new OutputFormat(resourceDocument.getDocumentElement().getOwnerDocument(), "UTF-8", true);
-
                     try {
-                        format.setIndent(4);
-                        format.setOmitXMLDeclaration(true);
-                        UtilXml.writeXmlDocument(fos, resourceElem, format);
-                    } finally {
-                        if (UtilValidate.isNotEmpty(fos)) {
-                               fos.close();
-
-                            // workaround to insert the Apache License Header at top of the file
-                            // because the comment on top the xml file has been not written
-                            String outBuffer = FileUtil.readString("UTF-8", outFile);
-                            String basePath = System.getProperty("ofbiz.home");
-
-                            if (UtilValidate.isNotEmpty(basePath)) {
-                                String apacheHeaderFileName = basePath + "/framework/webtools/config/APACHE2_HEADER_FOR_XML";
-                                String apacheHeaderBuffer = "";
-                                File apacheHeaderFile = new File(apacheHeaderFileName);
-
-                                if (UtilValidate.isNotEmpty(apacheHeaderFile)) {
-                                    apacheHeaderBuffer = FileUtil.readString("UTF-8", apacheHeaderFile);
-                                }
-
-                                FileUtil.writeString("UTF-8", apacheHeaderBuffer + outBuffer, outFile);
-
-                                // clear cache to see immediately the new labels and translations in OFBiz
-                                UtilCache.clearCache("properties.UtilPropertiesBundleCache");
-                            }
+                        if (apacheLicenseText != null) {
+                            fos.write(apacheLicenseText.getBytes());
                         }
+                        UtilXml.writeXmlDocument(resourceElem, fos, "UTF-8", true, true, 4);
+                    } finally {
+                        fos.close();
+                        // clear cache to see immediately the new labels and translations in OFBiz
+                        UtilCache.clearCache("properties.UtilPropertiesBundleCache");
                     }
                 }
             }
@@ -148,4 +124,15 @@
         }
         return ServiceUtil.returnSuccess();
     }
+    
+    public static String getApacheLicenseText() throws IOException {
+        String apacheLicenseText = null;
+        String basePath = System.getProperty("ofbiz.home");
+        if (UtilValidate.isNotEmpty(basePath)) {
+            String apacheLicenseFileName = basePath + "/framework/webtools/config/APACHE2_HEADER_FOR_XML";
+            File apacheLicenseFile = new File(apacheLicenseFileName);
+            apacheLicenseText = FileUtil.readString("UTF-8", apacheLicenseFile);
+        }
+        return apacheLicenseText;
+    }
 }



Re: svn commit: r768634 - /ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/SaveLabelsToXmlFile.java

Posted by Adrian Crum <ad...@yahoo.com>.
Good points all. There are other problems with that code too. I was just trying to eliminate the deprecated Xerces class and clean it up a little.

Maybe someone would be interested in working on cleaning this up more and implementing your suggestions.

-Adrian


--- On Sat, 4/25/09, Adam Heath <do...@brainfood.com> wrote:

> From: Adam Heath <do...@brainfood.com>
> Subject: Re: svn commit: r768634 - /ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/SaveLabelsToXmlFile.java
> To: dev@ofbiz.apache.org
> Cc: commits@ofbiz.apache.org
> Date: Saturday, April 25, 2009, 10:00 PM
> adrianc@apache.org wrote:
> > Author: adrianc
> > Date: Sun Apr 26 03:21:40 2009
> > New Revision: 768634
> > 
> > Modified:
> ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/SaveLabelsToXmlFile.java
> > URL:
> http://svn.apache.org/viewvc/ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/SaveLabelsToXmlFile.java?rev=768634&r1=768633&r2=768634&view=diff
> >
> ==============================================================================
> > ---
> ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/SaveLabelsToXmlFile.java
> (original)
> > +++
> ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/SaveLabelsToXmlFile.java
> Sun Apr 26 03:21:40 2009
> > @@ -148,4 +124,15 @@
> >          }
> >          return ServiceUtil.returnSuccess();
> >      }
> > +    
> > +    public static String getApacheLicenseText()
> throws IOException {
> > +        String apacheLicenseText = null;
> > +        String basePath =
> System.getProperty("ofbiz.home");
> > +        if (UtilValidate.isNotEmpty(basePath)) {
> > +            String apacheLicenseFileName = basePath +
> "/framework/webtools/config/APACHE2_HEADER_FOR_XML";
> > +            File apacheLicenseFile = new
> File(apacheLicenseFileName);
> > +            apacheLicenseText =
> FileUtil.readString("UTF-8", apacheLicenseFile);
> > +        }
> > +        return apacheLicenseText;
> > +    }
> >  }
> 
> 
> Would rather see this license header inside framework/base.
>  Then, a
> global function to read it in a plain string of text bytes.
>  Then,
> methods to embed it into xml, properties, etc.  For
> example, maybe
> alter UtilXml writeDocument methods to take a boolean
> method, which
> would signify that the license header should be added.
> 
> You've also broke windows machines, by hard-coding / as
> a file name
> separator.  Please use FlexibleLocation instead.  Doing so
> could also
> get rid if the ofbiz.home system property.
> 
> If you're worried about re-reading the file from disk
> all the time,
> then a soft-ref could be used; personally, I wouldn't
> bother with that.


      

Re: svn commit: r768634 - /ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/SaveLabelsToXmlFile.java

Posted by Adam Heath <do...@brainfood.com>.
adrianc@apache.org wrote:
> Author: adrianc
> Date: Sun Apr 26 03:21:40 2009
> New Revision: 768634
> 
> Modified: ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/SaveLabelsToXmlFile.java
> URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/SaveLabelsToXmlFile.java?rev=768634&r1=768633&r2=768634&view=diff
> ==============================================================================
> --- ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/SaveLabelsToXmlFile.java (original)
> +++ ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/SaveLabelsToXmlFile.java Sun Apr 26 03:21:40 2009
> @@ -148,4 +124,15 @@
>          }
>          return ServiceUtil.returnSuccess();
>      }
> +    
> +    public static String getApacheLicenseText() throws IOException {
> +        String apacheLicenseText = null;
> +        String basePath = System.getProperty("ofbiz.home");
> +        if (UtilValidate.isNotEmpty(basePath)) {
> +            String apacheLicenseFileName = basePath + "/framework/webtools/config/APACHE2_HEADER_FOR_XML";
> +            File apacheLicenseFile = new File(apacheLicenseFileName);
> +            apacheLicenseText = FileUtil.readString("UTF-8", apacheLicenseFile);
> +        }
> +        return apacheLicenseText;
> +    }
>  }


Would rather see this license header inside framework/base.  Then, a
global function to read it in a plain string of text bytes.  Then,
methods to embed it into xml, properties, etc.  For example, maybe
alter UtilXml writeDocument methods to take a boolean method, which
would signify that the license header should be added.

You've also broke windows machines, by hard-coding / as a file name
separator.  Please use FlexibleLocation instead.  Doing so could also
get rid if the ofbiz.home system property.

If you're worried about re-reading the file from disk all the time,
then a soft-ref could be used; personally, I wouldn't bother with that.