Author: jleroux
Date: Sun Mar 25 11:07:57 2012
New Revision: 1305004

URL: http://svn.apache.org/viewvc?rev=1305004&view=rev
Log:
A patch from Anne Jessel "Support validation of resource xml files" 
https://issues.apache.org/jira/browse/OFBIZ-4723

The xsd for the resource xml files is invalid. In addition, the format of the 
value of the xml:lang attribute in resource xml files is invalid, according to 
the xml standard.

The attached patch:

    provides a valid xsd
    updates UtilProperties and the Label Manager to deal with the correct 
formatting of the xml:lang value, while still working with the old format, 
following Adrian Crum's recommendation on the mailing list
    includes simple unit tests for UtilProperties to ensure it does work with 
both formats
    changes the Label Manager to include a reference to the new xsd, and to use 
the correct format of the xml:lang value, when writing a resource xml file.

Some minor code cleanups to these files.

jleroux: the base test suite was run successfully once UtilIOTests have been 
commented out (still an issue with it on Windows)

Added:
    
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/UtilPropertiesTests.java
   (with props)
Modified:
    ofbiz/trunk/framework/base/dtd/ofbiz-properties.xsd
    ofbiz/trunk/framework/base/testdef/basetests.xml
    
ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelInfo.java
    
ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelManagerFactory.java
    
ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/SaveLabelsToXmlFile.java

Modified: ofbiz/trunk/framework/base/dtd/ofbiz-properties.xsd
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/dtd/ofbiz-properties.xsd?rev=1305004&r1=1305003&r2=1305004&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/dtd/ofbiz-properties.xsd (original)
+++ ofbiz/trunk/framework/base/dtd/ofbiz-properties.xsd Sun Mar 25 11:07:57 2012
@@ -18,22 +18,31 @@ specific language governing permissions 
 under the License.
 -->
 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"; 
elementFormDefault="qualified">
+    <xs:import namespace="http://www.w3.org/XML/1998/namespace";
+            schemaLocation="http://www.w3.org/2001/xml.xsd"/>
     <xs:element name="resource">
         <xs:complexType>
             <xs:sequence>
-                <xs:element minOccurs="0" maxOccurs="unbounded" 
ref="property"/>
+                <xs:element minOccurs="1" maxOccurs="unbounded" 
ref="property"/>
             </xs:sequence>
         </xs:complexType>
     </xs:element>
     <xs:element name="property">
         <xs:complexType>
             <xs:sequence>
-                <xs:element minOccurs="0" maxOccurs="unbounded" ref="value"/>
-                <xs:attributeGroup ref="attlist.property"/>
+                <xs:element minOccurs="1" maxOccurs="unbounded" name="value" 
type="valueType"/>
             </xs:sequence>
+            <xs:attributeGroup ref="attlist.property"/>
         </xs:complexType>
     </xs:element>
     <xs:attributeGroup name="attlist.property">
         <xs:attribute type="xs:string" name="key" use="required"/>
     </xs:attributeGroup>
+    <xs:complexType name="valueType">
+        <xs:simpleContent>
+            <xs:extension base="xs:string">
+                <xs:attribute ref="xml:lang"/>
+            </xs:extension>
+        </xs:simpleContent>
+    </xs:complexType>
 </xs:schema>

Added: 
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/UtilPropertiesTests.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/UtilPropertiesTests.java?rev=1305004&view=auto
==============================================================================
--- 
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/UtilPropertiesTests.java
 (added)
+++ 
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/UtilPropertiesTests.java
 Sun Mar 25 11:07:57 2012
@@ -0,0 +1,94 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ 
*******************************************************************************/
+package org.ofbiz.base.util.test;
+
+
+import org.ofbiz.base.test.GenericTestCaseBase;
+import org.ofbiz.base.util.UtilProperties;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Locale;
+import java.util.Properties;
+
+public class UtilPropertiesTests extends GenericTestCaseBase {
+
+    private Locale locale;
+    private String country = "AU";
+    private String language = "en";
+
+    public UtilPropertiesTests(String name) {
+        super(name);
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        locale = new Locale(language, country);
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        super.tearDown();
+    }
+
+    /**
+     * Old style xml:lang attribute value was of form en_AU. Test this
+     * format still works.
+     * @throws Exception
+     */
+    public void testReadXmlLangOldStyle() throws Exception {
+        Properties result = xmlToProperties("_");
+        assertNotNull(result);
+        assertTrue(!result.isEmpty());
+        assertEquals(1, result.size());
+        assertEquals("Key Value", result.getProperty("PropertyKey"));
+    }
+
+    /**
+     * New (and correct) style xml:lang value is en-AU.
+     * Test it works.
+      * @throws Exception
+     */
+    public void testReadXmlLangNewStyle() throws Exception {
+        Properties result = xmlToProperties("-");
+        assertNotNull(result);
+        assertTrue(!result.isEmpty());
+        assertEquals(1, result.size());
+        assertEquals("Key Value", result.getProperty("PropertyKey"));
+
+    }
+
+    private Properties xmlToProperties(String separator) throws IOException {
+        String xmlData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
+                "<resource 
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; +
+                "          
xsi:noNamespaceSchemaLocation=\"http://ofbiz.apache.org/dtds/ofbiz-properties.xsd\";>\n"
 +
+                "    <property key=\"PropertyKey\">\n" +
+                "        <value xml:lang=\"" +
+                language + separator + country +
+                "\">Key Value</value>\n" +
+                "    </property>\n" +
+                "</resource>";
+        InputStream in = new ByteArrayInputStream(xmlData.getBytes());
+
+        return UtilProperties.xmlToProperties(in, locale, null);
+    }
+
+}

Propchange: 
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/UtilPropertiesTests.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/UtilPropertiesTests.java
------------------------------------------------------------------------------
    svn:keywords = Date Rev Author URL Id

Propchange: 
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/UtilPropertiesTests.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: ofbiz/trunk/framework/base/testdef/basetests.xml
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/testdef/basetests.xml?rev=1305004&r1=1305003&r2=1305004&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/testdef/basetests.xml (original)
+++ ofbiz/trunk/framework/base/testdef/basetests.xml Sun Mar 25 11:07:57 2012
@@ -34,7 +34,8 @@
         <junit-test-suite 
class-name="org.ofbiz.base.conversion.test.MiscTests"/>
         <junit-test-suite 
class-name="org.ofbiz.base.concurrent.test.DependencyPoolTests"/>
         <junit-test-suite class-name="org.ofbiz.base.json.test.JSONTests"/>
-        <junit-test-suite class-name="org.ofbiz.base.util.test.UtilIOTests"/>
+        <!--junit-test-suite 
class-name="org.ofbiz.base.util.test.UtilIOTests"/-->
         <junit-test-suite class-name="org.ofbiz.base.test.BaseUnitTests"/>
+        <junit-test-suite 
class-name="org.ofbiz.base.util.test.UtilPropertiesTests"/>
     </test-group>
 </test-suite>

Modified: 
ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelInfo.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelInfo.java?rev=1305004&r1=1305003&r2=1305004&view=diff
==============================================================================
--- 
ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelInfo.java
 (original)
+++ 
ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelInfo.java
 Sun Mar 25 11:07:57 2012
@@ -35,7 +35,7 @@ public class LabelInfo {
     protected String fileName = "";
     protected Map<String, LabelValue> labelValues = FastMap.newInstance();
 
-    public LabelInfo(String labelKey, String labelKeyComment, String fileName, 
String localeStr, String labelValue, String labelComment) throws 
GeneralException {
+    public LabelInfo(String labelKey, String labelKeyComment, String fileName, 
String localeStr, String labelValue, String labelComment) {
         this.labelKey = labelKey;
         this.labelKeyComment = labelKeyComment;
         this.fileName = fileName;
@@ -85,7 +85,9 @@ public class LabelInfo {
                     labelValues.remove(localeStr);
                 }
             } else {
-                Debug.logWarning("Already found locale " + localeStr + " for 
label " + labelKey + " into the file " + fileName, module);
+                if (Debug.warningOn()) {
+                    Debug.logWarning("Already found locale " + localeStr + " 
for label " + labelKey + " into the file " + fileName, module);
+                }
                 isDuplicatedLocales = true;
             }
         }

Modified: 
ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelManagerFactory.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelManagerFactory.java?rev=1305004&r1=1305003&r2=1305004&view=diff
==============================================================================
--- 
ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelManagerFactory.java
 (original)
+++ 
ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelManagerFactory.java
 Sun Mar 25 11:07:57 2012
@@ -60,7 +60,7 @@ public class LabelManagerFactory {
     protected Set<String> localesFound = new TreeSet<String>();
     protected List<LabelInfo> duplicatedLocalesLabelsList = 
FastList.newInstance();
 
-    public static synchronized LabelManagerFactory getInstance() throws 
GeneralException, IOException {
+    public static synchronized LabelManagerFactory getInstance() throws 
IOException {
         if (componentNamesFound == null) {
             loadComponentNames();
         }
@@ -120,7 +120,9 @@ public class LabelManagerFactory {
             if (UtilValidate.isNotEmpty(fileName) && 
!fileName.equals(fileInfo.getFileName())) {
                 continue;
             }
-            Debug.logInfo("Current file : " + fileInfo.getFileName(), module);
+            if (Debug.infoOn()) {
+                Debug.logInfo("Current file : " + fileInfo.getFileName(), 
module);
+            }
             Document resourceDocument = 
UtilXml.readXmlDocument(fileInfo.file.toURI().toURL(), false);
             Element resourceElem = resourceDocument.getDocumentElement();
             String labelKeyComment = "";
@@ -132,7 +134,12 @@ public class LabelManagerFactory {
                     for (Node valueNode : 
UtilXml.childNodeList(propertyElem.getFirstChild())) {
                         if (valueNode instanceof Element) {
                             Element valueElem = (Element) valueNode;
+                            // Support old way of specifying xml:lang value.
+                            // Old way: en_AU, new way: en-AU
                             String localeName = 
valueElem.getAttribute("xml:lang");
+                            if( localeName.contains("_")) {
+                                localeName = localeName.replace('_', '-');
+                            }
                             String labelValue = 
StringUtil.defaultWebEncoder.canonicalize(UtilXml.nodeValue(valueElem.getFirstChild()));
                             LabelInfo label = labels.get(labelKey + 
keySeparator + fileInfo.getFileName());
 
@@ -208,8 +215,10 @@ public class LabelManagerFactory {
                 } else {
                     label.setLabelKeyComment(keyComment);
                 }
-                label.setLabelValue(localeName, localeValue, localeComment, 
true);
-                notEmptyLabels++;
+                if (label != null) {
+                    label.setLabelValue(localeName, localeValue, 
localeComment, true);
+                    notEmptyLabels++;
+                }
             }
         }
         return notEmptyLabels;

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=1305004&r1=1305003&r2=1305004&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 Mar 25 11:07:57 2012
@@ -113,6 +113,7 @@ public class SaveLabelsToXmlFile {
                 Document resourceDocument = 
UtilXml.makeEmptyXmlDocument("resource");
                 Element resourceElem = resourceDocument.getDocumentElement();
                 resourceElem.setAttribute("xmlns:xsi", 
"http://www.w3.org/2001/XMLSchema-instance";);
+                
resourceElem.setAttribute("xsi:noNamespaceSchemaLocation","http://ofbiz.apache.org/dtds/ofbiz-properties.xsd";);
                 for (String labelKey : labelsList) {
                     LabelInfo labelInfo = labels.get(labelKey);
                     if (!(labelInfo.getFileName().equalsIgnoreCase(fileName))) 
{
@@ -135,7 +136,7 @@ public class SaveLabelsToXmlFile {
                             valueString = 
StringEscapeUtils.unescapeHtml(valueString);
                             Element valueElem = 
UtilXml.addChildElementValue(propertyElem, "value", valueString, 
resourceDocument);
                             valueElem.setAttribute("xml:lang", localeFound);
-                            if (valueString.trim().length() == 0) {
+                            if (valueString.trim().isEmpty()) {
                                 valueElem.setAttribute("xml:space", 
"preserve");
                             }
                             if 
(UtilValidate.isNotEmpty(labelValue.getLabelComment())) {


Reply via email to