Re: svn commit: r776988 [1/3] - in /ofbiz/trunk/framework/webtools: config/ servicedef/ src/org/ofbiz/webtools/labelmanager/ webapp/webtools/WEB-INF/ webapp/webtools/WEB-INF/actions/labelmanager/ webapp/webtools/labelmanager/ widget/

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: svn commit: r776988 [1/3] - in /ofbiz/trunk/framework/webtools: config/ servicedef/ src/org/ofbiz/webtools/labelmanager/ webapp/webtools/WEB-INF/ webapp/webtools/WEB-INF/actions/labelmanager/ webapp/webtools/labelmanager/ widget/

Jacques Le Roux
Administrator
Hi Adrian,

Marco wrote this code and I commited it. Actually I must say I did no review all details as you did. It was working, I needed it, so
I though it could be useful for other persons. Actually it did not even occur to me that it could be used by several persons
concurrently, which is of course a big miss :/

Actually as I said it was working but I'm sorry to say that, apart if I miss something, it does not work any longer...
For instance, if you search with all default options, nothing happens. And you can't anymore select a language.

Thanks

Jacques

From: <[hidden email]>

> Author: adrianc
> Date: Thu May 21 07:26:22 2009
> New Revision: 776988
>
> URL: http://svn.apache.org/viewvc?rev=776988&view=rev
> Log:
> Refactored the Webtools Label Manager - due to a multitude of problems. Most notably:
>
> 1. Not thread safe.
> 2. Server files were being read/written without multi-user synchronization
> 3. Reads all UI labels into memory, then stores them in static memory, PLUS
> 4. Finds all references of those labels in project files and store those references in static memory (!!!)
> 5. Doesn't follow best practices for UI layout
>
> It's amazing the original code worked. Even more amazing is the fact that this code made it into the project.
>
> Added:
>    ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelFile.java   (with props)
>    ofbiz/trunk/framework/webtools/webapp/webtools/WEB-INF/actions/labelmanager/ViewReferences.groovy   (with props)
> Modified:
>    ofbiz/trunk/framework/webtools/config/WebtoolsUiLabels.xml
>    ofbiz/trunk/framework/webtools/servicedef/services.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/LabelReferences.java
>    ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/SaveLabelsToXmlFile.java
>    ofbiz/trunk/framework/webtools/webapp/webtools/WEB-INF/actions/labelmanager/LabelManager.groovy
>    ofbiz/trunk/framework/webtools/webapp/webtools/WEB-INF/actions/labelmanager/UpdateManager.groovy
>    ofbiz/trunk/framework/webtools/webapp/webtools/WEB-INF/actions/labelmanager/ViewFile.groovy
>    ofbiz/trunk/framework/webtools/webapp/webtools/WEB-INF/controller.xml
>    ofbiz/trunk/framework/webtools/webapp/webtools/labelmanager/SearchLabels.ftl
>    ofbiz/trunk/framework/webtools/webapp/webtools/labelmanager/UpdateLabel.ftl
>    ofbiz/trunk/framework/webtools/webapp/webtools/labelmanager/ViewFile.ftl
>    ofbiz/trunk/framework/webtools/webapp/webtools/labelmanager/ViewHardcodedLabels.ftl
>    ofbiz/trunk/framework/webtools/webapp/webtools/labelmanager/ViewLabels.ftl
>    ofbiz/trunk/framework/webtools/webapp/webtools/labelmanager/ViewReferences.ftl
>    ofbiz/trunk/framework/webtools/widget/LabelManagerScreens.xml
>
> Modified: ofbiz/trunk/framework/webtools/config/WebtoolsUiLabels.xml
> URL:
> http://svn.apache.org/viewvc/ofbiz/trunk/framework/webtools/config/WebtoolsUiLabels.xml?rev=776988&r1=776987&r2=776988&view=diff
> ==============================================================================
> --- ofbiz/trunk/framework/webtools/config/WebtoolsUiLabels.xml (original)
> +++ ofbiz/trunk/framework/webtools/config/WebtoolsUiLabels.xml Thu May 21 07:26:22 2009
> @@ -1512,6 +1512,11 @@
>         <value xml:lang="fr">Nom de fichier</value>
>         <value xml:lang="it">Nome File</value>
>     </property>
> +    <property key="WebtoolsLabelManagerFindLabels">
> +        <value xml:lang="en">Find Labels</value>
> +        <value xml:lang="fr">Gestionnaire de traduction</value>
> +        <value xml:lang="it">Gestione delle label</value>
> +    </property>
>     <property key="WebtoolsLabelManagerHardcoded">
>         <value xml:lang="en">Hardcoded labels</value>
>         <value xml:lang="it">Label non traducibili</value>
>
> Modified: ofbiz/trunk/framework/webtools/servicedef/services.xml
> URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webtools/servicedef/services.xml?rev=776988&r1=776987&r2=776988&view=diff
> ==============================================================================
> --- ofbiz/trunk/framework/webtools/servicedef/services.xml (original)
> +++ ofbiz/trunk/framework/webtools/servicedef/services.xml Thu May 21 07:26:22 2009
> @@ -123,27 +123,10 @@
>         <attribute name="eomodeldFullPath" type="java.lang.String" mode="IN" optional="false"/>
>         <attribute name="serviceName" type="java.lang.String" mode="IN" optional="false"/>
>     </service>
> +
>     <service name="saveLabelsToXmlFile" engine="java" location="org.ofbiz.webtools.labelmanager.SaveLabelsToXmlFile"
>              invoke="saveLabelsToXmlFile" auth="true" use-transaction="false">
>         <description>Save labels to xml file</description>
> -        <attribute name="labelFileName" type="String" mode="IN" optional="true"/>
> -    </service>
> -    <service name="updateLabelKey" engine="java" location="org.ofbiz.webtools.labelmanager.LabelManagerFactory"
> -             invoke="updateLabelKey" auth="true" use-transaction="false">
> -        <description>Update Label Key</description>
> -        <attribute name="key" type="String" mode="IN" optional="true"/>
> -        <attribute name="keyComment" type="String" mode="IN" optional="true"/>
> -        <attribute name="update_label" type="String" mode="IN" optional="false"/>
> -        <attribute name="fileName" type="String" mode="IN" optional="false"/>
> -        <attribute name="confirm" type="String" mode="IN" optional="true"/>
> -        <attribute name="removeLabel" type="String" mode="IN" optional="true"/>
> -        <attribute name="localeNames" type="List" mode="IN" optional="true"/>
> -        <attribute name="localeValues" type="List" mode="IN" optional="true"/>
> -        <attribute name="localeComments" type="List" mode="IN" optional="true"/>
> -    </service>
> -    <service name="updateAndSaveLabelKey" engine="java" location="org.ofbiz.webtools.labelmanager.LabelManagerFactory"
> -             invoke="updateAndSaveLabelKey" auth="true" use-transaction="false">
> -        <description>Update Label Key</description>
>         <attribute name="key" type="String" mode="IN" optional="true"/>
>         <attribute name="keyComment" type="String" mode="IN" optional="true"/>
>         <attribute name="update_label" type="String" mode="IN" optional="false"/>
>
> Added: ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelFile.java
> URL:
> http://svn.apache.org/viewvc/ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelFile.java?rev=776988&view=auto
> ==============================================================================
> --- ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelFile.java (added)
> +++ ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelFile.java Thu May 21 07:26:22 2009
> @@ -0,0 +1,46 @@
> +/*
> + * 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.webtools.labelmanager;
> +
> +import java.io.File;
> +
> +public class LabelFile {
> +    public static final String module = LabelFile.class.getName();
> +
> +    protected final boolean fileLoaded = false;;
> +    protected final File file;
> +    protected final String componentName;
> +
> +    protected LabelFile(File file, String componentName) {
> +        this.file = file;
> +        this.componentName = componentName;
> +    }
> +
> +    public File getFile() {
> +        return this.file;
> +    }
> +
> +    public String getFileName() {
> +        return this.file.getName();
> +    }
> +
> +    public String getFilePath() {
> +        return this.file.getPath();
> +    }
> +}
>
> Propchange: ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelFile.java
> ------------------------------------------------------------------------------
>    svn:eol-style = native
>
> Propchange: ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelFile.java
> ------------------------------------------------------------------------------
>    svn:keywords = "Date Rev Author URL Id"
>
> Propchange: ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelFile.java
> ------------------------------------------------------------------------------
>    svn:mime-type = text/plain
>
> 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=776988&r1=776987&r2=776988&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 Thu May 21 07:26:22 2009
> @@ -33,14 +33,12 @@
>     protected String labelKey = "";
>     protected String labelKeyComment = "";
>     protected String fileName = "";
> -    protected String componentName = "";
>     protected Map<String, LabelValue> labelValues = FastMap.newInstance();
>
> -    public LabelInfo(String labelKey, String labelKeyComment, String fileName, String componentName, String localeStr, String
> labelValue, String labelComment) throws GeneralException {
> +    public LabelInfo(String labelKey, String labelKeyComment, String fileName, String localeStr, String labelValue, String
> labelComment) throws GeneralException {
>         this.labelKey = labelKey;
>         this.labelKeyComment = labelKeyComment;
>         this.fileName = fileName;
> -        this.componentName = componentName;
>         setLabelValue(localeStr, labelValue, labelComment, false);
>     }
>
> @@ -60,10 +58,6 @@
>         return fileName;
>     }
>
> -    public String getComponentName() {
> -        return componentName;
> -    }
> -
>     public LabelValue getLabelValue(String localeStr) {
>        return (LabelValue)labelValues.get(localeStr);
>     }
>
> 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=776988&r1=776987&r2=776988&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 Thu May 21 07:26:22 2009
> @@ -20,331 +20,185 @@
>
> import java.io.File;
> import java.io.IOException;
> +import java.net.MalformedURLException;
> import java.util.Collection;
> import java.util.List;
> -import java.util.Locale;
> import java.util.Map;
> import java.util.Set;
> import java.util.TreeMap;
> import java.util.TreeSet;
>
> +import javax.xml.parsers.ParserConfigurationException;
> +
> import javolution.util.FastList;
>
> import org.ofbiz.base.component.ComponentConfig;
> +import org.ofbiz.base.component.ComponentConfig.ClasspathInfo;
> +import org.ofbiz.base.util.Debug;
> import org.ofbiz.base.util.FileUtil;
> import org.ofbiz.base.util.GeneralException;
> import org.ofbiz.base.util.StringUtil;
> -import org.ofbiz.base.util.UtilGenerics;
> -import org.ofbiz.base.util.UtilMisc;
> -import org.ofbiz.base.util.UtilProperties;
> import org.ofbiz.base.util.UtilValidate;
> import org.ofbiz.base.util.UtilXml;
> -import org.ofbiz.base.util.cache.UtilCache;
> -import org.ofbiz.entity.GenericDelegator;
> -import org.ofbiz.entity.GenericValue;
> -import org.ofbiz.entity.model.ModelReader;
> -import org.ofbiz.service.DispatchContext;
> -import org.ofbiz.service.GenericServiceException;
> -import org.ofbiz.service.LocalDispatcher;
> -import org.ofbiz.service.ServiceUtil;
> +import org.owasp.esapi.errors.EncodingException;
> import org.w3c.dom.Comment;
> import org.w3c.dom.Document;
> import org.w3c.dom.Element;
> import org.w3c.dom.Node;
> +import org.xml.sax.SAXException;
>
> public class LabelManagerFactory {
>
>     public static final String module = LabelManagerFactory.class.getName();
>     public static final String resource = "WebtoolsUiLabels";
> -
>     public static final String keySeparator = "#";
>
> -    protected static UtilCache<String, LabelManagerFactory> labelManagerFactoryCache = new UtilCache<String,
> LabelManagerFactory>("LabelManagerFactory");
> -
> -    protected static Map<String, LabelInfo> labels = null;
> -    protected static Map<String, String> fileNamesFound = null;
> -    protected static Map<String, String> fileComponent = null;
> -    protected static Set<String> localesFound = null;
>     protected static Set<String> componentNamesFound = null;
> -    protected static Map<String, Map<String, Integer>> references = null;
> -    protected static List<LabelInfo> duplicatedLocalesLabelsList = null;
> -    protected static int duplicatedLocalesLabels = 0;
> -
> -    protected static GenericDelegator delegator;
> -    protected static ModelReader entityModelReader;
> -    protected static DispatchContext dispatchContext;
> -
> -    public static LabelManagerFactory getLabelManagerFactory(String delegatorName) throws GeneralException {
> -        if (UtilValidate.isEmpty(delegatorName)) {
> -            delegatorName = "default";
> -        }
> +    protected static Map<String, LabelFile> filesFound = null;
>
> -        LabelManagerFactory lmf = labelManagerFactoryCache.get(delegatorName);
> +    protected Map<String, LabelInfo> labels = new TreeMap<String, LabelInfo>();
> +    protected Set<String> localesFound = new TreeSet<String>();
> +    protected List<LabelInfo> duplicatedLocalesLabelsList = FastList.newInstance();
>
> -        if (lmf == null) {
> -            lmf = new LabelManagerFactory(delegatorName);
> -            labelManagerFactoryCache.put(delegatorName, lmf);
> +    public static synchronized LabelManagerFactory getInstance() throws GeneralException, IOException {
> +        if (componentNamesFound == null) {
> +            loadComponentNames();
>         }
> -        return lmf;
> +        if (filesFound == null) {
> +            loadLabelFiles();
> +        }
> +        return new LabelManagerFactory();
>     }
>
> -    protected LabelManagerFactory(String delegatorName) throws GeneralException {
> -        delegator = GenericDelegator.getGenericDelegator(delegatorName);
> -        entityModelReader = ModelReader.getModelReader(delegatorName);
> -        dispatchContext = new DispatchContext("LabelManagerFactoryDispCtx", null, this.getClass().getClassLoader(), null);
> -        prepareAll();
> +    protected LabelManagerFactory() {
>     }
>
> -    private static void prepareAll() throws GeneralException {
> -        labels = new TreeMap<String, LabelInfo>();
> -        fileNamesFound = new TreeMap<String, String>();
> -        fileComponent = new TreeMap<String, String>();
> -        localesFound = new TreeSet<String>();
> +    protected static void loadComponentNames() {
>         componentNamesFound = new TreeSet<String>();
> -        duplicatedLocalesLabelsList = FastList.newInstance();
> -        references = null;
> -        int duplicatedLocales = 0;
> -
> -        try {
> -            boolean sharkComponent = false;
> -            Collection<ComponentConfig> componentConfigs = ComponentConfig.getAllComponents();
> -
> -            for (ComponentConfig componentConfig : componentConfigs) {
> -                String componentName = componentConfig.getComponentName();
> -                List<File> resourceFiles = FileUtil.findXmlFiles(componentConfig.getRootLocation(), null, "resource", null);
> -                boolean cycle = true;
> -                while (cycle) {
> -                    for (File resourceFile : resourceFiles) {
> -                        String fileName = resourceFile.getName();
> -                        Document resourceDocument = UtilXml.readXmlDocument(resourceFile.toURI().toURL());
> -                        Element resourceElem = resourceDocument.getDocumentElement();
> -                        String labelKeyComment = "";
> -
> -                        for (Node propertyNode : UtilXml.childNodeList(resourceElem.getFirstChild())) {
> -                            if (propertyNode instanceof Element) {
> -                                Element propertyElem = (Element) propertyNode;
> -                                String labelKey = StringUtil.defaultWebEncoder.canonicalize(propertyElem.getAttribute("key"));
> -                                String labelComment = "";
> -
> -                                for (Node valueNode : UtilXml.childNodeList(propertyElem.getFirstChild())) {
> -                                    if (valueNode instanceof Element) {
> -                                        Element valueElem = (Element) valueNode;
> -                                        String localeName = valueElem.getAttribute("xml:lang");
> -                                        String labelValue =
> StringUtil.defaultWebEncoder.canonicalize(UtilXml.nodeValue(valueElem.getFirstChild()));
> -                                        LabelInfo label = labels.get(labelKey + keySeparator + fileName);
> -
> -                                        if (UtilValidate.isEmpty(label)) {
> -                                            label = new LabelInfo(labelKey, labelKeyComment, fileName, componentName, localeName,
> labelValue, labelComment);
> -                                            labels.put(labelKey + keySeparator + fileName, label);
> -                                        } else {
> -                                            if (label.setLabelValue(localeName, labelValue, labelComment, false)) {
> -                                                duplicatedLocalesLabelsList.add(label);
> -                                                duplicatedLocales++;
> -                                            }
> -                                        }
> -                                        localesFound.add(localeName);
> -                                        componentNamesFound.add(componentName);
> -                                        fileNamesFound.put(fileName, resourceFile.toURI().toString());
> -                                        fileComponent.put(fileName, componentName);
> -                                        labelComment = "";
> -                                    } else if (valueNode instanceof Comment) {
> -                                        labelComment = labelComment +
> StringUtil.defaultWebEncoder.canonicalize(valueNode.getNodeValue());
> -                                    }
> +        Collection<ComponentConfig> componentConfigs = ComponentConfig.getAllComponents();
> +        for (ComponentConfig componentConfig : componentConfigs) {
> +            componentNamesFound.add(componentConfig.getComponentName());
> +        }
> +        componentNamesFound.add("shark");
> +    }
> +
> +    protected static void loadLabelFiles() throws IOException {
> +        filesFound = new TreeMap<String, LabelFile>();
> +        List<ClasspathInfo> cpInfos = ComponentConfig.getAllClasspathInfos();
> +        for (ClasspathInfo cpi : cpInfos) {
> +            if ("dir".equals(cpi.type)) {
> +                String configRoot = cpi.componentConfig.getRootLocation();
> +                configRoot = configRoot.replace('\\', '/');
> +                if (!configRoot.endsWith("/")) {
> +                    configRoot = configRoot + "/";
> +                }
> +                String location = cpi.location.replace('\\', '/');
> +                if (location.startsWith("/")) {
> +                    location = location.substring(1);
> +                }
> +                List<File> resourceFiles = FileUtil.findXmlFiles(configRoot + location, null, "resource", null);
> +                for (File resourceFile : resourceFiles) {
> +                    filesFound.put(resourceFile.getName(), new LabelFile(resourceFile, cpi.componentConfig.getComponentName()));
> +                }
> +            }
> +        }
> +        List<File> resourceFiles = FileUtil.findXmlFiles(System.getProperty("ofbiz.home") + "/specialpurpose/shark/config", null,
> "resource", null);
> +        for (File resourceFile : resourceFiles) {
> +            filesFound.put(resourceFile.getName(), new LabelFile(resourceFile, "shark"));
> +        }
> +    }
> +
> +    public void findMatchingLabels(String component, String fileName, String key, String locale) throws MalformedURLException,
> SAXException, ParserConfigurationException, IOException, EncodingException, GeneralException {
> +        if (UtilValidate.isEmpty(component) && UtilValidate.isEmpty(fileName) && UtilValidate.isEmpty(key) &&
> UtilValidate.isEmpty(locale)) {
> +            // Important! Don't allow unparameterized queries - doing so will result in loading the entire project into memory
> +            return;
> +        }
> +        for (LabelFile fileInfo : filesFound.values()) {
> +            if (UtilValidate.isNotEmpty(component) && !component.equals(fileInfo.componentName)) {
> +                continue;
> +            }
> +            if (UtilValidate.isNotEmpty(fileName) && !fileName.equals(fileInfo.getFileName())) {
> +                continue;
> +            }
> +            Document resourceDocument = UtilXml.readXmlDocument(fileInfo.file.toURL(), false);
> +            Element resourceElem = resourceDocument.getDocumentElement();
> +            String labelKeyComment = "";
> +            for (Node propertyNode : UtilXml.childNodeList(resourceElem.getFirstChild())) {
> +                if (propertyNode instanceof Element) {
> +                    Element propertyElem = (Element) propertyNode;
> +                    String labelKey = StringUtil.defaultWebEncoder.canonicalize(propertyElem.getAttribute("key"));
> +                    String labelComment = "";
> +                    for (Node valueNode : UtilXml.childNodeList(propertyElem.getFirstChild())) {
> +                        if (valueNode instanceof Element) {
> +                            Element valueElem = (Element) valueNode;
> +                            String localeName = valueElem.getAttribute("xml:lang");
> +                            String labelValue =
> StringUtil.defaultWebEncoder.canonicalize(UtilXml.nodeValue(valueElem.getFirstChild()));
> +                            LabelInfo label = labels.get(labelKey + keySeparator + fileInfo.getFileName());
> +
> +                            if (UtilValidate.isEmpty(label)) {
> +                                label = new LabelInfo(labelKey, labelKeyComment, fileInfo.getFileName(), localeName, labelValue,
> labelComment);
> +                                labels.put(labelKey + keySeparator + fileInfo.getFileName(), label);
> +                            } else {
> +                                if (label.setLabelValue(localeName, labelValue, labelComment, false)) {
> +                                    duplicatedLocalesLabelsList.add(label);
>                                 }
> -                                labelKeyComment = "";
> -                            } else if (propertyNode instanceof Comment) {
> -                                labelKeyComment = labelKeyComment +
> StringUtil.defaultWebEncoder.canonicalize(propertyNode.getNodeValue());
>                             }
> +                            localesFound.add(localeName);
> +                            labelComment = "";
> +                        } else if (valueNode instanceof Comment) {
> +                            labelComment = labelComment + StringUtil.defaultWebEncoder.canonicalize(valueNode.getNodeValue());
>                         }
>                     }
> -                    if (!sharkComponent) {
> -                        componentName = "shark";
> -                        resourceFiles = FileUtil.findXmlFiles(System.getProperty("ofbiz.home") + "/specialpurpose/shark", null,
> "resource", null);
> -                        sharkComponent = true;
> -                    } else {
> -                        cycle = false;
> -                    }
> +                    labelKeyComment = "";
> +                } else if (propertyNode instanceof Comment) {
> +                    labelKeyComment = labelKeyComment + StringUtil.defaultWebEncoder.canonicalize(propertyNode.getNodeValue());
>                 }
>             }
> -
> -            // get labels references from sources
> -            references = LabelReferences.getLabelReferences();
> -        } catch (IOException ioe) {
> -            throw new GeneralException(ioe.getMessage());
> -        } catch (Exception e) {
> -            throw new GeneralException(e.getMessage());
>         }
> -        duplicatedLocalesLabels = duplicatedLocales;
> -    }
> -
> -    public static GenericDelegator getDelegator() {
> -        return delegator;
>     }
>
> -    public static ModelReader getModelReader() {
> -        return entityModelReader;
> +    public LabelFile getLabelFile(String fileName) {
> +        return filesFound.get(fileName);
>     }
>
> -    public static DispatchContext getDispatchContext() {
> -        return dispatchContext;
> -    }
> -
> -    public static Map<String, LabelInfo> getLabels() {
> +    public Map<String, LabelInfo> getLabels() {
>         return labels;
>     }
>
> -    public static Set<String> getLocalesFound() {
> +    public Set<String> getLocalesFound() {
>         return localesFound;
>     }
>
> -    public static Map<String, String> getFileNamesFound() {
> -        return fileNamesFound;
> -    }
> -
> -    public static String getFileComponent(String fileName) {
> -        String componentName = null;
> -        if (UtilValidate.isNotEmpty(fileName)) {
> -            componentName = fileComponent.get(fileName);
> -        }
> -        return componentName;
> +    public static Collection<LabelFile> getFilesFound() {
> +        return filesFound.values();
>     }
>
>     public static Set<String> getComponentNamesFound() {
>         return componentNamesFound;
>     }
>
> -    public static Map<String, Map<String, Integer>> getReferences() {
> -        return references;
> -    }
> -
> -    public static Set<String> getLabelsList() {
> +    public Set<String> getLabelsList() {
>         return labels.keySet();
>     }
>
> -    public static Set<String> getReferencesList() {
> -        return references.keySet();
> +    public int getDuplicatedLocalesLabels() {
> +        return duplicatedLocalesLabelsList.size();
>     }
>
> -    public static int getLabelReferenceFile(String key) {
> -        int refFile = 0;
> -        boolean keyFound = false;
> -
> -        if (key == null) {
> -            key = "";
> -        }
> -
> -        for (Map.Entry<String, String> e : fileNamesFound.entrySet()) {
> -            String keyToSearch = key + keySeparator + e.getKey();
> -
> -            if (labels.containsKey(keyToSearch)) {
> -                keyFound = true;
> -                break;
> -            }
> -        }
> -
> -        if (!keyFound) {
> -            Map<String, Integer> reference = references.get(key);
> -
> -            if (UtilValidate.isNotEmpty(reference)) {
> -                refFile = reference.size();
> -            }
> -        }
> -
> -        return refFile;
> -    }
> -
> -    public static int getDuplicatedLocalesLabels() {
> -        return duplicatedLocalesLabels;
> -    }
> -
> -    public static List<LabelInfo> getDuplicatedLocalesLabelsList() {
> +    public List<LabelInfo> getDuplicatedLocalesLabelsList() {
>         return duplicatedLocalesLabelsList;
>     }
>
> -    public static Map<String, Object> updateLabelKey(DispatchContext dctx, Map<String, ? extends Object> context) {
> -        String key = (String) context.get("key");
> -        String keyComment = (String) context.get("keyComment");
> -        String update_label = (String) context.get("update_label");
> -        String fileName = (String) context.get("fileName");
> -        String confirm = (String) context.get("confirm");
> -        String removeLabel = (String) context.get("removeLabel");
> -        List<String> localeNames = UtilGenerics.cast(context.get("localeNames"));
> -        List<String> localeValues = UtilGenerics.cast(context.get("localeValues"));
> -        List<String> localeComments = UtilGenerics.cast(context.get("localeComments"));
> -        Locale locale = (Locale) context.get("locale");
> -
> -        // Remove a Label
> -        if (UtilValidate.isNotEmpty(removeLabel)) {
> -            labels.remove(key + keySeparator + fileName);
> -        } else if (UtilValidate.isNotEmpty(confirm)) {
> -            LabelInfo label = labels.get(key + keySeparator + fileName);
> -
> -            // Update a Label
> -            if (update_label.equalsIgnoreCase("Y")) {
> -                if (UtilValidate.isNotEmpty(label)) {
> -                    updateLabelValue(localeNames, localeValues, localeComments, label, key, keyComment, fileName);
> -                }
> -                // Insert a new Label
> -            } else {
> -                if (UtilValidate.isNotEmpty(label)) {
> -                    return ServiceUtil.returnError(UtilProperties.getMessage(resource, "WebtoolsLabelManagerNewLabelExisting",
> UtilMisc.toMap("key", key, "fileName", fileName), locale));
> -                } else {
> -                    if (UtilValidate.isEmpty(key)) {
> -                        return ServiceUtil.returnError(UtilProperties.getMessage(resource,
> "WebtoolsLabelManagerNewLabelEmptyKey", locale));
> -                    } else {
> -                        int notEmptyLabels = updateLabelValue(localeNames, localeValues, localeComments, null, key, keyComment,
> fileName);
> -                        if (notEmptyLabels == 0) {
> -                            return ServiceUtil.returnError(UtilProperties.getMessage(resource,
> "WebtoolsLabelManagerNewLabelEmpty", locale));
> -                        }
> -                    }
> -                }
> -            }
> -        }
> -
> -        return ServiceUtil.returnSuccess();
> -    }
> -
> -    public static Map<String, Object> updateAndSaveLabelKey(DispatchContext dctx, Map<String, ? extends Object> context) {
> -        String key = (String) context.get("key");
> -        String keyComment = (String) context.get("keyComment");
> -        String update_label = (String) context.get("update_label");
> -        String fileName = (String) context.get("fileName");
> -        String confirm = (String) context.get("confirm");
> -        String removeLabel = (String) context.get("removeLabel");
> -        List<String> localeNames = UtilGenerics.cast(context.get("localeNames"));
> -        List<String> localeValues = UtilGenerics.cast(context.get("localeValues"));
> -        List<String> localeComments = UtilGenerics.cast(context.get("localeComments"));
> -        GenericValue userLogin = (GenericValue) context.get("userLogin");
> -        LocalDispatcher dispatcher = dctx.getDispatcher();
> -
> -        Map<String, Object> contextInput = UtilMisc.toMap("key", key, "keyComment", keyComment, "update_label", update_label,
> "fileName", fileName, "confirm", confirm, "removeLabel", removeLabel,
> -                "localeNames", localeNames, "localeValues", localeValues, "localeComments", localeComments, "userLogin",
> userLogin);
> -        try {
> -            Map<String, Object> updatedKey = dispatcher.runSync("updateLabelKey", contextInput);
> -
> -            if (ServiceUtil.isError(updatedKey)) {
> -                return updatedKey;
> -            } else {
> -                return dispatcher.runSync("saveLabelsToXmlFile", UtilMisc.toMap("labelFileName", fileName, "userLogin",
> userLogin));
> -            }
> -        } catch (GenericServiceException e) {
> -            return ServiceUtil.returnError("error on saving label key :" + key);
> -        }
> -    }
> -
> -    private static int updateLabelValue(List<String> localeNames, List<String> localeValues, List<String> localeComments,
> LabelInfo label, String key, String keyComment, String fileName) {
> +    public int updateLabelValue(List<String> localeNames, List<String> localeValues, List<String> localeComments, LabelInfo
> label, String key, String keyComment, String fileName) {
>         int notEmptyLabels = 0;
> -        int i = 0;
> -        while (i < localeNames.size()) {
> +        for (int i = 0; i < localeNames.size(); i++) {
>             String localeName = localeNames.get(i);
>             String localeValue = localeValues.get(i);
>             String localeComment = localeComments.get(i);
> -
>             if (UtilValidate.isNotEmpty(localeValue) || UtilValidate.isNotEmpty(localeComment)) {
>                 if (label == null) {
>                     try {
> -                        String componentName = getFileComponent(fileName);
> -                        label = new LabelInfo(key, keyComment, fileName, componentName, localeName, localeValue, localeComment);
> +                        label = new LabelInfo(key, keyComment, fileName, localeName, localeValue, localeComment);
>                         labels.put(key + keySeparator + fileName, label);
>                     } catch (Exception e) {
>                         e.printStackTrace();
> @@ -355,9 +209,7 @@
>                 label.setLabelValue(localeName, localeValue, localeComment, true);
>                 notEmptyLabels++;
>             }
> -            i++;
>         }
> -
>         return notEmptyLabels;
>     }
> }
>
>


Reply | Threaded
Open this post in threaded view
|

Re: svn commit: r776988 [1/3] - in /ofbiz/trunk/framework/webtools: config/ servicedef/ src/org/ofbiz/webtools/labelmanager/ webapp/webtools/WEB-INF/ webapp/webtools/WEB-INF/actions/labelmanager/ webapp/webtools/labelmanager/ widget/

Adrian Crum
You have to select search criteria. That was one of the flaws in the
original design - if you just click Find, it goes out and finds EVERY UI
label, and EVERY reference to them in the project. It's a very time and
memory consuming process.

Just select a component or a file to work on, and everything will work
as expected.

I spent a lot of time testing it, and I'm confident it is working properly.

-Adrian

Jacques Le Roux wrote:

> Hi Adrian,
>
> Marco wrote this code and I commited it. Actually I must say I did no
> review all details as you did. It was working, I needed it, so I though
> it could be useful for other persons. Actually it did not even occur to
> me that it could be used by several persons concurrently, which is of
> course a big miss :/
>
> Actually as I said it was working but I'm sorry to say that, apart if I
> miss something, it does not work any longer...
> For instance, if you search with all default options, nothing happens.
> And you can't anymore select a language.
>
> Thanks
>
> Jacques
>
> From: <[hidden email]>
>> Author: adrianc
>> Date: Thu May 21 07:26:22 2009
>> New Revision: 776988
>>
>> URL: http://svn.apache.org/viewvc?rev=776988&view=rev
>> Log:
>> Refactored the Webtools Label Manager - due to a multitude of
>> problems. Most notably:
>>
>> 1. Not thread safe.
>> 2. Server files were being read/written without multi-user
>> synchronization
>> 3. Reads all UI labels into memory, then stores them in static memory,
>> PLUS
>> 4. Finds all references of those labels in project files and store
>> those references in static memory (!!!)
>> 5. Doesn't follow best practices for UI layout
>>
>> It's amazing the original code worked. Even more amazing is the fact
>> that this code made it into the project.
>>
>> Added:
>>    
>> ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelFile.java  
>> (with props)
>>    
>> ofbiz/trunk/framework/webtools/webapp/webtools/WEB-INF/actions/labelmanager/ViewReferences.groovy  
>> (with props)
>> Modified:
>>    ofbiz/trunk/framework/webtools/config/WebtoolsUiLabels.xml
>>    ofbiz/trunk/framework/webtools/servicedef/services.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/LabelReferences.java
>>
>>    
>> ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/SaveLabelsToXmlFile.java
>>
>>    
>> ofbiz/trunk/framework/webtools/webapp/webtools/WEB-INF/actions/labelmanager/LabelManager.groovy
>>
>>    
>> ofbiz/trunk/framework/webtools/webapp/webtools/WEB-INF/actions/labelmanager/UpdateManager.groovy
>>
>>    
>> ofbiz/trunk/framework/webtools/webapp/webtools/WEB-INF/actions/labelmanager/ViewFile.groovy
>>
>>    ofbiz/trunk/framework/webtools/webapp/webtools/WEB-INF/controller.xml
>>    
>> ofbiz/trunk/framework/webtools/webapp/webtools/labelmanager/SearchLabels.ftl
>>
>>    
>> ofbiz/trunk/framework/webtools/webapp/webtools/labelmanager/UpdateLabel.ftl
>>
>>    
>> ofbiz/trunk/framework/webtools/webapp/webtools/labelmanager/ViewFile.ftl
>>    
>> ofbiz/trunk/framework/webtools/webapp/webtools/labelmanager/ViewHardcodedLabels.ftl
>>
>>    
>> ofbiz/trunk/framework/webtools/webapp/webtools/labelmanager/ViewLabels.ftl
>>
>>    
>> ofbiz/trunk/framework/webtools/webapp/webtools/labelmanager/ViewReferences.ftl
>>
>>    ofbiz/trunk/framework/webtools/widget/LabelManagerScreens.xml
>>
>> Modified: ofbiz/trunk/framework/webtools/config/WebtoolsUiLabels.xml
>> URL:
>> http://svn.apache.org/viewvc/ofbiz/trunk/framework/webtools/config/WebtoolsUiLabels.xml?rev=776988&r1=776987&r2=776988&view=diff 
>>
>> ==============================================================================
>>
>> --- ofbiz/trunk/framework/webtools/config/WebtoolsUiLabels.xml (original)
>> +++ ofbiz/trunk/framework/webtools/config/WebtoolsUiLabels.xml Thu May
>> 21 07:26:22 2009
>> @@ -1512,6 +1512,11 @@
>>         <value xml:lang="fr">Nom de fichier</value>
>>         <value xml:lang="it">Nome File</value>
>>     </property>
>> +    <property key="WebtoolsLabelManagerFindLabels">
>> +        <value xml:lang="en">Find Labels</value>
>> +        <value xml:lang="fr">Gestionnaire de traduction</value>
>> +        <value xml:lang="it">Gestione delle label</value>
>> +    </property>
>>     <property key="WebtoolsLabelManagerHardcoded">
>>         <value xml:lang="en">Hardcoded labels</value>
>>         <value xml:lang="it">Label non traducibili</value>
>>
>> Modified: ofbiz/trunk/framework/webtools/servicedef/services.xml
>> URL:
>> http://svn.apache.org/viewvc/ofbiz/trunk/framework/webtools/servicedef/services.xml?rev=776988&r1=776987&r2=776988&view=diff 
>>
>> ==============================================================================
>>
>> --- ofbiz/trunk/framework/webtools/servicedef/services.xml (original)
>> +++ ofbiz/trunk/framework/webtools/servicedef/services.xml Thu May 21
>> 07:26:22 2009
>> @@ -123,27 +123,10 @@
>>         <attribute name="eomodeldFullPath" type="java.lang.String"
>> mode="IN" optional="false"/>
>>         <attribute name="serviceName" type="java.lang.String"
>> mode="IN" optional="false"/>
>>     </service>
>> +
>>     <service name="saveLabelsToXmlFile" engine="java"
>> location="org.ofbiz.webtools.labelmanager.SaveLabelsToXmlFile"
>>              invoke="saveLabelsToXmlFile" auth="true"
>> use-transaction="false">
>>         <description>Save labels to xml file</description>
>> -        <attribute name="labelFileName" type="String" mode="IN"
>> optional="true"/>
>> -    </service>
>> -    <service name="updateLabelKey" engine="java"
>> location="org.ofbiz.webtools.labelmanager.LabelManagerFactory"
>> -             invoke="updateLabelKey" auth="true"
>> use-transaction="false">
>> -        <description>Update Label Key</description>
>> -        <attribute name="key" type="String" mode="IN" optional="true"/>
>> -        <attribute name="keyComment" type="String" mode="IN"
>> optional="true"/>
>> -        <attribute name="update_label" type="String" mode="IN"
>> optional="false"/>
>> -        <attribute name="fileName" type="String" mode="IN"
>> optional="false"/>
>> -        <attribute name="confirm" type="String" mode="IN"
>> optional="true"/>
>> -        <attribute name="removeLabel" type="String" mode="IN"
>> optional="true"/>
>> -        <attribute name="localeNames" type="List" mode="IN"
>> optional="true"/>
>> -        <attribute name="localeValues" type="List" mode="IN"
>> optional="true"/>
>> -        <attribute name="localeComments" type="List" mode="IN"
>> optional="true"/>
>> -    </service>
>> -    <service name="updateAndSaveLabelKey" engine="java"
>> location="org.ofbiz.webtools.labelmanager.LabelManagerFactory"
>> -             invoke="updateAndSaveLabelKey" auth="true"
>> use-transaction="false">
>> -        <description>Update Label Key</description>
>>         <attribute name="key" type="String" mode="IN" optional="true"/>
>>         <attribute name="keyComment" type="String" mode="IN"
>> optional="true"/>
>>         <attribute name="update_label" type="String" mode="IN"
>> optional="false"/>
>>
>> Added:
>> ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelFile.java
>>
>> URL:
>> http://svn.apache.org/viewvc/ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelFile.java?rev=776988&view=auto 
>>
>> ==============================================================================
>>
>> ---
>> ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelFile.java
>> (added)
>> +++
>> ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelFile.java
>> Thu May 21 07:26:22 2009
>> @@ -0,0 +1,46 @@
>> +/*
>> + * 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.webtools.labelmanager;
>> +
>> +import java.io.File;
>> +
>> +public class LabelFile {
>> +    public static final String module = LabelFile.class.getName();
>> +
>> +    protected final boolean fileLoaded = false;;
>> +    protected final File file;
>> +    protected final String componentName;
>> +
>> +    protected LabelFile(File file, String componentName) {
>> +        this.file = file;
>> +        this.componentName = componentName;
>> +    }
>> +
>> +    public File getFile() {
>> +        return this.file;
>> +    }
>> +
>> +    public String getFileName() {
>> +        return this.file.getName();
>> +    }
>> +
>> +    public String getFilePath() {
>> +        return this.file.getPath();
>> +    }
>> +}
>>
>> Propchange:
>> ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelFile.java
>>
>> ------------------------------------------------------------------------------
>>
>>    svn:eol-style = native
>>
>> Propchange:
>> ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelFile.java
>>
>> ------------------------------------------------------------------------------
>>
>>    svn:keywords = "Date Rev Author URL Id"
>>
>> Propchange:
>> ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelFile.java
>>
>> ------------------------------------------------------------------------------
>>
>>    svn:mime-type = text/plain
>>
>> 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=776988&r1=776987&r2=776988&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
>> Thu May 21 07:26:22 2009
>> @@ -33,14 +33,12 @@
>>     protected String labelKey = "";
>>     protected String labelKeyComment = "";
>>     protected String fileName = "";
>> -    protected String componentName = "";
>>     protected Map<String, LabelValue> labelValues =
>> FastMap.newInstance();
>>
>> -    public LabelInfo(String labelKey, String labelKeyComment, String
>> fileName, String componentName, String localeStr, String
>> labelValue, String labelComment) throws GeneralException {
>> +    public LabelInfo(String labelKey, String labelKeyComment, String
>> fileName, String localeStr, String labelValue, String
>> labelComment) throws GeneralException {
>>         this.labelKey = labelKey;
>>         this.labelKeyComment = labelKeyComment;
>>         this.fileName = fileName;
>> -        this.componentName = componentName;
>>         setLabelValue(localeStr, labelValue, labelComment, false);
>>     }
>>
>> @@ -60,10 +58,6 @@
>>         return fileName;
>>     }
>>
>> -    public String getComponentName() {
>> -        return componentName;
>> -    }
>> -
>>     public LabelValue getLabelValue(String localeStr) {
>>        return (LabelValue)labelValues.get(localeStr);
>>     }
>>
>> 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=776988&r1=776987&r2=776988&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
>> Thu May 21 07:26:22 2009
>> @@ -20,331 +20,185 @@
>>
>> import java.io.File;
>> import java.io.IOException;
>> +import java.net.MalformedURLException;
>> import java.util.Collection;
>> import java.util.List;
>> -import java.util.Locale;
>> import java.util.Map;
>> import java.util.Set;
>> import java.util.TreeMap;
>> import java.util.TreeSet;
>>
>> +import javax.xml.parsers.ParserConfigurationException;
>> +
>> import javolution.util.FastList;
>>
>> import org.ofbiz.base.component.ComponentConfig;
>> +import org.ofbiz.base.component.ComponentConfig.ClasspathInfo;
>> +import org.ofbiz.base.util.Debug;
>> import org.ofbiz.base.util.FileUtil;
>> import org.ofbiz.base.util.GeneralException;
>> import org.ofbiz.base.util.StringUtil;
>> -import org.ofbiz.base.util.UtilGenerics;
>> -import org.ofbiz.base.util.UtilMisc;
>> -import org.ofbiz.base.util.UtilProperties;
>> import org.ofbiz.base.util.UtilValidate;
>> import org.ofbiz.base.util.UtilXml;
>> -import org.ofbiz.base.util.cache.UtilCache;
>> -import org.ofbiz.entity.GenericDelegator;
>> -import org.ofbiz.entity.GenericValue;
>> -import org.ofbiz.entity.model.ModelReader;
>> -import org.ofbiz.service.DispatchContext;
>> -import org.ofbiz.service.GenericServiceException;
>> -import org.ofbiz.service.LocalDispatcher;
>> -import org.ofbiz.service.ServiceUtil;
>> +import org.owasp.esapi.errors.EncodingException;
>> import org.w3c.dom.Comment;
>> import org.w3c.dom.Document;
>> import org.w3c.dom.Element;
>> import org.w3c.dom.Node;
>> +import org.xml.sax.SAXException;
>>
>> public class LabelManagerFactory {
>>
>>     public static final String module =
>> LabelManagerFactory.class.getName();
>>     public static final String resource = "WebtoolsUiLabels";
>> -
>>     public static final String keySeparator = "#";
>>
>> -    protected static UtilCache<String, LabelManagerFactory>
>> labelManagerFactoryCache = new UtilCache<String,
>> LabelManagerFactory>("LabelManagerFactory");
>> -
>> -    protected static Map<String, LabelInfo> labels = null;
>> -    protected static Map<String, String> fileNamesFound = null;
>> -    protected static Map<String, String> fileComponent = null;
>> -    protected static Set<String> localesFound = null;
>>     protected static Set<String> componentNamesFound = null;
>> -    protected static Map<String, Map<String, Integer>> references =
>> null;
>> -    protected static List<LabelInfo> duplicatedLocalesLabelsList = null;
>> -    protected static int duplicatedLocalesLabels = 0;
>> -
>> -    protected static GenericDelegator delegator;
>> -    protected static ModelReader entityModelReader;
>> -    protected static DispatchContext dispatchContext;
>> -
>> -    public static LabelManagerFactory getLabelManagerFactory(String
>> delegatorName) throws GeneralException {
>> -        if (UtilValidate.isEmpty(delegatorName)) {
>> -            delegatorName = "default";
>> -        }
>> +    protected static Map<String, LabelFile> filesFound = null;
>>
>> -        LabelManagerFactory lmf =
>> labelManagerFactoryCache.get(delegatorName);
>> +    protected Map<String, LabelInfo> labels = new TreeMap<String,
>> LabelInfo>();
>> +    protected Set<String> localesFound = new TreeSet<String>();
>> +    protected List<LabelInfo> duplicatedLocalesLabelsList =
>> FastList.newInstance();
>>
>> -        if (lmf == null) {
>> -            lmf = new LabelManagerFactory(delegatorName);
>> -            labelManagerFactoryCache.put(delegatorName, lmf);
>> +    public static synchronized LabelManagerFactory getInstance()
>> throws GeneralException, IOException {
>> +        if (componentNamesFound == null) {
>> +            loadComponentNames();
>>         }
>> -        return lmf;
>> +        if (filesFound == null) {
>> +            loadLabelFiles();
>> +        }
>> +        return new LabelManagerFactory();
>>     }
>>
>> -    protected LabelManagerFactory(String delegatorName) throws
>> GeneralException {
>> -        delegator = GenericDelegator.getGenericDelegator(delegatorName);
>> -        entityModelReader = ModelReader.getModelReader(delegatorName);
>> -        dispatchContext = new
>> DispatchContext("LabelManagerFactoryDispCtx", null,
>> this.getClass().getClassLoader(), null);
>> -        prepareAll();
>> +    protected LabelManagerFactory() {
>>     }
>>
>> -    private static void prepareAll() throws GeneralException {
>> -        labels = new TreeMap<String, LabelInfo>();
>> -        fileNamesFound = new TreeMap<String, String>();
>> -        fileComponent = new TreeMap<String, String>();
>> -        localesFound = new TreeSet<String>();
>> +    protected static void loadComponentNames() {
>>         componentNamesFound = new TreeSet<String>();
>> -        duplicatedLocalesLabelsList = FastList.newInstance();
>> -        references = null;
>> -        int duplicatedLocales = 0;
>> -
>> -        try {
>> -            boolean sharkComponent = false;
>> -            Collection<ComponentConfig> componentConfigs =
>> ComponentConfig.getAllComponents();
>> -
>> -            for (ComponentConfig componentConfig : componentConfigs) {
>> -                String componentName =
>> componentConfig.getComponentName();
>> -                List<File> resourceFiles =
>> FileUtil.findXmlFiles(componentConfig.getRootLocation(), null,
>> "resource", null);
>> -                boolean cycle = true;
>> -                while (cycle) {
>> -                    for (File resourceFile : resourceFiles) {
>> -                        String fileName = resourceFile.getName();
>> -                        Document resourceDocument =
>> UtilXml.readXmlDocument(resourceFile.toURI().toURL());
>> -                        Element resourceElem =
>> resourceDocument.getDocumentElement();
>> -                        String labelKeyComment = "";
>> -
>> -                        for (Node propertyNode :
>> UtilXml.childNodeList(resourceElem.getFirstChild())) {
>> -                            if (propertyNode instanceof Element) {
>> -                                Element propertyElem = (Element)
>> propertyNode;
>> -                                String labelKey =
>> StringUtil.defaultWebEncoder.canonicalize(propertyElem.getAttribute("key"));
>>
>> -                                String labelComment = "";
>> -
>> -                                for (Node valueNode :
>> UtilXml.childNodeList(propertyElem.getFirstChild())) {
>> -                                    if (valueNode instanceof Element) {
>> -                                        Element valueElem = (Element)
>> valueNode;
>> -                                        String localeName =
>> valueElem.getAttribute("xml:lang");
>> -                                        String labelValue =
>> StringUtil.defaultWebEncoder.canonicalize(UtilXml.nodeValue(valueElem.getFirstChild()));
>>
>> -                                        LabelInfo label =
>> labels.get(labelKey + keySeparator + fileName);
>> -
>> -                                        if
>> (UtilValidate.isEmpty(label)) {
>> -                                            label = new
>> LabelInfo(labelKey, labelKeyComment, fileName, componentName, localeName,
>> labelValue, labelComment);
>> -                                            labels.put(labelKey +
>> keySeparator + fileName, label);
>> -                                        } else {
>> -                                            if
>> (label.setLabelValue(localeName, labelValue, labelComment, false)) {
>> -                                                
>> duplicatedLocalesLabelsList.add(label);
>> -                                                duplicatedLocales++;
>> -                                            }
>> -                                        }
>> -                                        localesFound.add(localeName);
>> -                                        
>> componentNamesFound.add(componentName);
>> -                                        fileNamesFound.put(fileName,
>> resourceFile.toURI().toString());
>> -                                        fileComponent.put(fileName,
>> componentName);
>> -                                        labelComment = "";
>> -                                    } else if (valueNode instanceof
>> Comment) {
>> -                                        labelComment = labelComment +
>> StringUtil.defaultWebEncoder.canonicalize(valueNode.getNodeValue());
>> -                                    }
>> +        Collection<ComponentConfig> componentConfigs =
>> ComponentConfig.getAllComponents();
>> +        for (ComponentConfig componentConfig : componentConfigs) {
>> +            componentNamesFound.add(componentConfig.getComponentName());
>> +        }
>> +        componentNamesFound.add("shark");
>> +    }
>> +
>> +    protected static void loadLabelFiles() throws IOException {
>> +        filesFound = new TreeMap<String, LabelFile>();
>> +        List<ClasspathInfo> cpInfos =
>> ComponentConfig.getAllClasspathInfos();
>> +        for (ClasspathInfo cpi : cpInfos) {
>> +            if ("dir".equals(cpi.type)) {
>> +                String configRoot =
>> cpi.componentConfig.getRootLocation();
>> +                configRoot = configRoot.replace('\\', '/');
>> +                if (!configRoot.endsWith("/")) {
>> +                    configRoot = configRoot + "/";
>> +                }
>> +                String location = cpi.location.replace('\\', '/');
>> +                if (location.startsWith("/")) {
>> +                    location = location.substring(1);
>> +                }
>> +                List<File> resourceFiles =
>> FileUtil.findXmlFiles(configRoot + location, null, "resource", null);
>> +                for (File resourceFile : resourceFiles) {
>> +                    filesFound.put(resourceFile.getName(), new
>> LabelFile(resourceFile, cpi.componentConfig.getComponentName()));
>> +                }
>> +            }
>> +        }
>> +        List<File> resourceFiles =
>> FileUtil.findXmlFiles(System.getProperty("ofbiz.home") +
>> "/specialpurpose/shark/config", null,
>> "resource", null);
>> +        for (File resourceFile : resourceFiles) {
>> +            filesFound.put(resourceFile.getName(), new
>> LabelFile(resourceFile, "shark"));
>> +        }
>> +    }
>> +
>> +    public void findMatchingLabels(String component, String fileName,
>> String key, String locale) throws MalformedURLException,
>> SAXException, ParserConfigurationException, IOException,
>> EncodingException, GeneralException {
>> +        if (UtilValidate.isEmpty(component) &&
>> UtilValidate.isEmpty(fileName) && UtilValidate.isEmpty(key) &&
>> UtilValidate.isEmpty(locale)) {
>> +            // Important! Don't allow unparameterized queries - doing
>> so will result in loading the entire project into memory
>> +            return;
>> +        }
>> +        for (LabelFile fileInfo : filesFound.values()) {
>> +            if (UtilValidate.isNotEmpty(component) &&
>> !component.equals(fileInfo.componentName)) {
>> +                continue;
>> +            }
>> +            if (UtilValidate.isNotEmpty(fileName) &&
>> !fileName.equals(fileInfo.getFileName())) {
>> +                continue;
>> +            }
>> +            Document resourceDocument =
>> UtilXml.readXmlDocument(fileInfo.file.toURL(), false);
>> +            Element resourceElem =
>> resourceDocument.getDocumentElement();
>> +            String labelKeyComment = "";
>> +            for (Node propertyNode :
>> UtilXml.childNodeList(resourceElem.getFirstChild())) {
>> +                if (propertyNode instanceof Element) {
>> +                    Element propertyElem = (Element) propertyNode;
>> +                    String labelKey =
>> StringUtil.defaultWebEncoder.canonicalize(propertyElem.getAttribute("key"));
>>
>> +                    String labelComment = "";
>> +                    for (Node valueNode :
>> UtilXml.childNodeList(propertyElem.getFirstChild())) {
>> +                        if (valueNode instanceof Element) {
>> +                            Element valueElem = (Element) valueNode;
>> +                            String localeName =
>> valueElem.getAttribute("xml:lang");
>> +                            String labelValue =
>> StringUtil.defaultWebEncoder.canonicalize(UtilXml.nodeValue(valueElem.getFirstChild()));
>>
>> +                            LabelInfo label = labels.get(labelKey +
>> keySeparator + fileInfo.getFileName());
>> +
>> +                            if (UtilValidate.isEmpty(label)) {
>> +                                label = new LabelInfo(labelKey,
>> labelKeyComment, fileInfo.getFileName(), localeName, labelValue,
>> labelComment);
>> +                                labels.put(labelKey + keySeparator +
>> fileInfo.getFileName(), label);
>> +                            } else {
>> +                                if (label.setLabelValue(localeName,
>> labelValue, labelComment, false)) {
>> +                                    
>> duplicatedLocalesLabelsList.add(label);
>>                                 }
>> -                                labelKeyComment = "";
>> -                            } else if (propertyNode instanceof
>> Comment) {
>> -                                labelKeyComment = labelKeyComment +
>> StringUtil.defaultWebEncoder.canonicalize(propertyNode.getNodeValue());
>>                             }
>> +                            localesFound.add(localeName);
>> +                            labelComment = "";
>> +                        } else if (valueNode instanceof Comment) {
>> +                            labelComment = labelComment +
>> StringUtil.defaultWebEncoder.canonicalize(valueNode.getNodeValue());
>>                         }
>>                     }
>> -                    if (!sharkComponent) {
>> -                        componentName = "shark";
>> -                        resourceFiles =
>> FileUtil.findXmlFiles(System.getProperty("ofbiz.home") +
>> "/specialpurpose/shark", null,
>> "resource", null);
>> -                        sharkComponent = true;
>> -                    } else {
>> -                        cycle = false;
>> -                    }
>> +                    labelKeyComment = "";
>> +                } else if (propertyNode instanceof Comment) {
>> +                    labelKeyComment = labelKeyComment +
>> StringUtil.defaultWebEncoder.canonicalize(propertyNode.getNodeValue());
>>                 }
>>             }
>> -
>> -            // get labels references from sources
>> -            references = LabelReferences.getLabelReferences();
>> -        } catch (IOException ioe) {
>> -            throw new GeneralException(ioe.getMessage());
>> -        } catch (Exception e) {
>> -            throw new GeneralException(e.getMessage());
>>         }
>> -        duplicatedLocalesLabels = duplicatedLocales;
>> -    }
>> -
>> -    public static GenericDelegator getDelegator() {
>> -        return delegator;
>>     }
>>
>> -    public static ModelReader getModelReader() {
>> -        return entityModelReader;
>> +    public LabelFile getLabelFile(String fileName) {
>> +        return filesFound.get(fileName);
>>     }
>>
>> -    public static DispatchContext getDispatchContext() {
>> -        return dispatchContext;
>> -    }
>> -
>> -    public static Map<String, LabelInfo> getLabels() {
>> +    public Map<String, LabelInfo> getLabels() {
>>         return labels;
>>     }
>>
>> -    public static Set<String> getLocalesFound() {
>> +    public Set<String> getLocalesFound() {
>>         return localesFound;
>>     }
>>
>> -    public static Map<String, String> getFileNamesFound() {
>> -        return fileNamesFound;
>> -    }
>> -
>> -    public static String getFileComponent(String fileName) {
>> -        String componentName = null;
>> -        if (UtilValidate.isNotEmpty(fileName)) {
>> -            componentName = fileComponent.get(fileName);
>> -        }
>> -        return componentName;
>> +    public static Collection<LabelFile> getFilesFound() {
>> +        return filesFound.values();
>>     }
>>
>>     public static Set<String> getComponentNamesFound() {
>>         return componentNamesFound;
>>     }
>>
>> -    public static Map<String, Map<String, Integer>> getReferences() {
>> -        return references;
>> -    }
>> -
>> -    public static Set<String> getLabelsList() {
>> +    public Set<String> getLabelsList() {
>>         return labels.keySet();
>>     }
>>
>> -    public static Set<String> getReferencesList() {
>> -        return references.keySet();
>> +    public int getDuplicatedLocalesLabels() {
>> +        return duplicatedLocalesLabelsList.size();
>>     }
>>
>> -    public static int getLabelReferenceFile(String key) {
>> -        int refFile = 0;
>> -        boolean keyFound = false;
>> -
>> -        if (key == null) {
>> -            key = "";
>> -        }
>> -
>> -        for (Map.Entry<String, String> e : fileNamesFound.entrySet()) {
>> -            String keyToSearch = key + keySeparator + e.getKey();
>> -
>> -            if (labels.containsKey(keyToSearch)) {
>> -                keyFound = true;
>> -                break;
>> -            }
>> -        }
>> -
>> -        if (!keyFound) {
>> -            Map<String, Integer> reference = references.get(key);
>> -
>> -            if (UtilValidate.isNotEmpty(reference)) {
>> -                refFile = reference.size();
>> -            }
>> -        }
>> -
>> -        return refFile;
>> -    }
>> -
>> -    public static int getDuplicatedLocalesLabels() {
>> -        return duplicatedLocalesLabels;
>> -    }
>> -
>> -    public static List<LabelInfo> getDuplicatedLocalesLabelsList() {
>> +    public List<LabelInfo> getDuplicatedLocalesLabelsList() {
>>         return duplicatedLocalesLabelsList;
>>     }
>>
>> -    public static Map<String, Object> updateLabelKey(DispatchContext
>> dctx, Map<String, ? extends Object> context) {
>> -        String key = (String) context.get("key");
>> -        String keyComment = (String) context.get("keyComment");
>> -        String update_label = (String) context.get("update_label");
>> -        String fileName = (String) context.get("fileName");
>> -        String confirm = (String) context.get("confirm");
>> -        String removeLabel = (String) context.get("removeLabel");
>> -        List<String> localeNames =
>> UtilGenerics.cast(context.get("localeNames"));
>> -        List<String> localeValues =
>> UtilGenerics.cast(context.get("localeValues"));
>> -        List<String> localeComments =
>> UtilGenerics.cast(context.get("localeComments"));
>> -        Locale locale = (Locale) context.get("locale");
>> -
>> -        // Remove a Label
>> -        if (UtilValidate.isNotEmpty(removeLabel)) {
>> -            labels.remove(key + keySeparator + fileName);
>> -        } else if (UtilValidate.isNotEmpty(confirm)) {
>> -            LabelInfo label = labels.get(key + keySeparator + fileName);
>> -
>> -            // Update a Label
>> -            if (update_label.equalsIgnoreCase("Y")) {
>> -                if (UtilValidate.isNotEmpty(label)) {
>> -                    updateLabelValue(localeNames, localeValues,
>> localeComments, label, key, keyComment, fileName);
>> -                }
>> -                // Insert a new Label
>> -            } else {
>> -                if (UtilValidate.isNotEmpty(label)) {
>> -                    return
>> ServiceUtil.returnError(UtilProperties.getMessage(resource,
>> "WebtoolsLabelManagerNewLabelExisting",
>> UtilMisc.toMap("key", key, "fileName", fileName), locale));
>> -                } else {
>> -                    if (UtilValidate.isEmpty(key)) {
>> -                        return
>> ServiceUtil.returnError(UtilProperties.getMessage(resource,
>> "WebtoolsLabelManagerNewLabelEmptyKey", locale));
>> -                    } else {
>> -                        int notEmptyLabels =
>> updateLabelValue(localeNames, localeValues, localeComments, null, key,
>> keyComment,
>> fileName);
>> -                        if (notEmptyLabels == 0) {
>> -                            return
>> ServiceUtil.returnError(UtilProperties.getMessage(resource,
>> "WebtoolsLabelManagerNewLabelEmpty", locale));
>> -                        }
>> -                    }
>> -                }
>> -            }
>> -        }
>> -
>> -        return ServiceUtil.returnSuccess();
>> -    }
>> -
>> -    public static Map<String, Object>
>> updateAndSaveLabelKey(DispatchContext dctx, Map<String, ? extends
>> Object> context) {
>> -        String key = (String) context.get("key");
>> -        String keyComment = (String) context.get("keyComment");
>> -        String update_label = (String) context.get("update_label");
>> -        String fileName = (String) context.get("fileName");
>> -        String confirm = (String) context.get("confirm");
>> -        String removeLabel = (String) context.get("removeLabel");
>> -        List<String> localeNames =
>> UtilGenerics.cast(context.get("localeNames"));
>> -        List<String> localeValues =
>> UtilGenerics.cast(context.get("localeValues"));
>> -        List<String> localeComments =
>> UtilGenerics.cast(context.get("localeComments"));
>> -        GenericValue userLogin = (GenericValue)
>> context.get("userLogin");
>> -        LocalDispatcher dispatcher = dctx.getDispatcher();
>> -
>> -        Map<String, Object> contextInput = UtilMisc.toMap("key", key,
>> "keyComment", keyComment, "update_label", update_label,
>> "fileName", fileName, "confirm", confirm, "removeLabel", removeLabel,
>> -                "localeNames", localeNames, "localeValues",
>> localeValues, "localeComments", localeComments, "userLogin",
>> userLogin);
>> -        try {
>> -            Map<String, Object> updatedKey =
>> dispatcher.runSync("updateLabelKey", contextInput);
>> -
>> -            if (ServiceUtil.isError(updatedKey)) {
>> -                return updatedKey;
>> -            } else {
>> -                return dispatcher.runSync("saveLabelsToXmlFile",
>> UtilMisc.toMap("labelFileName", fileName, "userLogin",
>> userLogin));
>> -            }
>> -        } catch (GenericServiceException e) {
>> -            return ServiceUtil.returnError("error on saving label key
>> :" + key);
>> -        }
>> -    }
>> -
>> -    private static int updateLabelValue(List<String> localeNames,
>> List<String> localeValues, List<String> localeComments,
>> LabelInfo label, String key, String keyComment, String fileName) {
>> +    public int updateLabelValue(List<String> localeNames,
>> List<String> localeValues, List<String> localeComments, LabelInfo
>> label, String key, String keyComment, String fileName) {
>>         int notEmptyLabels = 0;
>> -        int i = 0;
>> -        while (i < localeNames.size()) {
>> +        for (int i = 0; i < localeNames.size(); i++) {
>>             String localeName = localeNames.get(i);
>>             String localeValue = localeValues.get(i);
>>             String localeComment = localeComments.get(i);
>> -
>>             if (UtilValidate.isNotEmpty(localeValue) ||
>> UtilValidate.isNotEmpty(localeComment)) {
>>                 if (label == null) {
>>                     try {
>> -                        String componentName =
>> getFileComponent(fileName);
>> -                        label = new LabelInfo(key, keyComment,
>> fileName, componentName, localeName, localeValue, localeComment);
>> +                        label = new LabelInfo(key, keyComment,
>> fileName, localeName, localeValue, localeComment);
>>                         labels.put(key + keySeparator + fileName, label);
>>                     } catch (Exception e) {
>>                         e.printStackTrace();
>> @@ -355,9 +209,7 @@
>>                 label.setLabelValue(localeName, localeValue,
>> localeComment, true);
>>                 notEmptyLabels++;
>>             }
>> -            i++;
>>         }
>> -
>>         return notEmptyLabels;
>>     }
>> }
>>
>>
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: svn commit: r776988 [1/3] - in /ofbiz/trunk/framework/webtools: config/ servicedef/ src/org/ofbiz/webtools/labelmanager/ webapp/webtools/WEB-INF/ webapp/webtools/WEB-INF/actions/labelmanager/ webapp/webtools/labelmanager/ widget/

Adrian Crum
In reply to this post by Jacques Le Roux
Btw, it wasn't my intention to rewrite so much of it. I started out just
trying to fix a file naming problem that Adam pointed out, which led me
to another problem, which led me to another problem...

-Adrian

Jacques Le Roux wrote:

> Hi Adrian,
>
> Marco wrote this code and I commited it. Actually I must say I did no
> review all details as you did. It was working, I needed it, so I though
> it could be useful for other persons. Actually it did not even occur to
> me that it could be used by several persons concurrently, which is of
> course a big miss :/
>
> Actually as I said it was working but I'm sorry to say that, apart if I
> miss something, it does not work any longer...
> For instance, if you search with all default options, nothing happens.
> And you can't anymore select a language.
>
> Thanks
>
> Jacques
>
> From: <[hidden email]>
>> Author: adrianc
>> Date: Thu May 21 07:26:22 2009
>> New Revision: 776988
>>
>> URL: http://svn.apache.org/viewvc?rev=776988&view=rev
>> Log:
>> Refactored the Webtools Label Manager - due to a multitude of
>> problems. Most notably:
>>
>> 1. Not thread safe.
>> 2. Server files were being read/written without multi-user
>> synchronization
>> 3. Reads all UI labels into memory, then stores them in static memory,
>> PLUS
>> 4. Finds all references of those labels in project files and store
>> those references in static memory (!!!)
>> 5. Doesn't follow best practices for UI layout
>>
>> It's amazing the original code worked. Even more amazing is the fact
>> that this code made it into the project.
>>
>> Added:
>>    
>> ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelFile.java  
>> (with props)
>>    
>> ofbiz/trunk/framework/webtools/webapp/webtools/WEB-INF/actions/labelmanager/ViewReferences.groovy  
>> (with props)
>> Modified:
>>    ofbiz/trunk/framework/webtools/config/WebtoolsUiLabels.xml
>>    ofbiz/trunk/framework/webtools/servicedef/services.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/LabelReferences.java
>>
>>    
>> ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/SaveLabelsToXmlFile.java
>>
>>    
>> ofbiz/trunk/framework/webtools/webapp/webtools/WEB-INF/actions/labelmanager/LabelManager.groovy
>>
>>    
>> ofbiz/trunk/framework/webtools/webapp/webtools/WEB-INF/actions/labelmanager/UpdateManager.groovy
>>
>>    
>> ofbiz/trunk/framework/webtools/webapp/webtools/WEB-INF/actions/labelmanager/ViewFile.groovy
>>
>>    ofbiz/trunk/framework/webtools/webapp/webtools/WEB-INF/controller.xml
>>    
>> ofbiz/trunk/framework/webtools/webapp/webtools/labelmanager/SearchLabels.ftl
>>
>>    
>> ofbiz/trunk/framework/webtools/webapp/webtools/labelmanager/UpdateLabel.ftl
>>
>>    
>> ofbiz/trunk/framework/webtools/webapp/webtools/labelmanager/ViewFile.ftl
>>    
>> ofbiz/trunk/framework/webtools/webapp/webtools/labelmanager/ViewHardcodedLabels.ftl
>>
>>    
>> ofbiz/trunk/framework/webtools/webapp/webtools/labelmanager/ViewLabels.ftl
>>
>>    
>> ofbiz/trunk/framework/webtools/webapp/webtools/labelmanager/ViewReferences.ftl
>>
>>    ofbiz/trunk/framework/webtools/widget/LabelManagerScreens.xml
>>
>> Modified: ofbiz/trunk/framework/webtools/config/WebtoolsUiLabels.xml
>> URL:
>> http://svn.apache.org/viewvc/ofbiz/trunk/framework/webtools/config/WebtoolsUiLabels.xml?rev=776988&r1=776987&r2=776988&view=diff 
>>
>> ==============================================================================
>>
>> --- ofbiz/trunk/framework/webtools/config/WebtoolsUiLabels.xml (original)
>> +++ ofbiz/trunk/framework/webtools/config/WebtoolsUiLabels.xml Thu May
>> 21 07:26:22 2009
>> @@ -1512,6 +1512,11 @@
>>         <value xml:lang="fr">Nom de fichier</value>
>>         <value xml:lang="it">Nome File</value>
>>     </property>
>> +    <property key="WebtoolsLabelManagerFindLabels">
>> +        <value xml:lang="en">Find Labels</value>
>> +        <value xml:lang="fr">Gestionnaire de traduction</value>
>> +        <value xml:lang="it">Gestione delle label</value>
>> +    </property>
>>     <property key="WebtoolsLabelManagerHardcoded">
>>         <value xml:lang="en">Hardcoded labels</value>
>>         <value xml:lang="it">Label non traducibili</value>
>>
>> Modified: ofbiz/trunk/framework/webtools/servicedef/services.xml
>> URL:
>> http://svn.apache.org/viewvc/ofbiz/trunk/framework/webtools/servicedef/services.xml?rev=776988&r1=776987&r2=776988&view=diff 
>>
>> ==============================================================================
>>
>> --- ofbiz/trunk/framework/webtools/servicedef/services.xml (original)
>> +++ ofbiz/trunk/framework/webtools/servicedef/services.xml Thu May 21
>> 07:26:22 2009
>> @@ -123,27 +123,10 @@
>>         <attribute name="eomodeldFullPath" type="java.lang.String"
>> mode="IN" optional="false"/>
>>         <attribute name="serviceName" type="java.lang.String"
>> mode="IN" optional="false"/>
>>     </service>
>> +
>>     <service name="saveLabelsToXmlFile" engine="java"
>> location="org.ofbiz.webtools.labelmanager.SaveLabelsToXmlFile"
>>              invoke="saveLabelsToXmlFile" auth="true"
>> use-transaction="false">
>>         <description>Save labels to xml file</description>
>> -        <attribute name="labelFileName" type="String" mode="IN"
>> optional="true"/>
>> -    </service>
>> -    <service name="updateLabelKey" engine="java"
>> location="org.ofbiz.webtools.labelmanager.LabelManagerFactory"
>> -             invoke="updateLabelKey" auth="true"
>> use-transaction="false">
>> -        <description>Update Label Key</description>
>> -        <attribute name="key" type="String" mode="IN" optional="true"/>
>> -        <attribute name="keyComment" type="String" mode="IN"
>> optional="true"/>
>> -        <attribute name="update_label" type="String" mode="IN"
>> optional="false"/>
>> -        <attribute name="fileName" type="String" mode="IN"
>> optional="false"/>
>> -        <attribute name="confirm" type="String" mode="IN"
>> optional="true"/>
>> -        <attribute name="removeLabel" type="String" mode="IN"
>> optional="true"/>
>> -        <attribute name="localeNames" type="List" mode="IN"
>> optional="true"/>
>> -        <attribute name="localeValues" type="List" mode="IN"
>> optional="true"/>
>> -        <attribute name="localeComments" type="List" mode="IN"
>> optional="true"/>
>> -    </service>
>> -    <service name="updateAndSaveLabelKey" engine="java"
>> location="org.ofbiz.webtools.labelmanager.LabelManagerFactory"
>> -             invoke="updateAndSaveLabelKey" auth="true"
>> use-transaction="false">
>> -        <description>Update Label Key</description>
>>         <attribute name="key" type="String" mode="IN" optional="true"/>
>>         <attribute name="keyComment" type="String" mode="IN"
>> optional="true"/>
>>         <attribute name="update_label" type="String" mode="IN"
>> optional="false"/>
>>
>> Added:
>> ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelFile.java
>>
>> URL:
>> http://svn.apache.org/viewvc/ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelFile.java?rev=776988&view=auto 
>>
>> ==============================================================================
>>
>> ---
>> ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelFile.java
>> (added)
>> +++
>> ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelFile.java
>> Thu May 21 07:26:22 2009
>> @@ -0,0 +1,46 @@
>> +/*
>> + * 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.webtools.labelmanager;
>> +
>> +import java.io.File;
>> +
>> +public class LabelFile {
>> +    public static final String module = LabelFile.class.getName();
>> +
>> +    protected final boolean fileLoaded = false;;
>> +    protected final File file;
>> +    protected final String componentName;
>> +
>> +    protected LabelFile(File file, String componentName) {
>> +        this.file = file;
>> +        this.componentName = componentName;
>> +    }
>> +
>> +    public File getFile() {
>> +        return this.file;
>> +    }
>> +
>> +    public String getFileName() {
>> +        return this.file.getName();
>> +    }
>> +
>> +    public String getFilePath() {
>> +        return this.file.getPath();
>> +    }
>> +}
>>
>> Propchange:
>> ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelFile.java
>>
>> ------------------------------------------------------------------------------
>>
>>    svn:eol-style = native
>>
>> Propchange:
>> ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelFile.java
>>
>> ------------------------------------------------------------------------------
>>
>>    svn:keywords = "Date Rev Author URL Id"
>>
>> Propchange:
>> ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelFile.java
>>
>> ------------------------------------------------------------------------------
>>
>>    svn:mime-type = text/plain
>>
>> 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=776988&r1=776987&r2=776988&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
>> Thu May 21 07:26:22 2009
>> @@ -33,14 +33,12 @@
>>     protected String labelKey = "";
>>     protected String labelKeyComment = "";
>>     protected String fileName = "";
>> -    protected String componentName = "";
>>     protected Map<String, LabelValue> labelValues =
>> FastMap.newInstance();
>>
>> -    public LabelInfo(String labelKey, String labelKeyComment, String
>> fileName, String componentName, String localeStr, String
>> labelValue, String labelComment) throws GeneralException {
>> +    public LabelInfo(String labelKey, String labelKeyComment, String
>> fileName, String localeStr, String labelValue, String
>> labelComment) throws GeneralException {
>>         this.labelKey = labelKey;
>>         this.labelKeyComment = labelKeyComment;
>>         this.fileName = fileName;
>> -        this.componentName = componentName;
>>         setLabelValue(localeStr, labelValue, labelComment, false);
>>     }
>>
>> @@ -60,10 +58,6 @@
>>         return fileName;
>>     }
>>
>> -    public String getComponentName() {
>> -        return componentName;
>> -    }
>> -
>>     public LabelValue getLabelValue(String localeStr) {
>>        return (LabelValue)labelValues.get(localeStr);
>>     }
>>
>> 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=776988&r1=776987&r2=776988&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
>> Thu May 21 07:26:22 2009
>> @@ -20,331 +20,185 @@
>>
>> import java.io.File;
>> import java.io.IOException;
>> +import java.net.MalformedURLException;
>> import java.util.Collection;
>> import java.util.List;
>> -import java.util.Locale;
>> import java.util.Map;
>> import java.util.Set;
>> import java.util.TreeMap;
>> import java.util.TreeSet;
>>
>> +import javax.xml.parsers.ParserConfigurationException;
>> +
>> import javolution.util.FastList;
>>
>> import org.ofbiz.base.component.ComponentConfig;
>> +import org.ofbiz.base.component.ComponentConfig.ClasspathInfo;
>> +import org.ofbiz.base.util.Debug;
>> import org.ofbiz.base.util.FileUtil;
>> import org.ofbiz.base.util.GeneralException;
>> import org.ofbiz.base.util.StringUtil;
>> -import org.ofbiz.base.util.UtilGenerics;
>> -import org.ofbiz.base.util.UtilMisc;
>> -import org.ofbiz.base.util.UtilProperties;
>> import org.ofbiz.base.util.UtilValidate;
>> import org.ofbiz.base.util.UtilXml;
>> -import org.ofbiz.base.util.cache.UtilCache;
>> -import org.ofbiz.entity.GenericDelegator;
>> -import org.ofbiz.entity.GenericValue;
>> -import org.ofbiz.entity.model.ModelReader;
>> -import org.ofbiz.service.DispatchContext;
>> -import org.ofbiz.service.GenericServiceException;
>> -import org.ofbiz.service.LocalDispatcher;
>> -import org.ofbiz.service.ServiceUtil;
>> +import org.owasp.esapi.errors.EncodingException;
>> import org.w3c.dom.Comment;
>> import org.w3c.dom.Document;
>> import org.w3c.dom.Element;
>> import org.w3c.dom.Node;
>> +import org.xml.sax.SAXException;
>>
>> public class LabelManagerFactory {
>>
>>     public static final String module =
>> LabelManagerFactory.class.getName();
>>     public static final String resource = "WebtoolsUiLabels";
>> -
>>     public static final String keySeparator = "#";
>>
>> -    protected static UtilCache<String, LabelManagerFactory>
>> labelManagerFactoryCache = new UtilCache<String,
>> LabelManagerFactory>("LabelManagerFactory");
>> -
>> -    protected static Map<String, LabelInfo> labels = null;
>> -    protected static Map<String, String> fileNamesFound = null;
>> -    protected static Map<String, String> fileComponent = null;
>> -    protected static Set<String> localesFound = null;
>>     protected static Set<String> componentNamesFound = null;
>> -    protected static Map<String, Map<String, Integer>> references =
>> null;
>> -    protected static List<LabelInfo> duplicatedLocalesLabelsList = null;
>> -    protected static int duplicatedLocalesLabels = 0;
>> -
>> -    protected static GenericDelegator delegator;
>> -    protected static ModelReader entityModelReader;
>> -    protected static DispatchContext dispatchContext;
>> -
>> -    public static LabelManagerFactory getLabelManagerFactory(String
>> delegatorName) throws GeneralException {
>> -        if (UtilValidate.isEmpty(delegatorName)) {
>> -            delegatorName = "default";
>> -        }
>> +    protected static Map<String, LabelFile> filesFound = null;
>>
>> -        LabelManagerFactory lmf =
>> labelManagerFactoryCache.get(delegatorName);
>> +    protected Map<String, LabelInfo> labels = new TreeMap<String,
>> LabelInfo>();
>> +    protected Set<String> localesFound = new TreeSet<String>();
>> +    protected List<LabelInfo> duplicatedLocalesLabelsList =
>> FastList.newInstance();
>>
>> -        if (lmf == null) {
>> -            lmf = new LabelManagerFactory(delegatorName);
>> -            labelManagerFactoryCache.put(delegatorName, lmf);
>> +    public static synchronized LabelManagerFactory getInstance()
>> throws GeneralException, IOException {
>> +        if (componentNamesFound == null) {
>> +            loadComponentNames();
>>         }
>> -        return lmf;
>> +        if (filesFound == null) {
>> +            loadLabelFiles();
>> +        }
>> +        return new LabelManagerFactory();
>>     }
>>
>> -    protected LabelManagerFactory(String delegatorName) throws
>> GeneralException {
>> -        delegator = GenericDelegator.getGenericDelegator(delegatorName);
>> -        entityModelReader = ModelReader.getModelReader(delegatorName);
>> -        dispatchContext = new
>> DispatchContext("LabelManagerFactoryDispCtx", null,
>> this.getClass().getClassLoader(), null);
>> -        prepareAll();
>> +    protected LabelManagerFactory() {
>>     }
>>
>> -    private static void prepareAll() throws GeneralException {
>> -        labels = new TreeMap<String, LabelInfo>();
>> -        fileNamesFound = new TreeMap<String, String>();
>> -        fileComponent = new TreeMap<String, String>();
>> -        localesFound = new TreeSet<String>();
>> +    protected static void loadComponentNames() {
>>         componentNamesFound = new TreeSet<String>();
>> -        duplicatedLocalesLabelsList = FastList.newInstance();
>> -        references = null;
>> -        int duplicatedLocales = 0;
>> -
>> -        try {
>> -            boolean sharkComponent = false;
>> -            Collection<ComponentConfig> componentConfigs =
>> ComponentConfig.getAllComponents();
>> -
>> -            for (ComponentConfig componentConfig : componentConfigs) {
>> -                String componentName =
>> componentConfig.getComponentName();
>> -                List<File> resourceFiles =
>> FileUtil.findXmlFiles(componentConfig.getRootLocation(), null,
>> "resource", null);
>> -                boolean cycle = true;
>> -                while (cycle) {
>> -                    for (File resourceFile : resourceFiles) {
>> -                        String fileName = resourceFile.getName();
>> -                        Document resourceDocument =
>> UtilXml.readXmlDocument(resourceFile.toURI().toURL());
>> -                        Element resourceElem =
>> resourceDocument.getDocumentElement();
>> -                        String labelKeyComment = "";
>> -
>> -                        for (Node propertyNode :
>> UtilXml.childNodeList(resourceElem.getFirstChild())) {
>> -                            if (propertyNode instanceof Element) {
>> -                                Element propertyElem = (Element)
>> propertyNode;
>> -                                String labelKey =
>> StringUtil.defaultWebEncoder.canonicalize(propertyElem.getAttribute("key"));
>>
>> -                                String labelComment = "";
>> -
>> -                                for (Node valueNode :
>> UtilXml.childNodeList(propertyElem.getFirstChild())) {
>> -                                    if (valueNode instanceof Element) {
>> -                                        Element valueElem = (Element)
>> valueNode;
>> -                                        String localeName =
>> valueElem.getAttribute("xml:lang");
>> -                                        String labelValue =
>> StringUtil.defaultWebEncoder.canonicalize(UtilXml.nodeValue(valueElem.getFirstChild()));
>>
>> -                                        LabelInfo label =
>> labels.get(labelKey + keySeparator + fileName);
>> -
>> -                                        if
>> (UtilValidate.isEmpty(label)) {
>> -                                            label = new
>> LabelInfo(labelKey, labelKeyComment, fileName, componentName, localeName,
>> labelValue, labelComment);
>> -                                            labels.put(labelKey +
>> keySeparator + fileName, label);
>> -                                        } else {
>> -                                            if
>> (label.setLabelValue(localeName, labelValue, labelComment, false)) {
>> -                                                
>> duplicatedLocalesLabelsList.add(label);
>> -                                                duplicatedLocales++;
>> -                                            }
>> -                                        }
>> -                                        localesFound.add(localeName);
>> -                                        
>> componentNamesFound.add(componentName);
>> -                                        fileNamesFound.put(fileName,
>> resourceFile.toURI().toString());
>> -                                        fileComponent.put(fileName,
>> componentName);
>> -                                        labelComment = "";
>> -                                    } else if (valueNode instanceof
>> Comment) {
>> -                                        labelComment = labelComment +
>> StringUtil.defaultWebEncoder.canonicalize(valueNode.getNodeValue());
>> -                                    }
>> +        Collection<ComponentConfig> componentConfigs =
>> ComponentConfig.getAllComponents();
>> +        for (ComponentConfig componentConfig : componentConfigs) {
>> +            componentNamesFound.add(componentConfig.getComponentName());
>> +        }
>> +        componentNamesFound.add("shark");
>> +    }
>> +
>> +    protected static void loadLabelFiles() throws IOException {
>> +        filesFound = new TreeMap<String, LabelFile>();
>> +        List<ClasspathInfo> cpInfos =
>> ComponentConfig.getAllClasspathInfos();
>> +        for (ClasspathInfo cpi : cpInfos) {
>> +            if ("dir".equals(cpi.type)) {
>> +                String configRoot =
>> cpi.componentConfig.getRootLocation();
>> +                configRoot = configRoot.replace('\\', '/');
>> +                if (!configRoot.endsWith("/")) {
>> +                    configRoot = configRoot + "/";
>> +                }
>> +                String location = cpi.location.replace('\\', '/');
>> +                if (location.startsWith("/")) {
>> +                    location = location.substring(1);
>> +                }
>> +                List<File> resourceFiles =
>> FileUtil.findXmlFiles(configRoot + location, null, "resource", null);
>> +                for (File resourceFile : resourceFiles) {
>> +                    filesFound.put(resourceFile.getName(), new
>> LabelFile(resourceFile, cpi.componentConfig.getComponentName()));
>> +                }
>> +            }
>> +        }
>> +        List<File> resourceFiles =
>> FileUtil.findXmlFiles(System.getProperty("ofbiz.home") +
>> "/specialpurpose/shark/config", null,
>> "resource", null);
>> +        for (File resourceFile : resourceFiles) {
>> +            filesFound.put(resourceFile.getName(), new
>> LabelFile(resourceFile, "shark"));
>> +        }
>> +    }
>> +
>> +    public void findMatchingLabels(String component, String fileName,
>> String key, String locale) throws MalformedURLException,
>> SAXException, ParserConfigurationException, IOException,
>> EncodingException, GeneralException {
>> +        if (UtilValidate.isEmpty(component) &&
>> UtilValidate.isEmpty(fileName) && UtilValidate.isEmpty(key) &&
>> UtilValidate.isEmpty(locale)) {
>> +            // Important! Don't allow unparameterized queries - doing
>> so will result in loading the entire project into memory
>> +            return;
>> +        }
>> +        for (LabelFile fileInfo : filesFound.values()) {
>> +            if (UtilValidate.isNotEmpty(component) &&
>> !component.equals(fileInfo.componentName)) {
>> +                continue;
>> +            }
>> +            if (UtilValidate.isNotEmpty(fileName) &&
>> !fileName.equals(fileInfo.getFileName())) {
>> +                continue;
>> +            }
>> +            Document resourceDocument =
>> UtilXml.readXmlDocument(fileInfo.file.toURL(), false);
>> +            Element resourceElem =
>> resourceDocument.getDocumentElement();
>> +            String labelKeyComment = "";
>> +            for (Node propertyNode :
>> UtilXml.childNodeList(resourceElem.getFirstChild())) {
>> +                if (propertyNode instanceof Element) {
>> +                    Element propertyElem = (Element) propertyNode;
>> +                    String labelKey =
>> StringUtil.defaultWebEncoder.canonicalize(propertyElem.getAttribute("key"));
>>
>> +                    String labelComment = "";
>> +                    for (Node valueNode :
>> UtilXml.childNodeList(propertyElem.getFirstChild())) {
>> +                        if (valueNode instanceof Element) {
>> +                            Element valueElem = (Element) valueNode;
>> +                            String localeName =
>> valueElem.getAttribute("xml:lang");
>> +                            String labelValue =
>> StringUtil.defaultWebEncoder.canonicalize(UtilXml.nodeValue(valueElem.getFirstChild()));
>>
>> +                            LabelInfo label = labels.get(labelKey +
>> keySeparator + fileInfo.getFileName());
>> +
>> +                            if (UtilValidate.isEmpty(label)) {
>> +                                label = new LabelInfo(labelKey,
>> labelKeyComment, fileInfo.getFileName(), localeName, labelValue,
>> labelComment);
>> +                                labels.put(labelKey + keySeparator +
>> fileInfo.getFileName(), label);
>> +                            } else {
>> +                                if (label.setLabelValue(localeName,
>> labelValue, labelComment, false)) {
>> +                                    
>> duplicatedLocalesLabelsList.add(label);
>>                                 }
>> -                                labelKeyComment = "";
>> -                            } else if (propertyNode instanceof
>> Comment) {
>> -                                labelKeyComment = labelKeyComment +
>> StringUtil.defaultWebEncoder.canonicalize(propertyNode.getNodeValue());
>>                             }
>> +                            localesFound.add(localeName);
>> +                            labelComment = "";
>> +                        } else if (valueNode instanceof Comment) {
>> +                            labelComment = labelComment +
>> StringUtil.defaultWebEncoder.canonicalize(valueNode.getNodeValue());
>>                         }
>>                     }
>> -                    if (!sharkComponent) {
>> -                        componentName = "shark";
>> -                        resourceFiles =
>> FileUtil.findXmlFiles(System.getProperty("ofbiz.home") +
>> "/specialpurpose/shark", null,
>> "resource", null);
>> -                        sharkComponent = true;
>> -                    } else {
>> -                        cycle = false;
>> -                    }
>> +                    labelKeyComment = "";
>> +                } else if (propertyNode instanceof Comment) {
>> +                    labelKeyComment = labelKeyComment +
>> StringUtil.defaultWebEncoder.canonicalize(propertyNode.getNodeValue());
>>                 }
>>             }
>> -
>> -            // get labels references from sources
>> -            references = LabelReferences.getLabelReferences();
>> -        } catch (IOException ioe) {
>> -            throw new GeneralException(ioe.getMessage());
>> -        } catch (Exception e) {
>> -            throw new GeneralException(e.getMessage());
>>         }
>> -        duplicatedLocalesLabels = duplicatedLocales;
>> -    }
>> -
>> -    public static GenericDelegator getDelegator() {
>> -        return delegator;
>>     }
>>
>> -    public static ModelReader getModelReader() {
>> -        return entityModelReader;
>> +    public LabelFile getLabelFile(String fileName) {
>> +        return filesFound.get(fileName);
>>     }
>>
>> -    public static DispatchContext getDispatchContext() {
>> -        return dispatchContext;
>> -    }
>> -
>> -    public static Map<String, LabelInfo> getLabels() {
>> +    public Map<String, LabelInfo> getLabels() {
>>         return labels;
>>     }
>>
>> -    public static Set<String> getLocalesFound() {
>> +    public Set<String> getLocalesFound() {
>>         return localesFound;
>>     }
>>
>> -    public static Map<String, String> getFileNamesFound() {
>> -        return fileNamesFound;
>> -    }
>> -
>> -    public static String getFileComponent(String fileName) {
>> -        String componentName = null;
>> -        if (UtilValidate.isNotEmpty(fileName)) {
>> -            componentName = fileComponent.get(fileName);
>> -        }
>> -        return componentName;
>> +    public static Collection<LabelFile> getFilesFound() {
>> +        return filesFound.values();
>>     }
>>
>>     public static Set<String> getComponentNamesFound() {
>>         return componentNamesFound;
>>     }
>>
>> -    public static Map<String, Map<String, Integer>> getReferences() {
>> -        return references;
>> -    }
>> -
>> -    public static Set<String> getLabelsList() {
>> +    public Set<String> getLabelsList() {
>>         return labels.keySet();
>>     }
>>
>> -    public static Set<String> getReferencesList() {
>> -        return references.keySet();
>> +    public int getDuplicatedLocalesLabels() {
>> +        return duplicatedLocalesLabelsList.size();
>>     }
>>
>> -    public static int getLabelReferenceFile(String key) {
>> -        int refFile = 0;
>> -        boolean keyFound = false;
>> -
>> -        if (key == null) {
>> -            key = "";
>> -        }
>> -
>> -        for (Map.Entry<String, String> e : fileNamesFound.entrySet()) {
>> -            String keyToSearch = key + keySeparator + e.getKey();
>> -
>> -            if (labels.containsKey(keyToSearch)) {
>> -                keyFound = true;
>> -                break;
>> -            }
>> -        }
>> -
>> -        if (!keyFound) {
>> -            Map<String, Integer> reference = references.get(key);
>> -
>> -            if (UtilValidate.isNotEmpty(reference)) {
>> -                refFile = reference.size();
>> -            }
>> -        }
>> -
>> -        return refFile;
>> -    }
>> -
>> -    public static int getDuplicatedLocalesLabels() {
>> -        return duplicatedLocalesLabels;
>> -    }
>> -
>> -    public static List<LabelInfo> getDuplicatedLocalesLabelsList() {
>> +    public List<LabelInfo> getDuplicatedLocalesLabelsList() {
>>         return duplicatedLocalesLabelsList;
>>     }
>>
>> -    public static Map<String, Object> updateLabelKey(DispatchContext
>> dctx, Map<String, ? extends Object> context) {
>> -        String key = (String) context.get("key");
>> -        String keyComment = (String) context.get("keyComment");
>> -        String update_label = (String) context.get("update_label");
>> -        String fileName = (String) context.get("fileName");
>> -        String confirm = (String) context.get("confirm");
>> -        String removeLabel = (String) context.get("removeLabel");
>> -        List<String> localeNames =
>> UtilGenerics.cast(context.get("localeNames"));
>> -        List<String> localeValues =
>> UtilGenerics.cast(context.get("localeValues"));
>> -        List<String> localeComments =
>> UtilGenerics.cast(context.get("localeComments"));
>> -        Locale locale = (Locale) context.get("locale");
>> -
>> -        // Remove a Label
>> -        if (UtilValidate.isNotEmpty(removeLabel)) {
>> -            labels.remove(key + keySeparator + fileName);
>> -        } else if (UtilValidate.isNotEmpty(confirm)) {
>> -            LabelInfo label = labels.get(key + keySeparator + fileName);
>> -
>> -            // Update a Label
>> -            if (update_label.equalsIgnoreCase("Y")) {
>> -                if (UtilValidate.isNotEmpty(label)) {
>> -                    updateLabelValue(localeNames, localeValues,
>> localeComments, label, key, keyComment, fileName);
>> -                }
>> -                // Insert a new Label
>> -            } else {
>> -                if (UtilValidate.isNotEmpty(label)) {
>> -                    return
>> ServiceUtil.returnError(UtilProperties.getMessage(resource,
>> "WebtoolsLabelManagerNewLabelExisting",
>> UtilMisc.toMap("key", key, "fileName", fileName), locale));
>> -                } else {
>> -                    if (UtilValidate.isEmpty(key)) {
>> -                        return
>> ServiceUtil.returnError(UtilProperties.getMessage(resource,
>> "WebtoolsLabelManagerNewLabelEmptyKey", locale));
>> -                    } else {
>> -                        int notEmptyLabels =
>> updateLabelValue(localeNames, localeValues, localeComments, null, key,
>> keyComment,
>> fileName);
>> -                        if (notEmptyLabels == 0) {
>> -                            return
>> ServiceUtil.returnError(UtilProperties.getMessage(resource,
>> "WebtoolsLabelManagerNewLabelEmpty", locale));
>> -                        }
>> -                    }
>> -                }
>> -            }
>> -        }
>> -
>> -        return ServiceUtil.returnSuccess();
>> -    }
>> -
>> -    public static Map<String, Object>
>> updateAndSaveLabelKey(DispatchContext dctx, Map<String, ? extends
>> Object> context) {
>> -        String key = (String) context.get("key");
>> -        String keyComment = (String) context.get("keyComment");
>> -        String update_label = (String) context.get("update_label");
>> -        String fileName = (String) context.get("fileName");
>> -        String confirm = (String) context.get("confirm");
>> -        String removeLabel = (String) context.get("removeLabel");
>> -        List<String> localeNames =
>> UtilGenerics.cast(context.get("localeNames"));
>> -        List<String> localeValues =
>> UtilGenerics.cast(context.get("localeValues"));
>> -        List<String> localeComments =
>> UtilGenerics.cast(context.get("localeComments"));
>> -        GenericValue userLogin = (GenericValue)
>> context.get("userLogin");
>> -        LocalDispatcher dispatcher = dctx.getDispatcher();
>> -
>> -        Map<String, Object> contextInput = UtilMisc.toMap("key", key,
>> "keyComment", keyComment, "update_label", update_label,
>> "fileName", fileName, "confirm", confirm, "removeLabel", removeLabel,
>> -                "localeNames", localeNames, "localeValues",
>> localeValues, "localeComments", localeComments, "userLogin",
>> userLogin);
>> -        try {
>> -            Map<String, Object> updatedKey =
>> dispatcher.runSync("updateLabelKey", contextInput);
>> -
>> -            if (ServiceUtil.isError(updatedKey)) {
>> -                return updatedKey;
>> -            } else {
>> -                return dispatcher.runSync("saveLabelsToXmlFile",
>> UtilMisc.toMap("labelFileName", fileName, "userLogin",
>> userLogin));
>> -            }
>> -        } catch (GenericServiceException e) {
>> -            return ServiceUtil.returnError("error on saving label key
>> :" + key);
>> -        }
>> -    }
>> -
>> -    private static int updateLabelValue(List<String> localeNames,
>> List<String> localeValues, List<String> localeComments,
>> LabelInfo label, String key, String keyComment, String fileName) {
>> +    public int updateLabelValue(List<String> localeNames,
>> List<String> localeValues, List<String> localeComments, LabelInfo
>> label, String key, String keyComment, String fileName) {
>>         int notEmptyLabels = 0;
>> -        int i = 0;
>> -        while (i < localeNames.size()) {
>> +        for (int i = 0; i < localeNames.size(); i++) {
>>             String localeName = localeNames.get(i);
>>             String localeValue = localeValues.get(i);
>>             String localeComment = localeComments.get(i);
>> -
>>             if (UtilValidate.isNotEmpty(localeValue) ||
>> UtilValidate.isNotEmpty(localeComment)) {
>>                 if (label == null) {
>>                     try {
>> -                        String componentName =
>> getFileComponent(fileName);
>> -                        label = new LabelInfo(key, keyComment,
>> fileName, componentName, localeName, localeValue, localeComment);
>> +                        label = new LabelInfo(key, keyComment,
>> fileName, localeName, localeValue, localeComment);
>>                         labels.put(key + keySeparator + fileName, label);
>>                     } catch (Exception e) {
>>                         e.printStackTrace();
>> @@ -355,9 +209,7 @@
>>                 label.setLabelValue(localeName, localeValue,
>> localeComment, true);
>>                 notEmptyLabels++;
>>             }
>> -            i++;
>>         }
>> -
>>         return notEmptyLabels;
>>     }
>> }
>>
>>
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: svn commit: r776988 [1/3] - in /ofbiz/trunk/framework/webtools: config/ servicedef/ src/org/ofbiz/webtools/labelmanager/ webapp/webtools/WEB-INF/ webapp/webtools/WEB-INF/actions/labelmanager/ webapp/webtools/labelmanager/ widget/

Jacques Le Roux
Administrator
I see now. Sorry to say I tried changing only the component (to content), clicked Find and got

Expression label.componentName is undefined on line 86, column 139 in
component://webtools/webapp/webtools/labelmanager/ViewLabels.ftl. The problematic instruction: ---------- ==> if showLabel &&
parameters.labelComponentName?exists && parameters.labelComponentName != "" && parameters.labelComponentName != label.componentName
[on line 86, column 9 in component://webtools/webapp/webtools/labelmanager/ViewLabels.ftl]

Also how to do if you want all Missing Translations for a sole language ? I succeded one time (I selected a component 1st, then a
file. Then got this error above and was finally able to choose a language and got results) but was not able to do it again (got
always the error above, gave up)

I'm not in a hurry with that, it was only a test ;o)

Thanks

Jacques


From: "Adrian Crum" <[hidden email]>

> Btw, it wasn't my intention to rewrite so much of it. I started out just trying to fix a file naming problem that Adam pointed
> out, which led me to another problem, which led me to another problem...
>
> -Adrian
>
> Jacques Le Roux wrote:
>> Hi Adrian,
>>
>> Marco wrote this code and I commited it. Actually I must say I did no review all details as you did. It was working, I needed it,
>> so I though it could be useful for other persons. Actually it did not even occur to me that it could be used by several persons
>> concurrently, which is of course a big miss :/
>>
>> Actually as I said it was working but I'm sorry to say that, apart if I miss something, it does not work any longer...
>> For instance, if you search with all default options, nothing happens. And you can't anymore select a language.
>>
>> Thanks
>>
>> Jacques
>>
>> From: <[hidden email]>
>>> Author: adrianc
>>> Date: Thu May 21 07:26:22 2009
>>> New Revision: 776988
>>>
>>> URL: http://svn.apache.org/viewvc?rev=776988&view=rev
>>> Log:
>>> Refactored the Webtools Label Manager - due to a multitude of problems. Most notably:
>>>
>>> 1. Not thread safe.
>>> 2. Server files were being read/written without multi-user synchronization
>>> 3. Reads all UI labels into memory, then stores them in static memory, PLUS
>>> 4. Finds all references of those labels in project files and store those references in static memory (!!!)
>>> 5. Doesn't follow best practices for UI layout
>>>
>>> It's amazing the original code worked. Even more amazing is the fact that this code made it into the project.
>>>
>>> Added:
>>>    ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelFile.java   (with props)
>>>    ofbiz/trunk/framework/webtools/webapp/webtools/WEB-INF/actions/labelmanager/ViewReferences.groovy   (with props)
>>> Modified:
>>>    ofbiz/trunk/framework/webtools/config/WebtoolsUiLabels.xml
>>>    ofbiz/trunk/framework/webtools/servicedef/services.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/LabelReferences.java
>>>    ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/SaveLabelsToXmlFile.java
>>>    ofbiz/trunk/framework/webtools/webapp/webtools/WEB-INF/actions/labelmanager/LabelManager.groovy
>>>    ofbiz/trunk/framework/webtools/webapp/webtools/WEB-INF/actions/labelmanager/UpdateManager.groovy
>>>    ofbiz/trunk/framework/webtools/webapp/webtools/WEB-INF/actions/labelmanager/ViewFile.groovy
>>>    ofbiz/trunk/framework/webtools/webapp/webtools/WEB-INF/controller.xml
>>>    ofbiz/trunk/framework/webtools/webapp/webtools/labelmanager/SearchLabels.ftl
>>>    ofbiz/trunk/framework/webtools/webapp/webtools/labelmanager/UpdateLabel.ftl
>>>    ofbiz/trunk/framework/webtools/webapp/webtools/labelmanager/ViewFile.ftl
>>>    ofbiz/trunk/framework/webtools/webapp/webtools/labelmanager/ViewHardcodedLabels.ftl
>>>    ofbiz/trunk/framework/webtools/webapp/webtools/labelmanager/ViewLabels.ftl
>>>    ofbiz/trunk/framework/webtools/webapp/webtools/labelmanager/ViewReferences.ftl
>>>    ofbiz/trunk/framework/webtools/widget/LabelManagerScreens.xml
>>>
>>> Modified: ofbiz/trunk/framework/webtools/config/WebtoolsUiLabels.xml
>>> URL:
>>> http://svn.apache.org/viewvc/ofbiz/trunk/framework/webtools/config/WebtoolsUiLabels.xml?rev=776988&r1=776987&r2=776988&view=diff
>>> ==============================================================================
>>> --- ofbiz/trunk/framework/webtools/config/WebtoolsUiLabels.xml (original)
>>> +++ ofbiz/trunk/framework/webtools/config/WebtoolsUiLabels.xml Thu May 21 07:26:22 2009
>>> @@ -1512,6 +1512,11 @@
>>>         <value xml:lang="fr">Nom de fichier</value>
>>>         <value xml:lang="it">Nome File</value>
>>>     </property>
>>> +    <property key="WebtoolsLabelManagerFindLabels">
>>> +        <value xml:lang="en">Find Labels</value>
>>> +        <value xml:lang="fr">Gestionnaire de traduction</value>
>>> +        <value xml:lang="it">Gestione delle label</value>
>>> +    </property>
>>>     <property key="WebtoolsLabelManagerHardcoded">
>>>         <value xml:lang="en">Hardcoded labels</value>
>>>         <value xml:lang="it">Label non traducibili</value>
>>>
>>> Modified: ofbiz/trunk/framework/webtools/servicedef/services.xml
>>> URL:
>>> http://svn.apache.org/viewvc/ofbiz/trunk/framework/webtools/servicedef/services.xml?rev=776988&r1=776987&r2=776988&view=diff
>>> ==============================================================================
>>> --- ofbiz/trunk/framework/webtools/servicedef/services.xml (original)
>>> +++ ofbiz/trunk/framework/webtools/servicedef/services.xml Thu May 21 07:26:22 2009
>>> @@ -123,27 +123,10 @@
>>>         <attribute name="eomodeldFullPath" type="java.lang.String" mode="IN" optional="false"/>
>>>         <attribute name="serviceName" type="java.lang.String" mode="IN" optional="false"/>
>>>     </service>
>>> +
>>>     <service name="saveLabelsToXmlFile" engine="java" location="org.ofbiz.webtools.labelmanager.SaveLabelsToXmlFile"
>>>              invoke="saveLabelsToXmlFile" auth="true" use-transaction="false">
>>>         <description>Save labels to xml file</description>
>>> -        <attribute name="labelFileName" type="String" mode="IN" optional="true"/>
>>> -    </service>
>>> -    <service name="updateLabelKey" engine="java" location="org.ofbiz.webtools.labelmanager.LabelManagerFactory"
>>> -             invoke="updateLabelKey" auth="true" use-transaction="false">
>>> -        <description>Update Label Key</description>
>>> -        <attribute name="key" type="String" mode="IN" optional="true"/>
>>> -        <attribute name="keyComment" type="String" mode="IN" optional="true"/>
>>> -        <attribute name="update_label" type="String" mode="IN" optional="false"/>
>>> -        <attribute name="fileName" type="String" mode="IN" optional="false"/>
>>> -        <attribute name="confirm" type="String" mode="IN" optional="true"/>
>>> -        <attribute name="removeLabel" type="String" mode="IN" optional="true"/>
>>> -        <attribute name="localeNames" type="List" mode="IN" optional="true"/>
>>> -        <attribute name="localeValues" type="List" mode="IN" optional="true"/>
>>> -        <attribute name="localeComments" type="List" mode="IN" optional="true"/>
>>> -    </service>
>>> -    <service name="updateAndSaveLabelKey" engine="java" location="org.ofbiz.webtools.labelmanager.LabelManagerFactory"
>>> -             invoke="updateAndSaveLabelKey" auth="true" use-transaction="false">
>>> -        <description>Update Label Key</description>
>>>         <attribute name="key" type="String" mode="IN" optional="true"/>
>>>         <attribute name="keyComment" type="String" mode="IN" optional="true"/>
>>>         <attribute name="update_label" type="String" mode="IN" optional="false"/>
>>>
>>> Added: ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelFile.java
>>> URL:
>>> http://svn.apache.org/viewvc/ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelFile.java?rev=776988&view=auto
>>> ==============================================================================
>>> ---
>>> ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelFile.java (added)
>>> +++ ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelFile.java Thu May 21 07:26:22 2009
>>> @@ -0,0 +1,46 @@
>>> +/*
>>> + * 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.webtools.labelmanager;
>>> +
>>> +import java.io.File;
>>> +
>>> +public class LabelFile {
>>> +    public static final String module = LabelFile.class.getName();
>>> +
>>> +    protected final boolean fileLoaded = false;;
>>> +    protected final File file;
>>> +    protected final String componentName;
>>> +
>>> +    protected LabelFile(File file, String componentName) {
>>> +        this.file = file;
>>> +        this.componentName = componentName;
>>> +    }
>>> +
>>> +    public File getFile() {
>>> +        return this.file;
>>> +    }
>>> +
>>> +    public String getFileName() {
>>> +        return this.file.getName();
>>> +    }
>>> +
>>> +    public String getFilePath() {
>>> +        return this.file.getPath();
>>> +    }
>>> +}
>>>
>>> Propchange: ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelFile.java
>>> ------------------------------------------------------------------------------
>>>
>>>    svn:eol-style = native
>>>
>>> Propchange: ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelFile.java
>>> ------------------------------------------------------------------------------
>>>
>>>    svn:keywords = "Date Rev Author URL Id"
>>>
>>> Propchange: ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelFile.java
>>> ------------------------------------------------------------------------------
>>>
>>>    svn:mime-type = text/plain
>>>
>>> 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=776988&r1=776987&r2=776988&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 Thu May 21 07:26:22 2009
>>> @@ -33,14 +33,12 @@
>>>     protected String labelKey = "";
>>>     protected String labelKeyComment = "";
>>>     protected String fileName = "";
>>> -    protected String componentName = "";
>>>     protected Map<String, LabelValue> labelValues = FastMap.newInstance();
>>>
>>> -    public LabelInfo(String labelKey, String labelKeyComment, String fileName, String componentName, String localeStr, String
>>> labelValue, String labelComment) throws GeneralException {
>>> +    public LabelInfo(String labelKey, String labelKeyComment, String fileName, String localeStr, String labelValue, String
>>> labelComment) throws GeneralException {
>>>         this.labelKey = labelKey;
>>>         this.labelKeyComment = labelKeyComment;
>>>         this.fileName = fileName;
>>> -        this.componentName = componentName;
>>>         setLabelValue(localeStr, labelValue, labelComment, false);
>>>     }
>>>
>>> @@ -60,10 +58,6 @@
>>>         return fileName;
>>>     }
>>>
>>> -    public String getComponentName() {
>>> -        return componentName;
>>> -    }
>>> -
>>>     public LabelValue getLabelValue(String localeStr) {
>>>        return (LabelValue)labelValues.get(localeStr);
>>>     }
>>>
>>> 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=776988&r1=776987&r2=776988&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 Thu May 21 07:26:22 2009
>>> @@ -20,331 +20,185 @@
>>>
>>> import java.io.File;
>>> import java.io.IOException;
>>> +import java.net.MalformedURLException;
>>> import java.util.Collection;
>>> import java.util.List;
>>> -import java.util.Locale;
>>> import java.util.Map;
>>> import java.util.Set;
>>> import java.util.TreeMap;
>>> import java.util.TreeSet;
>>>
>>> +import javax.xml.parsers.ParserConfigurationException;
>>> +
>>> import javolution.util.FastList;
>>>
>>> import org.ofbiz.base.component.ComponentConfig;
>>> +import org.ofbiz.base.component.ComponentConfig.ClasspathInfo;
>>> +import org.ofbiz.base.util.Debug;
>>> import org.ofbiz.base.util.FileUtil;
>>> import org.ofbiz.base.util.GeneralException;
>>> import org.ofbiz.base.util.StringUtil;
>>> -import org.ofbiz.base.util.UtilGenerics;
>>> -import org.ofbiz.base.util.UtilMisc;
>>> -import org.ofbiz.base.util.UtilProperties;
>>> import org.ofbiz.base.util.UtilValidate;
>>> import org.ofbiz.base.util.UtilXml;
>>> -import org.ofbiz.base.util.cache.UtilCache;
>>> -import org.ofbiz.entity.GenericDelegator;
>>> -import org.ofbiz.entity.GenericValue;
>>> -import org.ofbiz.entity.model.ModelReader;
>>> -import org.ofbiz.service.DispatchContext;
>>> -import org.ofbiz.service.GenericServiceException;
>>> -import org.ofbiz.service.LocalDispatcher;
>>> -import org.ofbiz.service.ServiceUtil;
>>> +import org.owasp.esapi.errors.EncodingException;
>>> import org.w3c.dom.Comment;
>>> import org.w3c.dom.Document;
>>> import org.w3c.dom.Element;
>>> import org.w3c.dom.Node;
>>> +import org.xml.sax.SAXException;
>>>
>>> public class LabelManagerFactory {
>>>
>>>     public static final String module = LabelManagerFactory.class.getName();
>>>     public static final String resource = "WebtoolsUiLabels";
>>> -
>>>     public static final String keySeparator = "#";
>>>
>>> -    protected static UtilCache<String, LabelManagerFactory> labelManagerFactoryCache = new UtilCache<String,
>>> LabelManagerFactory>("LabelManagerFactory");
>>> -
>>> -    protected static Map<String, LabelInfo> labels = null;
>>> -    protected static Map<String, String> fileNamesFound = null;
>>> -    protected static Map<String, String> fileComponent = null;
>>> -    protected static Set<String> localesFound = null;
>>>     protected static Set<String> componentNamesFound = null;
>>> -    protected static Map<String, Map<String, Integer>> references = null;
>>> -    protected static List<LabelInfo> duplicatedLocalesLabelsList = null;
>>> -    protected static int duplicatedLocalesLabels = 0;
>>> -
>>> -    protected static GenericDelegator delegator;
>>> -    protected static ModelReader entityModelReader;
>>> -    protected static DispatchContext dispatchContext;
>>> -
>>> -    public static LabelManagerFactory getLabelManagerFactory(String delegatorName) throws GeneralException {
>>> -        if (UtilValidate.isEmpty(delegatorName)) {
>>> -            delegatorName = "default";
>>> -        }
>>> +    protected static Map<String, LabelFile> filesFound = null;
>>>
>>> -        LabelManagerFactory lmf = labelManagerFactoryCache.get(delegatorName);
>>> +    protected Map<String, LabelInfo> labels = new TreeMap<String, LabelInfo>();
>>> +    protected Set<String> localesFound = new TreeSet<String>();
>>> +    protected List<LabelInfo> duplicatedLocalesLabelsList = FastList.newInstance();
>>>
>>> -        if (lmf == null) {
>>> -            lmf = new LabelManagerFactory(delegatorName);
>>> -            labelManagerFactoryCache.put(delegatorName, lmf);
>>> +    public static synchronized LabelManagerFactory getInstance() throws GeneralException, IOException {
>>> +        if (componentNamesFound == null) {
>>> +            loadComponentNames();
>>>         }
>>> -        return lmf;
>>> +        if (filesFound == null) {
>>> +            loadLabelFiles();
>>> +        }
>>> +        return new LabelManagerFactory();
>>>     }
>>>
>>> -    protected LabelManagerFactory(String delegatorName) throws GeneralException {
>>> -        delegator = GenericDelegator.getGenericDelegator(delegatorName);
>>> -        entityModelReader = ModelReader.getModelReader(delegatorName);
>>> -        dispatchContext = new DispatchContext("LabelManagerFactoryDispCtx", null, this.getClass().getClassLoader(), null);
>>> -        prepareAll();
>>> +    protected LabelManagerFactory() {
>>>     }
>>>
>>> -    private static void prepareAll() throws GeneralException {
>>> -        labels = new TreeMap<String, LabelInfo>();
>>> -        fileNamesFound = new TreeMap<String, String>();
>>> -        fileComponent = new TreeMap<String, String>();
>>> -        localesFound = new TreeSet<String>();
>>> +    protected static void loadComponentNames() {
>>>         componentNamesFound = new TreeSet<String>();
>>> -        duplicatedLocalesLabelsList = FastList.newInstance();
>>> -        references = null;
>>> -        int duplicatedLocales = 0;
>>> -
>>> -        try {
>>> -            boolean sharkComponent = false;
>>> -            Collection<ComponentConfig> componentConfigs = ComponentConfig.getAllComponents();
>>> -
>>> -            for (ComponentConfig componentConfig : componentConfigs) {
>>> -                String componentName = componentConfig.getComponentName();
>>> -                List<File> resourceFiles = FileUtil.findXmlFiles(componentConfig.getRootLocation(), null, "resource", null);
>>> -                boolean cycle = true;
>>> -                while (cycle) {
>>> -                    for (File resourceFile : resourceFiles) {
>>> -                        String fileName = resourceFile.getName();
>>> -                        Document resourceDocument = UtilXml.readXmlDocument(resourceFile.toURI().toURL());
>>> -                        Element resourceElem = resourceDocument.getDocumentElement();
>>> -                        String labelKeyComment = "";
>>> -
>>> -                        for (Node propertyNode : UtilXml.childNodeList(resourceElem.getFirstChild())) {
>>> -                            if (propertyNode instanceof Element) {
>>> -                                Element propertyElem = (Element) propertyNode;
>>> -                                String labelKey = StringUtil.defaultWebEncoder.canonicalize(propertyElem.getAttribute("key"));
>>> -                                String labelComment = "";
>>> -
>>> -                                for (Node valueNode : UtilXml.childNodeList(propertyElem.getFirstChild())) {
>>> -                                    if (valueNode instanceof Element) {
>>> -                                        Element valueElem = (Element) valueNode;
>>> -                                        String localeName = valueElem.getAttribute("xml:lang");
>>> -                                        String labelValue =
>>> StringUtil.defaultWebEncoder.canonicalize(UtilXml.nodeValue(valueElem.getFirstChild()));
>>> -                                        LabelInfo label = labels.get(labelKey + keySeparator + fileName);
>>> -
>>> -                                        if (UtilValidate.isEmpty(label)) {
>>> -                                            label = new LabelInfo(labelKey, labelKeyComment, fileName, componentName,
>>> localeName,
>>> labelValue, labelComment);
>>> -                                            labels.put(labelKey + keySeparator + fileName, label);
>>> -                                        } else {
>>> -                                            if (label.setLabelValue(localeName, labelValue, labelComment, false)) {
>>> -                                                duplicatedLocalesLabelsList.add(label);
>>> -                                                duplicatedLocales++;
>>> -                                            }
>>> -                                        }
>>> -                                        localesFound.add(localeName);
>>> -                                        componentNamesFound.add(componentName);
>>> -                                        fileNamesFound.put(fileName, resourceFile.toURI().toString());
>>> -                                        fileComponent.put(fileName, componentName);
>>> -                                        labelComment = "";
>>> -                                    } else if (valueNode instanceof Comment) {
>>> -                                        labelComment = labelComment +
>>> StringUtil.defaultWebEncoder.canonicalize(valueNode.getNodeValue());
>>> -                                    }
>>> +        Collection<ComponentConfig> componentConfigs = ComponentConfig.getAllComponents();
>>> +        for (ComponentConfig componentConfig : componentConfigs) {
>>> +            componentNamesFound.add(componentConfig.getComponentName());
>>> +        }
>>> +        componentNamesFound.add("shark");
>>> +    }
>>> +
>>> +    protected static void loadLabelFiles() throws IOException {
>>> +        filesFound = new TreeMap<String, LabelFile>();
>>> +        List<ClasspathInfo> cpInfos = ComponentConfig.getAllClasspathInfos();
>>> +        for (ClasspathInfo cpi : cpInfos) {
>>> +            if ("dir".equals(cpi.type)) {
>>> +                String configRoot = cpi.componentConfig.getRootLocation();
>>> +                configRoot = configRoot.replace('\\', '/');
>>> +                if (!configRoot.endsWith("/")) {
>>> +                    configRoot = configRoot + "/";
>>> +                }
>>> +                String location = cpi.location.replace('\\', '/');
>>> +                if (location.startsWith("/")) {
>>> +                    location = location.substring(1);
>>> +                }
>>> +                List<File> resourceFiles = FileUtil.findXmlFiles(configRoot + location, null, "resource", null);
>>> +                for (File resourceFile : resourceFiles) {
>>> +                    filesFound.put(resourceFile.getName(), new LabelFile(resourceFile,
>>> cpi.componentConfig.getComponentName()));
>>> +                }
>>> +            }
>>> +        }
>>> +        List<File> resourceFiles = FileUtil.findXmlFiles(System.getProperty("ofbiz.home") + "/specialpurpose/shark/config",
>>> null,
>>> "resource", null);
>>> +        for (File resourceFile : resourceFiles) {
>>> +            filesFound.put(resourceFile.getName(), new LabelFile(resourceFile, "shark"));
>>> +        }
>>> +    }
>>> +
>>> +    public void findMatchingLabels(String component, String fileName, String key, String locale) throws MalformedURLException,
>>> SAXException, ParserConfigurationException, IOException, EncodingException, GeneralException {
>>> +        if (UtilValidate.isEmpty(component) && UtilValidate.isEmpty(fileName) && UtilValidate.isEmpty(key) &&
>>> UtilValidate.isEmpty(locale)) {
>>> +            // Important! Don't allow unparameterized queries - doing so will result in loading the entire project into memory
>>> +            return;
>>> +        }
>>> +        for (LabelFile fileInfo : filesFound.values()) {
>>> +            if (UtilValidate.isNotEmpty(component) && !component.equals(fileInfo.componentName)) {
>>> +                continue;
>>> +            }
>>> +            if (UtilValidate.isNotEmpty(fileName) && !fileName.equals(fileInfo.getFileName())) {
>>> +                continue;
>>> +            }
>>> +            Document resourceDocument = UtilXml.readXmlDocument(fileInfo.file.toURL(), false);
>>> +            Element resourceElem = resourceDocument.getDocumentElement();
>>> +            String labelKeyComment = "";
>>> +            for (Node propertyNode : UtilXml.childNodeList(resourceElem.getFirstChild())) {
>>> +                if (propertyNode instanceof Element) {
>>> +                    Element propertyElem = (Element) propertyNode;
>>> +                    String labelKey = StringUtil.defaultWebEncoder.canonicalize(propertyElem.getAttribute("key"));
>>> +                    String labelComment = "";
>>> +                    for (Node valueNode : UtilXml.childNodeList(propertyElem.getFirstChild())) {
>>> +                        if (valueNode instanceof Element) {
>>> +                            Element valueElem = (Element) valueNode;
>>> +                            String localeName = valueElem.getAttribute("xml:lang");
>>> +                            String labelValue =
>>> StringUtil.defaultWebEncoder.canonicalize(UtilXml.nodeValue(valueElem.getFirstChild()));
>>> +                            LabelInfo label = labels.get(labelKey + keySeparator + fileInfo.getFileName());
>>> +
>>> +                            if (UtilValidate.isEmpty(label)) {
>>> +                                label = new LabelInfo(labelKey, labelKeyComment, fileInfo.getFileName(), localeName,
>>> labelValue,
>>> labelComment);
>>> +                                labels.put(labelKey + keySeparator + fileInfo.getFileName(), label);
>>> +                            } else {
>>> +                                if (label.setLabelValue(localeName, labelValue, labelComment, false)) {
>>> +                                    duplicatedLocalesLabelsList.add(label);
>>>                                 }
>>> -                                labelKeyComment = "";
>>> -                            } else if (propertyNode instanceof Comment) {
>>> -                                labelKeyComment = labelKeyComment +
>>> StringUtil.defaultWebEncoder.canonicalize(propertyNode.getNodeValue());
>>>                             }
>>> +                            localesFound.add(localeName);
>>> +                            labelComment = "";
>>> +                        } else if (valueNode instanceof Comment) {
>>> +                            labelComment = labelComment + StringUtil.defaultWebEncoder.canonicalize(valueNode.getNodeValue());
>>>                         }
>>>                     }
>>> -                    if (!sharkComponent) {
>>> -                        componentName = "shark";
>>> -                        resourceFiles = FileUtil.findXmlFiles(System.getProperty("ofbiz.home") + "/specialpurpose/shark", null,
>>> "resource", null);
>>> -                        sharkComponent = true;
>>> -                    } else {
>>> -                        cycle = false;
>>> -                    }
>>> +                    labelKeyComment = "";
>>> +                } else if (propertyNode instanceof Comment) {
>>> +                    labelKeyComment = labelKeyComment + StringUtil.defaultWebEncoder.canonicalize(propertyNode.getNodeValue());
>>>                 }
>>>             }
>>> -
>>> -            // get labels references from sources
>>> -            references = LabelReferences.getLabelReferences();
>>> -        } catch (IOException ioe) {
>>> -            throw new GeneralException(ioe.getMessage());
>>> -        } catch (Exception e) {
>>> -            throw new GeneralException(e.getMessage());
>>>         }
>>> -        duplicatedLocalesLabels = duplicatedLocales;
>>> -    }
>>> -
>>> -    public static GenericDelegator getDelegator() {
>>> -        return delegator;
>>>     }
>>>
>>> -    public static ModelReader getModelReader() {
>>> -        return entityModelReader;
>>> +    public LabelFile getLabelFile(String fileName) {
>>> +        return filesFound.get(fileName);
>>>     }
>>>
>>> -    public static DispatchContext getDispatchContext() {
>>> -        return dispatchContext;
>>> -    }
>>> -
>>> -    public static Map<String, LabelInfo> getLabels() {
>>> +    public Map<String, LabelInfo> getLabels() {
>>>         return labels;
>>>     }
>>>
>>> -    public static Set<String> getLocalesFound() {
>>> +    public Set<String> getLocalesFound() {
>>>         return localesFound;
>>>     }
>>>
>>> -    public static Map<String, String> getFileNamesFound() {
>>> -        return fileNamesFound;
>>> -    }
>>> -
>>> -    public static String getFileComponent(String fileName) {
>>> -        String componentName = null;
>>> -        if (UtilValidate.isNotEmpty(fileName)) {
>>> -            componentName = fileComponent.get(fileName);
>>> -        }
>>> -        return componentName;
>>> +    public static Collection<LabelFile> getFilesFound() {
>>> +        return filesFound.values();
>>>     }
>>>
>>>     public static Set<String> getComponentNamesFound() {
>>>         return componentNamesFound;
>>>     }
>>>
>>> -    public static Map<String, Map<String, Integer>> getReferences() {
>>> -        return references;
>>> -    }
>>> -
>>> -    public static Set<String> getLabelsList() {
>>> +    public Set<String> getLabelsList() {
>>>         return labels.keySet();
>>>     }
>>>
>>> -    public static Set<String> getReferencesList() {
>>> -        return references.keySet();
>>> +    public int getDuplicatedLocalesLabels() {
>>> +        return duplicatedLocalesLabelsList.size();
>>>     }
>>>
>>> -    public static int getLabelReferenceFile(String key) {
>>> -        int refFile = 0;
>>> -        boolean keyFound = false;
>>> -
>>> -        if (key == null) {
>>> -            key = "";
>>> -        }
>>> -
>>> -        for (Map.Entry<String, String> e : fileNamesFound.entrySet()) {
>>> -            String keyToSearch = key + keySeparator + e.getKey();
>>> -
>>> -            if (labels.containsKey(keyToSearch)) {
>>> -                keyFound = true;
>>> -                break;
>>> -            }
>>> -        }
>>> -
>>> -        if (!keyFound) {
>>> -            Map<String, Integer> reference = references.get(key);
>>> -
>>> -            if (UtilValidate.isNotEmpty(reference)) {
>>> -                refFile = reference.size();
>>> -            }
>>> -        }
>>> -
>>> -        return refFile;
>>> -    }
>>> -
>>> -    public static int getDuplicatedLocalesLabels() {
>>> -        return duplicatedLocalesLabels;
>>> -    }
>>> -
>>> -    public static List<LabelInfo> getDuplicatedLocalesLabelsList() {
>>> +    public List<LabelInfo> getDuplicatedLocalesLabelsList() {
>>>         return duplicatedLocalesLabelsList;
>>>     }
>>>
>>> -    public static Map<String, Object> updateLabelKey(DispatchContext dctx, Map<String, ? extends Object> context) {
>>> -        String key = (String) context.get("key");
>>> -        String keyComment = (String) context.get("keyComment");
>>> -        String update_label = (String) context.get("update_label");
>>> -        String fileName = (String) context.get("fileName");
>>> -        String confirm = (String) context.get("confirm");
>>> -        String removeLabel = (String) context.get("removeLabel");
>>> -        List<String> localeNames = UtilGenerics.cast(context.get("localeNames"));
>>> -        List<String> localeValues = UtilGenerics.cast(context.get("localeValues"));
>>> -        List<String> localeComments = UtilGenerics.cast(context.get("localeComments"));
>>> -        Locale locale = (Locale) context.get("locale");
>>> -
>>> -        // Remove a Label
>>> -        if (UtilValidate.isNotEmpty(removeLabel)) {
>>> -            labels.remove(key + keySeparator + fileName);
>>> -        } else if (UtilValidate.isNotEmpty(confirm)) {
>>> -            LabelInfo label = labels.get(key + keySeparator + fileName);
>>> -
>>> -            // Update a Label
>>> -            if (update_label.equalsIgnoreCase("Y")) {
>>> -                if (UtilValidate.isNotEmpty(label)) {
>>> -                    updateLabelValue(localeNames, localeValues, localeComments, label, key, keyComment, fileName);
>>> -                }
>>> -                // Insert a new Label
>>> -            } else {
>>> -                if (UtilValidate.isNotEmpty(label)) {
>>> -                    return ServiceUtil.returnError(UtilProperties.getMessage(resource, "WebtoolsLabelManagerNewLabelExisting",
>>> UtilMisc.toMap("key", key, "fileName", fileName), locale));
>>> -                } else {
>>> -                    if (UtilValidate.isEmpty(key)) {
>>> -                        return ServiceUtil.returnError(UtilProperties.getMessage(resource,
>>> "WebtoolsLabelManagerNewLabelEmptyKey", locale));
>>> -                    } else {
>>> -                        int notEmptyLabels = updateLabelValue(localeNames, localeValues, localeComments, null, key, keyComment,
>>> fileName);
>>> -                        if (notEmptyLabels == 0) {
>>> -                            return ServiceUtil.returnError(UtilProperties.getMessage(resource,
>>> "WebtoolsLabelManagerNewLabelEmpty", locale));
>>> -                        }
>>> -                    }
>>> -                }
>>> -            }
>>> -        }
>>> -
>>> -        return ServiceUtil.returnSuccess();
>>> -    }
>>> -
>>> -    public static Map<String, Object> updateAndSaveLabelKey(DispatchContext dctx, Map<String, ? extends Object> context) {
>>> -        String key = (String) context.get("key");
>>> -        String keyComment = (String) context.get("keyComment");
>>> -        String update_label = (String) context.get("update_label");
>>> -        String fileName = (String) context.get("fileName");
>>> -        String confirm = (String) context.get("confirm");
>>> -        String removeLabel = (String) context.get("removeLabel");
>>> -        List<String> localeNames = UtilGenerics.cast(context.get("localeNames"));
>>> -        List<String> localeValues = UtilGenerics.cast(context.get("localeValues"));
>>> -        List<String> localeComments = UtilGenerics.cast(context.get("localeComments"));
>>> -        GenericValue userLogin = (GenericValue) context.get("userLogin");
>>> -        LocalDispatcher dispatcher = dctx.getDispatcher();
>>> -
>>> -        Map<String, Object> contextInput = UtilMisc.toMap("key", key, "keyComment", keyComment, "update_label", update_label,
>>> "fileName", fileName, "confirm", confirm, "removeLabel", removeLabel,
>>> -                "localeNames", localeNames, "localeValues", localeValues, "localeComments", localeComments, "userLogin",
>>> userLogin);
>>> -        try {
>>> -            Map<String, Object> updatedKey = dispatcher.runSync("updateLabelKey", contextInput);
>>> -
>>> -            if (ServiceUtil.isError(updatedKey)) {
>>> -                return updatedKey;
>>> -            } else {
>>> -                return dispatcher.runSync("saveLabelsToXmlFile", UtilMisc.toMap("labelFileName", fileName, "userLogin",
>>> userLogin));
>>> -            }
>>> -        } catch (GenericServiceException e) {
>>> -            return ServiceUtil.returnError("error on saving label key :" + key);
>>> -        }
>>> -    }
>>> -
>>> -    private static int updateLabelValue(List<String> localeNames, List<String> localeValues, List<String> localeComments,
>>> LabelInfo label, String key, String keyComment, String fileName) {
>>> +    public int updateLabelValue(List<String> localeNames, List<String> localeValues, List<String> localeComments, LabelInfo
>>> label, String key, String keyComment, String fileName) {
>>>         int notEmptyLabels = 0;
>>> -        int i = 0;
>>> -        while (i < localeNames.size()) {
>>> +        for (int i = 0; i < localeNames.size(); i++) {
>>>             String localeName = localeNames.get(i);
>>>             String localeValue = localeValues.get(i);
>>>             String localeComment = localeComments.get(i);
>>> -
>>>             if (UtilValidate.isNotEmpty(localeValue) || UtilValidate.isNotEmpty(localeComment)) {
>>>                 if (label == null) {
>>>                     try {
>>> -                        String componentName = getFileComponent(fileName);
>>> -                        label = new LabelInfo(key, keyComment, fileName, componentName, localeName, localeValue,
>>> localeComment);
>>> +                        label = new LabelInfo(key, keyComment, fileName, localeName, localeValue, localeComment);
>>>                         labels.put(key + keySeparator + fileName, label);
>>>                     } catch (Exception e) {
>>>                         e.printStackTrace();
>>> @@ -355,9 +209,7 @@
>>>                 label.setLabelValue(localeName, localeValue, localeComment, true);
>>>                 notEmptyLabels++;
>>>             }
>>> -            i++;
>>>         }
>>> -
>>>         return notEmptyLabels;
>>>     }
>>> }
>>>
>>>
>>
>>
>>
>


Reply | Threaded
Open this post in threaded view
|

Re: svn commit: r776988 [1/3] - in /ofbiz/trunk/framework/webtools: config/ servicedef/ src/org/ofbiz/webtools/labelmanager/ webapp/webtools/WEB-INF/ webapp/webtools/WEB-INF/actions/labelmanager/ webapp/webtools/labelmanager/ widget/

Adrian Crum
Fixed, rev 777587.

There are some improvements that can be made in the design. The Locale
drop-down in the search screen should include all locales (like in the
old-style masthead) - not just the ones found in the label file. Also,
it would be nice to have the ability to ADD a locale to a label file.

-Adrian

Jacques Le Roux wrote:

> I see now. Sorry to say I tried changing only the component (to
> content), clicked Find and got
>
> Expression label.componentName is undefined on line 86, column 139 in
> component://webtools/webapp/webtools/labelmanager/ViewLabels.ftl. The
> problematic instruction: ---------- ==> if showLabel &&
> parameters.labelComponentName?exists && parameters.labelComponentName !=
> "" && parameters.labelComponentName != label.componentName [on line 86,
> column 9 in
> component://webtools/webapp/webtools/labelmanager/ViewLabels.ftl]
>
> Also how to do if you want all Missing Translations for a sole language
> ? I succeded one time (I selected a component 1st, then a file. Then got
> this error above and was finally able to choose a language and got
> results) but was not able to do it again (got always the error above,
> gave up)
>
> I'm not in a hurry with that, it was only a test ;o)
>
> Thanks
>
> Jacques
>
>
> From: "Adrian Crum" <[hidden email]>
>> Btw, it wasn't my intention to rewrite so much of it. I started out
>> just trying to fix a file naming problem that Adam pointed out, which
>> led me to another problem, which led me to another problem...
>>
>> -Adrian
>>
>> Jacques Le Roux wrote:
>>> Hi Adrian,
>>>
>>> Marco wrote this code and I commited it. Actually I must say I did no
>>> review all details as you did. It was working, I needed it, so I
>>> though it could be useful for other persons. Actually it did not even
>>> occur to me that it could be used by several persons concurrently,
>>> which is of course a big miss :/
>>>
>>> Actually as I said it was working but I'm sorry to say that, apart if
>>> I miss something, it does not work any longer...
>>> For instance, if you search with all default options, nothing
>>> happens. And you can't anymore select a language.
>>>
>>> Thanks
>>>
>>> Jacques
>>>
>>> From: <[hidden email]>
>>>> Author: adrianc
>>>> Date: Thu May 21 07:26:22 2009
>>>> New Revision: 776988
>>>>
>>>> URL: http://svn.apache.org/viewvc?rev=776988&view=rev
>>>> Log:
>>>> Refactored the Webtools Label Manager - due to a multitude of
>>>> problems. Most notably:
>>>>
>>>> 1. Not thread safe.
>>>> 2. Server files were being read/written without multi-user
>>>> synchronization
>>>> 3. Reads all UI labels into memory, then stores them in static
>>>> memory, PLUS
>>>> 4. Finds all references of those labels in project files and store
>>>> those references in static memory (!!!)
>>>> 5. Doesn't follow best practices for UI layout
>>>>
>>>> It's amazing the original code worked. Even more amazing is the fact
>>>> that this code made it into the project.
>>>>
>>>> Added:
>>>>    
>>>> ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelFile.java  
>>>> (with props)
>>>>    
>>>> ofbiz/trunk/framework/webtools/webapp/webtools/WEB-INF/actions/labelmanager/ViewReferences.groovy  
>>>> (with props)
>>>> Modified:
>>>>    ofbiz/trunk/framework/webtools/config/WebtoolsUiLabels.xml
>>>>    ofbiz/trunk/framework/webtools/servicedef/services.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/LabelReferences.java
>>>>
>>>>    
>>>> ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/SaveLabelsToXmlFile.java
>>>>
>>>>    
>>>> ofbiz/trunk/framework/webtools/webapp/webtools/WEB-INF/actions/labelmanager/LabelManager.groovy
>>>>
>>>>    
>>>> ofbiz/trunk/framework/webtools/webapp/webtools/WEB-INF/actions/labelmanager/UpdateManager.groovy
>>>>
>>>>    
>>>> ofbiz/trunk/framework/webtools/webapp/webtools/WEB-INF/actions/labelmanager/ViewFile.groovy
>>>>
>>>>    
>>>> ofbiz/trunk/framework/webtools/webapp/webtools/WEB-INF/controller.xml
>>>>    
>>>> ofbiz/trunk/framework/webtools/webapp/webtools/labelmanager/SearchLabels.ftl
>>>>
>>>>    
>>>> ofbiz/trunk/framework/webtools/webapp/webtools/labelmanager/UpdateLabel.ftl
>>>>
>>>>    
>>>> ofbiz/trunk/framework/webtools/webapp/webtools/labelmanager/ViewFile.ftl
>>>>
>>>>    
>>>> ofbiz/trunk/framework/webtools/webapp/webtools/labelmanager/ViewHardcodedLabels.ftl
>>>>
>>>>    
>>>> ofbiz/trunk/framework/webtools/webapp/webtools/labelmanager/ViewLabels.ftl
>>>>
>>>>    
>>>> ofbiz/trunk/framework/webtools/webapp/webtools/labelmanager/ViewReferences.ftl
>>>>
>>>>    ofbiz/trunk/framework/webtools/widget/LabelManagerScreens.xml
>>>>
>>>> Modified: ofbiz/trunk/framework/webtools/config/WebtoolsUiLabels.xml
>>>> URL:
>>>> http://svn.apache.org/viewvc/ofbiz/trunk/framework/webtools/config/WebtoolsUiLabels.xml?rev=776988&r1=776987&r2=776988&view=diff 
>>>>
>>>> ==============================================================================
>>>>
>>>> --- ofbiz/trunk/framework/webtools/config/WebtoolsUiLabels.xml
>>>> (original)
>>>> +++ ofbiz/trunk/framework/webtools/config/WebtoolsUiLabels.xml Thu
>>>> May 21 07:26:22 2009
>>>> @@ -1512,6 +1512,11 @@
>>>>         <value xml:lang="fr">Nom de fichier</value>
>>>>         <value xml:lang="it">Nome File</value>
>>>>     </property>
>>>> +    <property key="WebtoolsLabelManagerFindLabels">
>>>> +        <value xml:lang="en">Find Labels</value>
>>>> +        <value xml:lang="fr">Gestionnaire de traduction</value>
>>>> +        <value xml:lang="it">Gestione delle label</value>
>>>> +    </property>
>>>>     <property key="WebtoolsLabelManagerHardcoded">
>>>>         <value xml:lang="en">Hardcoded labels</value>
>>>>         <value xml:lang="it">Label non traducibili</value>
>>>>
>>>> Modified: ofbiz/trunk/framework/webtools/servicedef/services.xml
>>>> URL:
>>>> http://svn.apache.org/viewvc/ofbiz/trunk/framework/webtools/servicedef/services.xml?rev=776988&r1=776987&r2=776988&view=diff 
>>>>
>>>> ==============================================================================
>>>>
>>>> --- ofbiz/trunk/framework/webtools/servicedef/services.xml (original)
>>>> +++ ofbiz/trunk/framework/webtools/servicedef/services.xml Thu May
>>>> 21 07:26:22 2009
>>>> @@ -123,27 +123,10 @@
>>>>         <attribute name="eomodeldFullPath" type="java.lang.String"
>>>> mode="IN" optional="false"/>
>>>>         <attribute name="serviceName" type="java.lang.String"
>>>> mode="IN" optional="false"/>
>>>>     </service>
>>>> +
>>>>     <service name="saveLabelsToXmlFile" engine="java"
>>>> location="org.ofbiz.webtools.labelmanager.SaveLabelsToXmlFile"
>>>>              invoke="saveLabelsToXmlFile" auth="true"
>>>> use-transaction="false">
>>>>         <description>Save labels to xml file</description>
>>>> -        <attribute name="labelFileName" type="String" mode="IN"
>>>> optional="true"/>
>>>> -    </service>
>>>> -    <service name="updateLabelKey" engine="java"
>>>> location="org.ofbiz.webtools.labelmanager.LabelManagerFactory"
>>>> -             invoke="updateLabelKey" auth="true"
>>>> use-transaction="false">
>>>> -        <description>Update Label Key</description>
>>>> -        <attribute name="key" type="String" mode="IN"
>>>> optional="true"/>
>>>> -        <attribute name="keyComment" type="String" mode="IN"
>>>> optional="true"/>
>>>> -        <attribute name="update_label" type="String" mode="IN"
>>>> optional="false"/>
>>>> -        <attribute name="fileName" type="String" mode="IN"
>>>> optional="false"/>
>>>> -        <attribute name="confirm" type="String" mode="IN"
>>>> optional="true"/>
>>>> -        <attribute name="removeLabel" type="String" mode="IN"
>>>> optional="true"/>
>>>> -        <attribute name="localeNames" type="List" mode="IN"
>>>> optional="true"/>
>>>> -        <attribute name="localeValues" type="List" mode="IN"
>>>> optional="true"/>
>>>> -        <attribute name="localeComments" type="List" mode="IN"
>>>> optional="true"/>
>>>> -    </service>
>>>> -    <service name="updateAndSaveLabelKey" engine="java"
>>>> location="org.ofbiz.webtools.labelmanager.LabelManagerFactory"
>>>> -             invoke="updateAndSaveLabelKey" auth="true"
>>>> use-transaction="false">
>>>> -        <description>Update Label Key</description>
>>>>         <attribute name="key" type="String" mode="IN" optional="true"/>
>>>>         <attribute name="keyComment" type="String" mode="IN"
>>>> optional="true"/>
>>>>         <attribute name="update_label" type="String" mode="IN"
>>>> optional="false"/>
>>>>
>>>> Added:
>>>> ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelFile.java
>>>>
>>>> URL:
>>>> http://svn.apache.org/viewvc/ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelFile.java?rev=776988&view=auto 
>>>>
>>>> ==============================================================================
>>>>
>>>> ---
>>>> ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelFile.java
>>>> (added)
>>>> +++
>>>> ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelFile.java
>>>> Thu May 21 07:26:22 2009
>>>> @@ -0,0 +1,46 @@
>>>> +/*
>>>> + * 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.webtools.labelmanager;
>>>> +
>>>> +import java.io.File;
>>>> +
>>>> +public class LabelFile {
>>>> +    public static final String module = LabelFile.class.getName();
>>>> +
>>>> +    protected final boolean fileLoaded = false;;
>>>> +    protected final File file;
>>>> +    protected final String componentName;
>>>> +
>>>> +    protected LabelFile(File file, String componentName) {
>>>> +        this.file = file;
>>>> +        this.componentName = componentName;
>>>> +    }
>>>> +
>>>> +    public File getFile() {
>>>> +        return this.file;
>>>> +    }
>>>> +
>>>> +    public String getFileName() {
>>>> +        return this.file.getName();
>>>> +    }
>>>> +
>>>> +    public String getFilePath() {
>>>> +        return this.file.getPath();
>>>> +    }
>>>> +}
>>>>
>>>> Propchange:
>>>> ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelFile.java
>>>>
>>>> ------------------------------------------------------------------------------
>>>>
>>>>    svn:eol-style = native
>>>>
>>>> Propchange:
>>>> ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelFile.java
>>>>
>>>> ------------------------------------------------------------------------------
>>>>
>>>>    svn:keywords = "Date Rev Author URL Id"
>>>>
>>>> Propchange:
>>>> ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/labelmanager/LabelFile.java
>>>>
>>>> ------------------------------------------------------------------------------
>>>>
>>>>    svn:mime-type = text/plain
>>>>
>>>> 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=776988&r1=776987&r2=776988&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
>>>> Thu May 21 07:26:22 2009
>>>> @@ -33,14 +33,12 @@
>>>>     protected String labelKey = "";
>>>>     protected String labelKeyComment = "";
>>>>     protected String fileName = "";
>>>> -    protected String componentName = "";
>>>>     protected Map<String, LabelValue> labelValues =
>>>> FastMap.newInstance();
>>>>
>>>> -    public LabelInfo(String labelKey, String labelKeyComment,
>>>> String fileName, String componentName, String localeStr, String
>>>> labelValue, String labelComment) throws GeneralException {
>>>> +    public LabelInfo(String labelKey, String labelKeyComment,
>>>> String fileName, String localeStr, String labelValue, String
>>>> labelComment) throws GeneralException {
>>>>         this.labelKey = labelKey;
>>>>         this.labelKeyComment = labelKeyComment;
>>>>         this.fileName = fileName;
>>>> -        this.componentName = componentName;
>>>>         setLabelValue(localeStr, labelValue, labelComment, false);
>>>>     }
>>>>
>>>> @@ -60,10 +58,6 @@
>>>>         return fileName;
>>>>     }
>>>>
>>>> -    public String getComponentName() {
>>>> -        return componentName;
>>>> -    }
>>>> -
>>>>     public LabelValue getLabelValue(String localeStr) {
>>>>        return (LabelValue)labelValues.get(localeStr);
>>>>     }
>>>>
>>>> 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=776988&r1=776987&r2=776988&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
>>>> Thu May 21 07:26:22 2009
>>>> @@ -20,331 +20,185 @@
>>>>
>>>> import java.io.File;
>>>> import java.io.IOException;
>>>> +import java.net.MalformedURLException;
>>>> import java.util.Collection;
>>>> import java.util.List;
>>>> -import java.util.Locale;
>>>> import java.util.Map;
>>>> import java.util.Set;
>>>> import java.util.TreeMap;
>>>> import java.util.TreeSet;
>>>>
>>>> +import javax.xml.parsers.ParserConfigurationException;
>>>> +
>>>> import javolution.util.FastList;
>>>>
>>>> import org.ofbiz.base.component.ComponentConfig;
>>>> +import org.ofbiz.base.component.ComponentConfig.ClasspathInfo;
>>>> +import org.ofbiz.base.util.Debug;
>>>> import org.ofbiz.base.util.FileUtil;
>>>> import org.ofbiz.base.util.GeneralException;
>>>> import org.ofbiz.base.util.StringUtil;
>>>> -import org.ofbiz.base.util.UtilGenerics;
>>>> -import org.ofbiz.base.util.UtilMisc;
>>>> -import org.ofbiz.base.util.UtilProperties;
>>>> import org.ofbiz.base.util.UtilValidate;
>>>> import org.ofbiz.base.util.UtilXml;
>>>> -import org.ofbiz.base.util.cache.UtilCache;
>>>> -import org.ofbiz.entity.GenericDelegator;
>>>> -import org.ofbiz.entity.GenericValue;
>>>> -import org.ofbiz.entity.model.ModelReader;
>>>> -import org.ofbiz.service.DispatchContext;
>>>> -import org.ofbiz.service.GenericServiceException;
>>>> -import org.ofbiz.service.LocalDispatcher;
>>>> -import org.ofbiz.service.ServiceUtil;
>>>> +import org.owasp.esapi.errors.EncodingException;
>>>> import org.w3c.dom.Comment;
>>>> import org.w3c.dom.Document;
>>>> import org.w3c.dom.Element;
>>>> import org.w3c.dom.Node;
>>>> +import org.xml.sax.SAXException;
>>>>
>>>> public class LabelManagerFactory {
>>>>
>>>>     public static final String module =
>>>> LabelManagerFactory.class.getName();
>>>>     public static final String resource = "WebtoolsUiLabels";
>>>> -
>>>>     public static final String keySeparator = "#";
>>>>
>>>> -    protected static UtilCache<String, LabelManagerFactory>
>>>> labelManagerFactoryCache = new UtilCache<String,
>>>> LabelManagerFactory>("LabelManagerFactory");
>>>> -
>>>> -    protected static Map<String, LabelInfo> labels = null;
>>>> -    protected static Map<String, String> fileNamesFound = null;
>>>> -    protected static Map<String, String> fileComponent = null;
>>>> -    protected static Set<String> localesFound = null;
>>>>     protected static Set<String> componentNamesFound = null;
>>>> -    protected static Map<String, Map<String, Integer>> references =
>>>> null;
>>>> -    protected static List<LabelInfo> duplicatedLocalesLabelsList =
>>>> null;
>>>> -    protected static int duplicatedLocalesLabels = 0;
>>>> -
>>>> -    protected static GenericDelegator delegator;
>>>> -    protected static ModelReader entityModelReader;
>>>> -    protected static DispatchContext dispatchContext;
>>>> -
>>>> -    public static LabelManagerFactory getLabelManagerFactory(String
>>>> delegatorName) throws GeneralException {
>>>> -        if (UtilValidate.isEmpty(delegatorName)) {
>>>> -            delegatorName = "default";
>>>> -        }
>>>> +    protected static Map<String, LabelFile> filesFound = null;
>>>>
>>>> -        LabelManagerFactory lmf =
>>>> labelManagerFactoryCache.get(delegatorName);
>>>> +    protected Map<String, LabelInfo> labels = new TreeMap<String,
>>>> LabelInfo>();
>>>> +    protected Set<String> localesFound = new TreeSet<String>();
>>>> +    protected List<LabelInfo> duplicatedLocalesLabelsList =
>>>> FastList.newInstance();
>>>>
>>>> -        if (lmf == null) {
>>>> -            lmf = new LabelManagerFactory(delegatorName);
>>>> -            labelManagerFactoryCache.put(delegatorName, lmf);
>>>> +    public static synchronized LabelManagerFactory getInstance()
>>>> throws GeneralException, IOException {
>>>> +        if (componentNamesFound == null) {
>>>> +            loadComponentNames();
>>>>         }
>>>> -        return lmf;
>>>> +        if (filesFound == null) {
>>>> +            loadLabelFiles();
>>>> +        }
>>>> +        return new LabelManagerFactory();
>>>>     }
>>>>
>>>> -    protected LabelManagerFactory(String delegatorName) throws
>>>> GeneralException {
>>>> -        delegator =
>>>> GenericDelegator.getGenericDelegator(delegatorName);
>>>> -        entityModelReader = ModelReader.getModelReader(delegatorName);
>>>> -        dispatchContext = new
>>>> DispatchContext("LabelManagerFactoryDispCtx", null,
>>>> this.getClass().getClassLoader(), null);
>>>> -        prepareAll();
>>>> +    protected LabelManagerFactory() {
>>>>     }
>>>>
>>>> -    private static void prepareAll() throws GeneralException {
>>>> -        labels = new TreeMap<String, LabelInfo>();
>>>> -        fileNamesFound = new TreeMap<String, String>();
>>>> -        fileComponent = new TreeMap<String, String>();
>>>> -        localesFound = new TreeSet<String>();
>>>> +    protected static void loadComponentNames() {
>>>>         componentNamesFound = new TreeSet<String>();
>>>> -        duplicatedLocalesLabelsList = FastList.newInstance();
>>>> -        references = null;
>>>> -        int duplicatedLocales = 0;
>>>> -
>>>> -        try {
>>>> -            boolean sharkComponent = false;
>>>> -            Collection<ComponentConfig> componentConfigs =
>>>> ComponentConfig.getAllComponents();
>>>> -
>>>> -            for (ComponentConfig componentConfig : componentConfigs) {
>>>> -                String componentName =
>>>> componentConfig.getComponentName();
>>>> -                List<File> resourceFiles =
>>>> FileUtil.findXmlFiles(componentConfig.getRootLocation(), null,
>>>> "resource", null);
>>>> -                boolean cycle = true;
>>>> -                while (cycle) {
>>>> -                    for (File resourceFile : resourceFiles) {
>>>> -                        String fileName = resourceFile.getName();
>>>> -                        Document resourceDocument =
>>>> UtilXml.readXmlDocument(resourceFile.toURI().toURL());
>>>> -                        Element resourceElem =
>>>> resourceDocument.getDocumentElement();
>>>> -                        String labelKeyComment = "";
>>>> -
>>>> -                        for (Node propertyNode :
>>>> UtilXml.childNodeList(resourceElem.getFirstChild())) {
>>>> -                            if (propertyNode instanceof Element) {
>>>> -                                Element propertyElem = (Element)
>>>> propertyNode;
>>>> -                                String labelKey =
>>>> StringUtil.defaultWebEncoder.canonicalize(propertyElem.getAttribute("key"));
>>>>
>>>> -                                String labelComment = "";
>>>> -
>>>> -                                for (Node valueNode :
>>>> UtilXml.childNodeList(propertyElem.getFirstChild())) {
>>>> -                                    if (valueNode instanceof
>>>> Element) {
>>>> -                                        Element valueElem =
>>>> (Element) valueNode;
>>>> -                                        String localeName =
>>>> valueElem.getAttribute("xml:lang");
>>>> -                                        String labelValue =
>>>> StringUtil.defaultWebEncoder.canonicalize(UtilXml.nodeValue(valueElem.getFirstChild()));
>>>>
>>>> -                                        LabelInfo label =
>>>> labels.get(labelKey + keySeparator + fileName);
>>>> -
>>>> -                                        if
>>>> (UtilValidate.isEmpty(label)) {
>>>> -                                            label = new
>>>> LabelInfo(labelKey, labelKeyComment, fileName, componentName,
>>>> localeName,
>>>> labelValue, labelComment);
>>>> -                                            labels.put(labelKey +
>>>> keySeparator + fileName, label);
>>>> -                                        } else {
>>>> -                                            if
>>>> (label.setLabelValue(localeName, labelValue, labelComment, false)) {
>>>> -                                                
>>>> duplicatedLocalesLabelsList.add(label);
>>>> -                                                duplicatedLocales++;
>>>> -                                            }
>>>> -                                        }
>>>> -                                        localesFound.add(localeName);
>>>> -                                        
>>>> componentNamesFound.add(componentName);
>>>> -                                        
>>>> fileNamesFound.put(fileName, resourceFile.toURI().toString());
>>>> -                                        fileComponent.put(fileName,
>>>> componentName);
>>>> -                                        labelComment = "";
>>>> -                                    } else if (valueNode instanceof
>>>> Comment) {
>>>> -                                        labelComment = labelComment +
>>>> StringUtil.defaultWebEncoder.canonicalize(valueNode.getNodeValue());
>>>> -                                    }
>>>> +        Collection<ComponentConfig> componentConfigs =
>>>> ComponentConfig.getAllComponents();
>>>> +        for (ComponentConfig componentConfig : componentConfigs) {
>>>> +            
>>>> componentNamesFound.add(componentConfig.getComponentName());
>>>> +        }
>>>> +        componentNamesFound.add("shark");
>>>> +    }
>>>> +
>>>> +    protected static void loadLabelFiles() throws IOException {
>>>> +        filesFound = new TreeMap<String, LabelFile>();
>>>> +        List<ClasspathInfo> cpInfos =
>>>> ComponentConfig.getAllClasspathInfos();
>>>> +        for (ClasspathInfo cpi : cpInfos) {
>>>> +            if ("dir".equals(cpi.type)) {
>>>> +                String configRoot =
>>>> cpi.componentConfig.getRootLocation();
>>>> +                configRoot = configRoot.replace('\\', '/');
>>>> +                if (!configRoot.endsWith("/")) {
>>>> +                    configRoot = configRoot + "/";
>>>> +                }
>>>> +                String location = cpi.location.replace('\\', '/');
>>>> +                if (location.startsWith("/")) {
>>>> +                    location = location.substring(1);
>>>> +                }
>>>> +                List<File> resourceFiles =
>>>> FileUtil.findXmlFiles(configRoot + location, null, "resource", null);
>>>> +                for (File resourceFile : resourceFiles) {
>>>> +                    filesFound.put(resourceFile.getName(), new
>>>> LabelFile(resourceFile, cpi.componentConfig.getComponentName()));
>>>> +                }
>>>> +            }
>>>> +        }
>>>> +        List<File> resourceFiles =
>>>> FileUtil.findXmlFiles(System.getProperty("ofbiz.home") +
>>>> "/specialpurpose/shark/config", null,
>>>> "resource", null);
>>>> +        for (File resourceFile : resourceFiles) {
>>>> +            filesFound.put(resourceFile.getName(), new
>>>> LabelFile(resourceFile, "shark"));
>>>> +        }
>>>> +    }
>>>> +
>>>> +    public void findMatchingLabels(String component, String
>>>> fileName, String key, String locale) throws MalformedURLException,
>>>> SAXException, ParserConfigurationException, IOException,
>>>> EncodingException, GeneralException {
>>>> +        if (UtilValidate.isEmpty(component) &&
>>>> UtilValidate.isEmpty(fileName) && UtilValidate.isEmpty(key) &&
>>>> UtilValidate.isEmpty(locale)) {
>>>> +            // Important! Don't allow unparameterized queries -
>>>> doing so will result in loading the entire project into memory
>>>> +            return;
>>>> +        }
>>>> +        for (LabelFile fileInfo : filesFound.values()) {
>>>> +            if (UtilValidate.isNotEmpty(component) &&
>>>> !component.equals(fileInfo.componentName)) {
>>>> +                continue;
>>>> +            }
>>>> +            if (UtilValidate.isNotEmpty(fileName) &&
>>>> !fileName.equals(fileInfo.getFileName())) {
>>>> +                continue;
>>>> +            }
>>>> +            Document resourceDocument =
>>>> UtilXml.readXmlDocument(fileInfo.file.toURL(), false);
>>>> +            Element resourceElem =
>>>> resourceDocument.getDocumentElement();
>>>> +            String labelKeyComment = "";
>>>> +            for (Node propertyNode :
>>>> UtilXml.childNodeList(resourceElem.getFirstChild())) {
>>>> +                if (propertyNode instanceof Element) {
>>>> +                    Element propertyElem = (Element) propertyNode;
>>>> +                    String labelKey =
>>>> StringUtil.defaultWebEncoder.canonicalize(propertyElem.getAttribute("key"));
>>>>
>>>> +                    String labelComment = "";
>>>> +                    for (Node valueNode :
>>>> UtilXml.childNodeList(propertyElem.getFirstChild())) {
>>>> +                        if (valueNode instanceof Element) {
>>>> +                            Element valueElem = (Element) valueNode;
>>>> +                            String localeName =
>>>> valueElem.getAttribute("xml:lang");
>>>> +                            String labelValue =
>>>> StringUtil.defaultWebEncoder.canonicalize(UtilXml.nodeValue(valueElem.getFirstChild()));
>>>>
>>>> +                            LabelInfo label = labels.get(labelKey +
>>>> keySeparator + fileInfo.getFileName());
>>>> +
>>>> +                            if (UtilValidate.isEmpty(label)) {
>>>> +                                label = new LabelInfo(labelKey,
>>>> labelKeyComment, fileInfo.getFileName(), localeName, labelValue,
>>>> labelComment);
>>>> +                                labels.put(labelKey + keySeparator
>>>> + fileInfo.getFileName(), label);
>>>> +                            } else {
>>>> +                                if (label.setLabelValue(localeName,
>>>> labelValue, labelComment, false)) {
>>>> +                                    
>>>> duplicatedLocalesLabelsList.add(label);
>>>>                                 }
>>>> -                                labelKeyComment = "";
>>>> -                            } else if (propertyNode instanceof
>>>> Comment) {
>>>> -                                labelKeyComment = labelKeyComment +
>>>> StringUtil.defaultWebEncoder.canonicalize(propertyNode.getNodeValue());
>>>>                             }
>>>> +                            localesFound.add(localeName);
>>>> +                            labelComment = "";
>>>> +                        } else if (valueNode instanceof Comment) {
>>>> +                            labelComment = labelComment +
>>>> StringUtil.defaultWebEncoder.canonicalize(valueNode.getNodeValue());
>>>>                         }
>>>>                     }
>>>> -                    if (!sharkComponent) {
>>>> -                        componentName = "shark";
>>>> -                        resourceFiles =
>>>> FileUtil.findXmlFiles(System.getProperty("ofbiz.home") +
>>>> "/specialpurpose/shark", null,
>>>> "resource", null);
>>>> -                        sharkComponent = true;
>>>> -                    } else {
>>>> -                        cycle = false;
>>>> -                    }
>>>> +                    labelKeyComment = "";
>>>> +                } else if (propertyNode instanceof Comment) {
>>>> +                    labelKeyComment = labelKeyComment +
>>>> StringUtil.defaultWebEncoder.canonicalize(propertyNode.getNodeValue());
>>>>                 }
>>>>             }
>>>> -
>>>> -            // get labels references from sources
>>>> -            references = LabelReferences.getLabelReferences();
>>>> -        } catch (IOException ioe) {
>>>> -            throw new GeneralException(ioe.getMessage());
>>>> -        } catch (Exception e) {
>>>> -            throw new GeneralException(e.getMessage());
>>>>         }
>>>> -        duplicatedLocalesLabels = duplicatedLocales;
>>>> -    }
>>>> -
>>>> -    public static GenericDelegator getDelegator() {
>>>> -        return delegator;
>>>>     }
>>>>
>>>> -    public static ModelReader getModelReader() {
>>>> -        return entityModelReader;
>>>> +    public LabelFile getLabelFile(String fileName) {
>>>> +        return filesFound.get(fileName);
>>>>     }
>>>>
>>>> -    public static DispatchContext getDispatchContext() {
>>>> -        return dispatchContext;
>>>> -    }
>>>> -
>>>> -    public static Map<String, LabelInfo> getLabels() {
>>>> +    public Map<String, LabelInfo> getLabels() {
>>>>         return labels;
>>>>     }
>>>>
>>>> -    public static Set<String> getLocalesFound() {
>>>> +    public Set<String> getLocalesFound() {
>>>>         return localesFound;
>>>>     }
>>>>
>>>> -    public static Map<String, String> getFileNamesFound() {
>>>> -        return fileNamesFound;
>>>> -    }
>>>> -
>>>> -    public static String getFileComponent(String fileName) {
>>>> -        String componentName = null;
>>>> -        if (UtilValidate.isNotEmpty(fileName)) {
>>>> -            componentName = fileComponent.get(fileName);
>>>> -        }
>>>> -        return componentName;
>>>> +    public static Collection<LabelFile> getFilesFound() {
>>>> +        return filesFound.values();
>>>>     }
>>>>
>>>>     public static Set<String> getComponentNamesFound() {
>>>>         return componentNamesFound;
>>>>     }
>>>>
>>>> -    public static Map<String, Map<String, Integer>> getReferences() {
>>>> -        return references;
>>>> -    }
>>>> -
>>>> -    public static Set<String> getLabelsList() {
>>>> +    public Set<String> getLabelsList() {
>>>>         return labels.keySet();
>>>>     }
>>>>
>>>> -    public static Set<String> getReferencesList() {
>>>> -        return references.keySet();
>>>> +    public int getDuplicatedLocalesLabels() {
>>>> +        return duplicatedLocalesLabelsList.size();
>>>>     }
>>>>
>>>> -    public static int getLabelReferenceFile(String key) {
>>>> -        int refFile = 0;
>>>> -        boolean keyFound = false;
>>>> -
>>>> -        if (key == null) {
>>>> -            key = "";
>>>> -        }
>>>> -
>>>> -        for (Map.Entry<String, String> e :
>>>> fileNamesFound.entrySet()) {
>>>> -            String keyToSearch = key + keySeparator + e.getKey();
>>>> -
>>>> -            if (labels.containsKey(keyToSearch)) {
>>>> -                keyFound = true;
>>>> -                break;
>>>> -            }
>>>> -        }
>>>> -
>>>> -        if (!keyFound) {
>>>> -            Map<String, Integer> reference = references.get(key);
>>>> -
>>>> -            if (UtilValidate.isNotEmpty(reference)) {
>>>> -                refFile = reference.size();
>>>> -            }
>>>> -        }
>>>> -
>>>> -        return refFile;
>>>> -    }
>>>> -
>>>> -    public static int getDuplicatedLocalesLabels() {
>>>> -        return duplicatedLocalesLabels;
>>>> -    }
>>>> -
>>>> -    public static List<LabelInfo> getDuplicatedLocalesLabelsList() {
>>>> +    public List<LabelInfo> getDuplicatedLocalesLabelsList() {
>>>>         return duplicatedLocalesLabelsList;
>>>>     }
>>>>
>>>> -    public static Map<String, Object>
>>>> updateLabelKey(DispatchContext dctx, Map<String, ? extends Object>
>>>> context) {
>>>> -        String key = (String) context.get("key");
>>>> -        String keyComment = (String) context.get("keyComment");
>>>> -        String update_label = (String) context.get("update_label");
>>>> -        String fileName = (String) context.get("fileName");
>>>> -        String confirm = (String) context.get("confirm");
>>>> -        String removeLabel = (String) context.get("removeLabel");
>>>> -        List<String> localeNames =
>>>> UtilGenerics.cast(context.get("localeNames"));
>>>> -        List<String> localeValues =
>>>> UtilGenerics.cast(context.get("localeValues"));
>>>> -        List<String> localeComments =
>>>> UtilGenerics.cast(context.get("localeComments"));
>>>> -        Locale locale = (Locale) context.get("locale");
>>>> -
>>>> -        // Remove a Label
>>>> -        if (UtilValidate.isNotEmpty(removeLabel)) {
>>>> -            labels.remove(key + keySeparator + fileName);
>>>> -        } else if (UtilValidate.isNotEmpty(confirm)) {
>>>> -            LabelInfo label = labels.get(key + keySeparator +
>>>> fileName);
>>>> -
>>>> -            // Update a Label
>>>> -            if (update_label.equalsIgnoreCase("Y")) {
>>>> -                if (UtilValidate.isNotEmpty(label)) {
>>>> -                    updateLabelValue(localeNames, localeValues,
>>>> localeComments, label, key, keyComment, fileName);
>>>> -                }
>>>> -                // Insert a new Label
>>>> -            } else {
>>>> -                if (UtilValidate.isNotEmpty(label)) {
>>>> -                    return
>>>> ServiceUtil.returnError(UtilProperties.getMessage(resource,
>>>> "WebtoolsLabelManagerNewLabelExisting",
>>>> UtilMisc.toMap("key", key, "fileName", fileName), locale));
>>>> -                } else {
>>>> -                    if (UtilValidate.isEmpty(key)) {
>>>> -                        return
>>>> ServiceUtil.returnError(UtilProperties.getMessage(resource,
>>>> "WebtoolsLabelManagerNewLabelEmptyKey", locale));
>>>> -                    } else {
>>>> -                        int notEmptyLabels =
>>>> updateLabelValue(localeNames, localeValues, localeComments, null,
>>>> key, keyComment,
>>>> fileName);
>>>> -                        if (notEmptyLabels == 0) {
>>>> -                            return
>>>> ServiceUtil.returnError(UtilProperties.getMessage(resource,
>>>> "WebtoolsLabelManagerNewLabelEmpty", locale));
>>>> -                        }
>>>> -                    }
>>>> -                }
>>>> -            }
>>>> -        }
>>>> -
>>>> -        return ServiceUtil.returnSuccess();
>>>> -    }
>>>> -
>>>> -    public static Map<String, Object>
>>>> updateAndSaveLabelKey(DispatchContext dctx, Map<String, ? extends
>>>> Object> context) {
>>>> -        String key = (String) context.get("key");
>>>> -        String keyComment = (String) context.get("keyComment");
>>>> -        String update_label = (String) context.get("update_label");
>>>> -        String fileName = (String) context.get("fileName");
>>>> -        String confirm = (String) context.get("confirm");
>>>> -        String removeLabel = (String) context.get("removeLabel");
>>>> -        List<String> localeNames =
>>>> UtilGenerics.cast(context.get("localeNames"));
>>>> -        List<String> localeValues =
>>>> UtilGenerics.cast(context.get("localeValues"));
>>>> -        List<String> localeComments =
>>>> UtilGenerics.cast(context.get("localeComments"));
>>>> -        GenericValue userLogin = (GenericValue)
>>>> context.get("userLogin");
>>>> -        LocalDispatcher dispatcher = dctx.getDispatcher();
>>>> -
>>>> -        Map<String, Object> contextInput = UtilMisc.toMap("key",
>>>> key, "keyComment", keyComment, "update_label", update_label,
>>>> "fileName", fileName, "confirm", confirm, "removeLabel", removeLabel,
>>>> -                "localeNames", localeNames, "localeValues",
>>>> localeValues, "localeComments", localeComments, "userLogin",
>>>> userLogin);
>>>> -        try {
>>>> -            Map<String, Object> updatedKey =
>>>> dispatcher.runSync("updateLabelKey", contextInput);
>>>> -
>>>> -            if (ServiceUtil.isError(updatedKey)) {
>>>> -                return updatedKey;
>>>> -            } else {
>>>> -                return dispatcher.runSync("saveLabelsToXmlFile",
>>>> UtilMisc.toMap("labelFileName", fileName, "userLogin",
>>>> userLogin));
>>>> -            }
>>>> -        } catch (GenericServiceException e) {
>>>> -            return ServiceUtil.returnError("error on saving label
>>>> key :" + key);
>>>> -        }
>>>> -    }
>>>> -
>>>> -    private static int updateLabelValue(List<String> localeNames,
>>>> List<String> localeValues, List<String> localeComments,
>>>> LabelInfo label, String key, String keyComment, String fileName) {
>>>> +    public int updateLabelValue(List<String> localeNames,
>>>> List<String> localeValues, List<String> localeComments, LabelInfo
>>>> label, String key, String keyComment, String fileName) {
>>>>         int notEmptyLabels = 0;
>>>> -        int i = 0;
>>>> -        while (i < localeNames.size()) {
>>>> +        for (int i = 0; i < localeNames.size(); i++) {
>>>>             String localeName = localeNames.get(i);
>>>>             String localeValue = localeValues.get(i);
>>>>             String localeComment = localeComments.get(i);
>>>> -
>>>>             if (UtilValidate.isNotEmpty(localeValue) ||
>>>> UtilValidate.isNotEmpty(localeComment)) {
>>>>                 if (label == null) {
>>>>                     try {
>>>> -                        String componentName =
>>>> getFileComponent(fileName);
>>>> -                        label = new LabelInfo(key, keyComment,
>>>> fileName, componentName, localeName, localeValue, localeComment);
>>>> +                        label = new LabelInfo(key, keyComment,
>>>> fileName, localeName, localeValue, localeComment);
>>>>                         labels.put(key + keySeparator + fileName,
>>>> label);
>>>>                     } catch (Exception e) {
>>>>                         e.printStackTrace();
>>>> @@ -355,9 +209,7 @@
>>>>                 label.setLabelValue(localeName, localeValue,
>>>> localeComment, true);
>>>>                 notEmptyLabels++;
>>>>             }
>>>> -            i++;
>>>>         }
>>>> -
>>>>         return notEmptyLabels;
>>>>     }
>>>> }
>>>>
>>>>
>>>
>>>
>>>
>>
>
>
>