LabelsInfo14.patch from Marco "New tool to get labels information" (https://issues.apache.org/jira/browse/OFBIZ-2070) - OFBIZ-2070

+ some correct changes done with this update of Labels Manager (I updated some wrong ones by hand to keep going forward...)

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@739955 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jacques Le Roux
2009-02-02 10:17:34 +00:00
parent e1a14431c6
commit 5abb9596cb
16 changed files with 688 additions and 526 deletions
@@ -31,21 +31,31 @@ public class LabelInfo {
public static final String module = LabelInfo.class.getName();
protected String labelKey = "";
protected String labelKeyComment = "";
protected String fileName = "";
protected String componentName = "";
protected Map<String, String> labelValues = FastMap.newInstance();
protected String componentName = "";
protected Map<String, LabelValue> labelValues = FastMap.newInstance();
public LabelInfo(String labelKey, String fileName, String componentName, String localeStr, String labelValue) throws GeneralException {
public LabelInfo(String labelKey, String labelKeyComment, String fileName, String componentName, String localeStr, String labelValue, String labelComment) throws GeneralException {
this.labelKey = labelKey;
this.labelKeyComment = labelKeyComment;
this.fileName = fileName;
this.componentName = componentName;
setLabelValue(localeStr, labelValue, false);
setLabelValue(localeStr, labelValue, labelComment, false);
}
public String getLabelKey() {
return labelKey;
}
public String getLabelKeyComment() {
return labelKeyComment;
}
public void setLabelKeyComment(String labelKeyComment) {
this.labelKeyComment = labelKeyComment;
}
public String getFileName() {
return fileName;
}
@@ -54,26 +64,29 @@ public class LabelInfo {
return componentName;
}
public String getLabelValue(String localeStr) {
return (String)labelValues.get(localeStr);
public LabelValue getLabelValue(String localeStr) {
return (LabelValue)labelValues.get(localeStr);
}
public int getLabelValueSize() {
return labelValues.size();
}
public boolean setLabelValue(String localeStr, String labelValue, boolean update) {
String localeFound = getLabelValue(localeStr);
public boolean setLabelValue(String localeStr, String labelValue, String labelComment, boolean update) {
LabelValue localeFound = getLabelValue(localeStr);
boolean isDuplicatedLocales = false;
if (UtilValidate.isEmpty(localeFound)) {
if (UtilValidate.isNotEmpty(labelValue)) {
labelValues.put(localeStr, labelValue);
localeFound = new LabelValue(labelValue, labelComment);
labelValues.put(localeStr, localeFound);
}
} else {
if (update) {
if (UtilValidate.isNotEmpty(labelValue)) {
labelValues.put(localeStr, labelValue);
localeFound.setLabelValue(labelValue);
localeFound.setLabelComment(labelComment);
labelValues.put(localeStr, localeFound);
} else {
labelValues.remove(localeStr);
}
@@ -31,6 +31,7 @@ import java.util.TreeSet;
import org.ofbiz.base.component.ComponentConfig;
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;
@@ -41,8 +42,10 @@ import org.ofbiz.base.util.cache.UtilCache;
import org.ofbiz.service.DispatchContext;
import org.ofbiz.service.ServiceUtil;
import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
public class LabelManagerFactory {
@@ -101,27 +104,41 @@ public class LabelManagerFactory {
String fileName = resourceFile.getName();
Document resourceDocument = UtilXml.readXmlDocument(resourceFile.toURI().toURL());
Element resourceElem = resourceDocument.getDocumentElement();
String labelKeyComment = "";
for (Element propertyElem: UtilXml.childElementList(resourceElem, "property")) {
String labelKey = propertyElem.getAttribute("key");
for (Element valueElem: UtilXml.childElementList(propertyElem, "value")) {
String localeName = valueElem.getAttribute("xml:lang");
String labelValue = UtilXml.elementValue(valueElem);
LabelInfo label = (LabelInfo)labels.get(labelKey + keySeparator + fileName);
for (Node propertyNode: UtilXml.childNodeList(resourceElem.getFirstChild())) {
if (propertyNode instanceof Element) {
Element propertyElem = (Element)propertyNode;
String labelKey = StringUtil.htmlSpecialChars(propertyElem.getAttribute("key"), true, true, false);
String labelComment = "";
if (UtilValidate.isEmpty(label)) {
label = new LabelInfo(labelKey, fileName, componentName, localeName, labelValue);
labels.put(labelKey + keySeparator + fileName, label);
} else {
if (label.setLabelValue(localeName, labelValue, false)) {
duplicatedLocales++;
for (Node valueNode: UtilXml.childNodeList(propertyElem.getFirstChild())) {
if (valueNode instanceof Element) {
Element valueElem = (Element)valueNode;
String localeName = valueElem.getAttribute("xml:lang");
String labelValue = StringUtil.htmlSpecialChars(UtilXml.nodeValue(valueElem.getFirstChild()), true, true, false);
LabelInfo label = (LabelInfo)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)) {
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.htmlSpecialChars(valueNode.getNodeValue(), true, true, false);
}
}
localesFound.add(localeName);
componentNamesFound.add(componentName);
fileNamesFound.put(fileName, resourceFile.toURI().toString());
fileComponent.put(fileName, componentName);
}
labelKeyComment = "";
} else if (propertyNode instanceof Comment) {
labelKeyComment = labelKeyComment + StringUtil.htmlSpecialChars(propertyNode.getNodeValue(), true, true, false);
}
}
}
@@ -168,12 +185,14 @@ public class LabelManagerFactory {
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
@@ -185,7 +204,7 @@ public class LabelManagerFactory {
// Update a Label
if (update_label.equalsIgnoreCase("Y")) {
if (UtilValidate.isNotEmpty(label)) {
updateLabelValue(localeNames, localeValues, label, key, fileName);
updateLabelValue(localeNames, localeValues, localeComments, label, key, keyComment, fileName);
}
// Insert a new Label
} else {
@@ -195,7 +214,7 @@ public class LabelManagerFactory {
if (UtilValidate.isEmpty(key)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "WebtoolsLabelManagerNewLabelEmptyKey", locale));
} else {
int notEmptyLabels = updateLabelValue(localeNames, localeValues, null, key, fileName);
int notEmptyLabels = updateLabelValue(localeNames, localeValues, localeComments, null, key, keyComment, fileName);
if (notEmptyLabels == 0) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "WebtoolsLabelManagerNewLabelEmpty", locale));
}
@@ -207,24 +226,27 @@ public class LabelManagerFactory {
return ServiceUtil.returnSuccess();
}
private static int updateLabelValue(List<String> localeNames, List<String> localeValues, LabelInfo label, String key, String fileName) {
private static 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()) {
String localeName = (String)localeNames.get(i);
String localeValue = (String)localeValues.get(i);
String localeComment = (String)localeComments.get(i);
if (UtilValidate.isNotEmpty(localeValue)) {
if (UtilValidate.isNotEmpty(localeValue) || UtilValidate.isNotEmpty(localeComment)) {
if (label == null) {
try {
String componentName = getFileComponent(fileName);
label = new LabelInfo(key, fileName, componentName, localeName, localeValue);
label = new LabelInfo(key, keyComment, fileName, componentName, localeName, localeValue, localeComment);
labels.put(key + keySeparator + fileName, label);
} catch(Exception e) {
e.printStackTrace();
}
} else {
label.setLabelKeyComment(keyComment);
}
label.setLabelValue(localeName, localeValue, true);
label.setLabelValue(localeName, localeValue, localeComment, true);
notEmptyLabels++;
}
i++;
@@ -0,0 +1,48 @@
/*
* 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;
public class LabelValue {
public static final String module = LabelValue.class.getName();
protected String labelValue = "";
protected String labelComment = "";
public LabelValue(String labelValue, String labelComment) {
this.labelValue = labelValue;
this.labelComment = labelComment;
}
public String getLabelValue() {
return labelValue;
}
public String getLabelComment() {
return labelComment;
}
public void setLabelValue(String labelValue) {
this.labelValue = labelValue;
}
public void setLabelComment(String labelComment) {
this.labelComment = labelComment;
}
}
@@ -27,104 +27,119 @@ import java.util.Set;
import org.apache.xml.serialize.OutputFormat;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.FileUtil;
import org.ofbiz.base.util.StringUtil;
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.base.util.FileUtil;
import org.ofbiz.service.DispatchContext;
import org.ofbiz.service.ServiceUtil;
import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
public class SaveLabelsToXmlFile {
private static final String resource = "WebtoolsUiLabels";
private static final String module = SaveLabelsToXmlFile.class.getName();
private static final String resource = "WebtoolsUiLabels";
private static final String module = SaveLabelsToXmlFile.class.getName();
public static Map<String, Object> saveLabelsToXmlFile(DispatchContext dctx, Map<String, ? extends Object> context) {
Locale locale = (Locale)context.get("locale");
String labelFileName = (String)context.get("labelFileName");
public static Map<String, Object> saveLabelsToXmlFile(DispatchContext dctx, Map<String, ? extends Object> context) {
Locale locale = (Locale)context.get("locale");
String labelFileName = (String)context.get("labelFileName");
try {
LabelManagerFactory.getLabelManagerFactory(dctx.getDelegator().getDelegatorName());
Map<String, LabelInfo> labels = LabelManagerFactory.getLabels();
Map<String, String> fileNamesFound = LabelManagerFactory.getFileNamesFound();
Set<String> labelsList = LabelManagerFactory.getLabelsList();
Set<String> localesFound = LabelManagerFactory.getLocalesFound();
try {
LabelManagerFactory.getLabelManagerFactory(dctx.getDelegator().getDelegatorName());
Map<String, LabelInfo> labels = LabelManagerFactory.getLabels();
Map<String, String> fileNamesFound = LabelManagerFactory.getFileNamesFound();
Set<String> labelsList = LabelManagerFactory.getLabelsList();
Set<String> localesFound = LabelManagerFactory.getLocalesFound();
for (String fileName : fileNamesFound.keySet()) {
if (UtilValidate.isNotEmpty(labelFileName) && !(labelFileName.equalsIgnoreCase(fileName))) {
continue;
}
for (String fileName : fileNamesFound.keySet()) {
if (UtilValidate.isNotEmpty(labelFileName) && !(labelFileName.equalsIgnoreCase(fileName))) {
continue;
}
String uri = (String)fileNamesFound.get(fileName);
Document resourceDocument = UtilXml.makeEmptyXmlDocument("resource");
Element resourceElem = resourceDocument.getDocumentElement();
resourceElem.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
String uri = (String)fileNamesFound.get(fileName);
Document resourceDocument = UtilXml.makeEmptyXmlDocument("resource");
Element resourceElem = resourceDocument.getDocumentElement();
resourceElem.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
for (String labelKey : labelsList) {
LabelInfo labelInfo = (LabelInfo)labels.get(labelKey);
for (String labelKey : labelsList) {
LabelInfo labelInfo = (LabelInfo)labels.get(labelKey);
if (!(labelInfo.getFileName().equalsIgnoreCase(fileName))) {
continue;
}
if (!(labelInfo.getFileName().equalsIgnoreCase(fileName))) {
continue;
}
Element propertyElem = UtilXml.addChildElement(resourceElem, "property", resourceDocument);
propertyElem.setAttribute("key", labelInfo.getLabelKey());
for (String localeFound : localesFound) {
String labelValue = labelInfo.getLabelValue(localeFound);
if (UtilValidate.isNotEmpty(labelValue)) {
Element valueElem = UtilXml.addChildElementValue(propertyElem, "value", labelValue, resourceDocument);
valueElem.setAttribute("xml:lang", localeFound);
}
}
}
if (UtilValidate.isNotEmpty(resourceElem) && UtilValidate.isNotEmpty(uri)) {
File outFile = new File(new URI(uri));
FileOutputStream fos = new FileOutputStream(outFile);
OutputFormat format = new OutputFormat(resourceDocument.getDocumentElement().getOwnerDocument(), "UTF-8", true);
Element propertyElem = UtilXml.addChildElement(resourceElem, "property", resourceDocument);
propertyElem.setAttribute("key", StringUtil.fromHtmlToSpecialChars(labelInfo.getLabelKey(), true, true, false));
try {
format.setIndent(4);
format.setOmitXMLDeclaration(true);
UtilXml.writeXmlDocument(fos, resourceElem, format);
} finally {
if (UtilValidate.isNotEmpty(fos)) {
fos.close();
if (UtilValidate.isNotEmpty(labelInfo.getLabelKeyComment())) {
Comment labelKeyComment = resourceDocument.createComment(StringUtil.fromHtmlToSpecialChars(labelInfo.getLabelKeyComment(), true, true, false));
Node parent = propertyElem.getParentNode();
parent.insertBefore(labelKeyComment, propertyElem);
}
for (String localeFound : localesFound) {
LabelValue labelValue = labelInfo.getLabelValue(localeFound);
if (UtilValidate.isNotEmpty(labelValue)) {
Element valueElem = UtilXml.addChildElementValue(propertyElem, "value", StringUtil.fromHtmlToSpecialChars(labelValue.getLabelValue(), true, true, false), resourceDocument);
valueElem.setAttribute("xml:lang", localeFound);
if (UtilValidate.isNotEmpty(labelValue.getLabelComment())) {
Comment labelComment = resourceDocument.createComment(StringUtil.fromHtmlToSpecialChars(labelValue.getLabelComment(), true, true, false));
Node parent = valueElem.getParentNode();
parent.insertBefore(labelComment, valueElem);
}
}
}
}
if (UtilValidate.isNotEmpty(resourceElem) && UtilValidate.isNotEmpty(uri)) {
File outFile = new File(new URI(uri));
FileOutputStream fos = new FileOutputStream(outFile);
OutputFormat format = new OutputFormat(resourceDocument.getDocumentElement().getOwnerDocument(), "UTF-8", true);
try {
format.setIndent(4);
format.setOmitXMLDeclaration(true);
UtilXml.writeXmlDocument(fos, resourceElem, format);
} finally {
if (UtilValidate.isNotEmpty(fos)) {
fos.close();
// workaround to insert the Apache License Header at top of the file
// because the comment on top the xml file has been not written
String outBuffer = FileUtil.readString("UTF-8", outFile);
String basePath = System.getProperty("ofbiz.home");
// workaround to insert the Apache License Header at top of the file
// because the comment on top the xml file has been not written
String outBuffer = FileUtil.readString("UTF-8", outFile);
String basePath = System.getProperty("ofbiz.home");
if (UtilValidate.isNotEmpty(basePath)) {
String apacheHeaderFileName = basePath + "/framework/webtools/config/APACHE2_HEADER_FOR_XML";
String apacheHeaderBuffer = "";
File apacheHeaderFile = new File(apacheHeaderFileName);
if (UtilValidate.isNotEmpty(basePath)) {
String apacheHeaderFileName = basePath + "/framework/webtools/config/APACHE2_HEADER_FOR_XML";
String apacheHeaderBuffer = "";
File apacheHeaderFile = new File(apacheHeaderFileName);
if (UtilValidate.isNotEmpty(apacheHeaderFile)) {
apacheHeaderBuffer = FileUtil.readString("UTF-8", apacheHeaderFile);
}
if (UtilValidate.isNotEmpty(apacheHeaderFile)) {
apacheHeaderBuffer = FileUtil.readString("UTF-8", apacheHeaderFile);
}
FileUtil.writeString("UTF-8", apacheHeaderBuffer + outBuffer, outFile);
FileUtil.writeString("UTF-8", apacheHeaderBuffer + outBuffer, outFile);
// clear cache to see immediately the new labels and translations in OFBiz
UtilCache.clearCache("properties.UtilPropertiesBundleCache");
}
}
}
}
}
} catch (Exception e) {
Debug.logError(e, "Exception during save labels to xml file:", module);
return ServiceUtil.returnFailure(UtilProperties.getMessage(resource, "saveLabelsToXmlFile.exceptionDuringSaveLabelsToXmlFile", locale));
}
return ServiceUtil.returnSuccess();
}
// clear cache to see immediately the new labels and translations in OFBiz
UtilCache.clearCache("properties.UtilPropertiesBundleCache");
}
}
}
}
}
} catch (Exception e) {
Debug.logError(e, "Exception during save labels to xml file:", module);
return ServiceUtil.returnFailure(UtilProperties.getMessage(resource, "saveLabelsToXmlFile.exceptionDuringSaveLabelsToXmlFile", locale));
}
return ServiceUtil.returnSuccess();
}
}