Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating xml-builder #2

Merged
merged 5 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ build.xml
manifest.mf
/lib/
/keystores/
/bin/
.project
.classpath
/bin/
34 changes: 0 additions & 34 deletions pom.xml

This file was deleted.

197 changes: 197 additions & 0 deletions src/mz/cassamo/jls/Builder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
package mz.cassamo.jls;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import mz.cassamo.jls.exceptions.BuilderParseException;

/**
*
* @author cassamo
*/
class Builder {

private final Map<String, Map<String, String>> translations = new HashMap<>();
private String filePath = null;
public Builder() {
}


public void loadFromFile(String filePath) throws BuilderParseException {
this.filePath = filePath;


try {
String dir_path = new File(filePath).getAbsoluteFile().getParent();
File f = new File(dir_path);
if(!f.exists()) {
f.mkdir();
}
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
LanguageHandler handler = new LanguageHandler();

saxParser.parse(new File(filePath), handler);
mergeTranslations(handler.getLanguages());
} catch (Exception e) {
if(e.getClass().getName().equals("java.io.FileNotFoundException")) {
File file = new File(filePath);
try {
FileWriter w = new FileWriter(filePath);
w.write("<languages>\n</languages>");
w.flush();
} catch (IOException e1) {

}

}
else if(e.getClass().getName().equals("org.xml.sax.SAXParseException")) {
throw new BuilderParseException(e.getMessage());

} else {
System.out.println(e.getClass().getName());
System.out.println(e.getMessage());
}

}
}


public void loadFromResources(String resourcePath, Class<?> resourceClass) {
try (InputStream inputStream = resourceClass.getResourceAsStream(resourcePath)) {
if (inputStream == null) {
throw new IllegalArgumentException("Resource not found: " + resourcePath);
}

SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
LanguageHandler handler = new LanguageHandler();

saxParser.parse(inputStream, handler);
mergeTranslations(handler.getLanguages());
} catch (Exception e) {

}
}


public void putLanguage(String language) {
translations.putIfAbsent(language, new HashMap<>());
normalizeTranslations();
}


public void removeLanguage(String language) {
translations.remove(language);
}


public void putTranslation(String language, String key, String value) {
translations.putIfAbsent(language, new HashMap<>());
translations.get(language).put(key, value);
normalizeTranslations();
}


public void removeTranslation(String language, String key) {
if (translations.containsKey(language)) {
translations.get(language).remove(key);
}
normalizeTranslations();
}


public void saveToFile(String path) {
try (FileWriter writer = new FileWriter(path)) {
writer.write(toXmlString());
} catch (IOException e) {
if (LanguageSystem.isDebugMode()) {

}
}
}

public void save() {
try (FileWriter writer = new FileWriter(filePath)) {
writer.write(toXmlString());
} catch (IOException e) {
if (LanguageSystem.isDebugMode()) {
e.printStackTrace();
}
}
}

public String toXmlString() {
StringBuilder xmlBuilder = new StringBuilder();
xmlBuilder.append("<!--%s %s-->\n".formatted(Info.LIB_NAME, Info.VERSION));
xmlBuilder.append("<!--LANGUAGES: ").append(translations.size()).append("-->\n");

xmlBuilder.append("<languages>\n");

for (Map.Entry<String, Map<String, String>> languageEntry : translations.entrySet()) {
String language = languageEntry.getKey();
Map<String, String> languageTranslations = languageEntry.getValue();

xmlBuilder.append(" <language value=\"").append(language).append("\">\n");
for (Map.Entry<String, String> translationEntry : languageTranslations.entrySet()) {
String key = translationEntry.getKey();
String value = translationEntry.getValue();

xmlBuilder.append(" <translated value=\"").append(key).append("\">\n");
xmlBuilder.append(" <value>").append(escapeXml(value)).append("</value>\n");
xmlBuilder.append(" </translated>\n");
}
xmlBuilder.append(" </language>\n");
}

xmlBuilder.append("</languages>");
return xmlBuilder.toString();
}

// Escapa caracteres especiais para XML
private String escapeXml(String value) {
if (value == null) {
return "";
}
return value.replace("&", "&amp;")
.replace("<", "&lt;")
.replace(">", "&gt;")
.replace("\"", "&quot;")
.replace("'", "&apos;");
}


private void mergeTranslations(Map<String, Map<String, String>> newTranslations) {
for (Map.Entry<String, Map<String, String>> entry : newTranslations.entrySet()) {
translations.putIfAbsent(entry.getKey(), new HashMap<>());
translations.get(entry.getKey()).putAll(entry.getValue());
}
normalizeTranslations();
}


private void normalizeTranslations() {
Set<String> allKeys = new HashSet<>();


for (Map<String, String> languageMap : translations.values()) {
allKeys.addAll(languageMap.keySet());
}


for (Map.Entry<String, Map<String, String>> entry : translations.entrySet()) {
Map<String, String> languageMap = entry.getValue();
for (String key : allKeys) {
languageMap.putIfAbsent(key, "");
}
}
}
}
11 changes: 11 additions & 0 deletions src/mz/cassamo/jls/Info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package mz.cassamo.jls;

/**
*
* @author cassamo
*/
class Info {

public static final String LIB_NAME = "JLS";
public static final String VERSION = "1.0.0";
}
53 changes: 45 additions & 8 deletions src/mz/cassamo/jls/LanguageSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;

/**
* A system for managing and applying language translations across user
Expand All @@ -27,7 +28,6 @@
*/
public class LanguageSystem {


private static final ArrayList<HashMap<String, Object>> appliedComponents = new ArrayList<>();
private static final ArrayList<HashMap<String, Object>> appliedWidgets = new ArrayList<>();
private static boolean debug = false;
Expand Down Expand Up @@ -99,10 +99,11 @@ public static ArrayList<String> getLanguages() {
}
return langs;
}

/**
* Gets a list of available translations for specific language.
*@param language the language to be used.
*
* @param language the language to be used.
* @return a list of translations.
*/
public static ArrayList<String> getTranslationKeys(String language) {
Expand All @@ -127,6 +128,25 @@ public static void setCurrentLanguage(String language) {
}
}

/**
* Sets the current language of the system with a fallback to the default
* language if the specified language does not exist.
*
* @param language the desired language code.
* @param default_language the default language code to use as a fallback.
*/
public static void setCurrentLanguage(String language, String default_language) {
String lang = language;
if (!existsLanguage(language)) {
lang = default_language;
}
LanguageReader.setLanguage(lang);
autoInsertLanguage();
if (li != null) {
li.onChange(language);
}
}

/**
* Automatically translates a single component based on the provided
* language key.
Expand All @@ -149,17 +169,13 @@ public static void autoTranslateComponent(Component component, String language_k
* @param widget the android widget to be translated.
* @param language_key the language key for translation.
*/

/* public static void autoTranslateWidget(Object widget, String language_key) {
/* public static void autoTranslateWidget(Object widget, String language_key) {
HashMap<String, Object> temp = new HashMap<>();
temp.put("widget", widget);
temp.put("language_value", language_key);
appliedWidgets.add(temp);
autoInsertLanguage();
}*/



/**
* Automatically translates multiple components based on the provided
* language key.
Expand Down Expand Up @@ -337,4 +353,25 @@ public static boolean existsLanguage(String language) {
public static boolean existsKey(String key) {
return get(key, null) != null;
}

/**
* Retrieves the default system language based on the user's locale.
*
* @return the system's default language in English.
*/
public static String getDefaultSystemLanguage() {
Locale currentLocale = Locale.getDefault();
String language = currentLocale.getDisplayLanguage(Locale.ENGLISH);

return language.toLowerCase();
}

public static class Builder extends mz.cassamo.jls.Builder {

public Builder() {
super();
}

}

}
20 changes: 20 additions & 0 deletions src/mz/cassamo/jls/exceptions/BuilderParseException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package mz.cassamo.jls.exceptions;

public class BuilderParseException extends Exception {

public BuilderParseException(String message) {
super(message);

}

public BuilderParseException(Throwable cause) {
super(cause);
}

public BuilderParseException(String message, Throwable cause) {
super(message, cause);

}


}
Loading