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

Draft: Translation manager #1237

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@
<artifactId>javafx-swing</artifactId>
<version>17.0.2</version>
</dependency>

<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-web</artifactId>
<version>17.0.2</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
Expand Down Expand Up @@ -78,6 +84,12 @@
<version>2.6</version>
</dependency>

<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.40.0.0</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
requires javafx.controls;
requires javafx.media;
requires javafx.swing;
requires javafx.web;
requires java.desktop;
requires java.logging;
requires java.sql;
requires java.xml;
requires jdk.charsets;
requires jsr305; //AUT - fixed with module injector - only needed for jsoup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.darisadesigns.polyglotlina.CustomControls.GrammarSectionNode;
import org.darisadesigns.polyglotlina.CustomControls.GrammarChapNode;
import org.darisadesigns.polyglotlina.ManagersCollections.RomanizationManager;
import org.darisadesigns.polyglotlina.ManagersCollections.TranslationManager;
import org.darisadesigns.polyglotlina.Nodes.EtyExternalParent;
import org.darisadesigns.polyglotlina.Nodes.ToDoNode;
import org.darisadesigns.polyglotlina.Nodes.WordClassValue;
Expand Down Expand Up @@ -97,7 +98,7 @@ public static CustHandler getCustHandler(InputStream iStream, DictCore core) thr
private static CustHandler get075orHigherHandler(final DictCore core, final int versionHierarchy) {
return new CustHandler() {

private StringBuilder stringBuilder;
private StringBuilder stringBuilder = new StringBuilder();
PronunciationNode proBuffer;
PronunciationNode romBuffer;
String charRepCharBuffer = "";
Expand Down Expand Up @@ -236,7 +237,7 @@ private static CustHandler get075orHigherHandler(final DictCore core, final int

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) {
stringBuilder = new StringBuilder();
stringBuilder.setLength(0);

if (qName.equalsIgnoreCase(PGTUtil.DICTIONARY_SAVE_DATE)) {
blastSave = true;
Expand Down Expand Up @@ -493,6 +494,7 @@ public void startElement(String uri, String localName, String qName, Attributes
@Override
public void endElement(String uri, String localName,
String qName) throws SAXException {
String builderValue = stringBuilder.toString();

if (qName.equalsIgnoreCase(PGTUtil.DICTIONARY_SAVE_DATE)) {
blastSave = false;
Expand Down Expand Up @@ -543,7 +545,7 @@ public void endElement(String uri, String localName,
} else if (qName.equalsIgnoreCase(PGTUtil.WORD_RULEOVERRIDE_XID)) {
bwordRuleOverride = false;
} else if (qName.equalsIgnoreCase(PGTUtil.WORD_CLASS_AND_VALUE_XID)) {
String[] classValIds = stringBuilder.toString().split(",");
String[] classValIds = builderValue.split(",");
int classId = Integer.parseInt(classValIds[0]);
int valId = Integer.parseInt(classValIds[1]);
core.getWordCollection().getBufferWord().setClassValue(classId, valId);
Expand Down Expand Up @@ -866,13 +868,16 @@ public void endElement(String uri, String localName,
procMan.addSyllable(tmpString);
} else if (qName.equalsIgnoreCase(PGTUtil.PRO_GUIDE_COMPOSITION_SYLLABLE)) {
bsyllableComposition = false;
} else if (qName.equalsIgnoreCase(TranslationManager.TMP_FILENAME_XID)) {
core.getTranslationManager().setTmpFileName(builderValue);
}
}

@Override
public void characters(char[] ch, int start, int length)
throws SAXException {

stringBuilder.append(ch, start, length);

if (blastSave) {
core.setLastSaveTime(Instant.parse(new String(ch, start, length)));
} else if (blocalWord) {
Expand Down Expand Up @@ -907,7 +912,7 @@ public void characters(char[] ch, int start, int length)
.setRulesOverride(new String(ch, start, length).equals(PGTUtil.TRUE));
bwordRuleOverride = false;
} else if (bclassVal) {
stringBuilder.append(new String(ch, start, length));
// stringBuilder.append(new String(ch, start, length));
} else if (bwordClassTextVal) {
if (ruleIdBuffer == 0) {
String[] classValIds = new String(ch, start, length).split(",");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.darisadesigns.polyglotlina.ManagersCollections.ImageCollection;
import org.darisadesigns.polyglotlina.Desktop.ManagersCollections.DesktopOptionsManager;
import org.darisadesigns.polyglotlina.ManagersCollections.ReversionManager;
import org.darisadesigns.polyglotlina.ManagersCollections.TranslationManager;
import org.darisadesigns.polyglotlina.Nodes.ImageNode;
import org.darisadesigns.polyglotlina.Nodes.ReversionNode;
import java.awt.Desktop;
Expand Down Expand Up @@ -530,6 +531,7 @@ private String writeRawFileOutput(File tmpSaveLocation, byte[] xmlData, DictCore
writeLog += writeImagesToArchive(out, core);
writeLog += writeWavToArchive(out, core);
writeLog += writePriorStatesToArchive(out, core);
writeLog += writeTranslationDBToArchive(out, core);

out.finish();
}
Expand Down Expand Up @@ -733,6 +735,24 @@ private String writeImagesToArchive(ZipOutputStream out, DictCore core) {
return writeLog;
}

private String writeTranslationDBToArchive(ZipOutputStream out, DictCore core) {
String writeLog = "";
String tmpFileName = core.getTranslationManager().getTmpFileName();
File dir = core.getOSHandler().getWorkingDirectory();
String dbPath = "";
try {
dbPath = dir.getCanonicalPath() + File.separator + tmpFileName;
out.putNextEntry(new ZipEntry(TranslationManager.ZIP_FILE_NAME));
try (InputStream dbInput = new FileInputStream(dbPath)) {
dbInput.transferTo(out);
}
out.closeEntry();
} catch (IOException e) {
writeLog = "Unable to save translations DB.\n";
}
return writeLog;
}

/**
* Tests whether a file at a particular location exists. Wrapped to avoid IO
* code outside this file
Expand Down Expand Up @@ -854,6 +874,24 @@ public void loadReversionStates(ReversionManager reversionManager,
}
}

@Override
public void loadTranslationDB(TranslationManager translationManager, String filePath) throws IOException {
ZipFile zipFile = new ZipFile(filePath);
try (zipFile) {
ZipEntry translationDB = zipFile.getEntry(TranslationManager.ZIP_FILE_NAME);

if (null == translationDB) return;

File dir = translationManager.getCore().getOSHandler().getWorkingDirectory();
String dbPath = dir.getCanonicalPath() + File.separator + translationManager.getTmpFileName();
OutputStream tmpFileStream = new FileOutputStream(dbPath);
try (tmpFileStream) {
zipFile.getInputStream(translationDB).transferTo(tmpFileStream);
translationManager.setIsInitialized(true);
}
}
}

/**
* Exports font in PGD to external file
*
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/darisadesigns/polyglotlina/DictCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.darisadesigns.polyglotlina.ManagersCollections.ReversionManager;
import org.darisadesigns.polyglotlina.ManagersCollections.RomanizationManager;
import org.darisadesigns.polyglotlina.ManagersCollections.ToDoManager;
import org.darisadesigns.polyglotlina.ManagersCollections.TranslationManager;
import org.darisadesigns.polyglotlina.ManagersCollections.WordClassCollection;
import org.darisadesigns.polyglotlina.OSHandler.CoreUpdatedListener;
import org.darisadesigns.polyglotlina.OSHandler.FileReadListener;
Expand Down Expand Up @@ -75,6 +76,7 @@ public class DictCore {
private ReversionManager reversionManager;
private PhraseManager phraseManager;
private ToDoManager toDoManager;
private TranslationManager translationManager;
private final OSHandler osHandler;
private boolean curLoading = false;
private Instant lastSaveTime = Instant.MIN;
Expand Down Expand Up @@ -115,6 +117,7 @@ private void initializeDictCore(PropertiesManager _propertiesManager, GrammarMan
reversionManager = new ReversionManager(this);
toDoManager = new ToDoManager();
phraseManager = new PhraseManager(this);
translationManager = new TranslationManager(this);

PAlphaMap<String, Integer> alphaOrder = propertiesManager.getAlphaOrder();
subscribers = new ArrayList<>();
Expand Down Expand Up @@ -408,6 +411,13 @@ public void readFile(String _fileName, byte[] overrideXML, boolean useFileReadLi
warningLog += e.getLocalizedMessage() + "\n";
}

try {
this.osHandler.getIOHandler().loadTranslationDB(translationManager, _fileName);
} catch (IOException e) {
this.osHandler.getIOHandler().writeErrorLog(e);
warningLog += e.getLocalizedMessage() + "\n";
}

curLoading = false;

if (!errorLog.trim().isEmpty()) {
Expand Down Expand Up @@ -523,6 +533,7 @@ public void writeFile(String _fileName, boolean writeToReversionMgr)
grammarManager.writeXML(doc, rootElement);
toDoManager.writeXML(doc, rootElement);
phraseManager.writeXML(doc, rootElement);
translationManager.writeXML(doc, rootElement);

// write family entries
rootElement.appendChild(famManager.writeToSaveXML(doc));
Expand Down Expand Up @@ -609,6 +620,10 @@ public ToDoManager getToDoManager() {
public PhraseManager getPhraseManager() {
return phraseManager;
}

public TranslationManager getTranslationManager() {
return this.translationManager;
}

/**
* Returns DictCore OS handler
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/org/darisadesigns/polyglotlina/IOHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.darisadesigns.polyglotlina.ManagersCollections.LogoCollection;
import org.darisadesigns.polyglotlina.ManagersCollections.ImageCollection;
import org.darisadesigns.polyglotlina.ManagersCollections.ReversionManager;
import org.darisadesigns.polyglotlina.ManagersCollections.TranslationManager;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
Expand Down Expand Up @@ -204,8 +206,16 @@ public void loadLogographs(LogoCollection logoCollection,
* @param fileName full path of polyglot archive
* @throws IOException on read error
*/
public void loadReversionStates(ReversionManager reversionManager,
String fileName) throws IOException;
public void loadReversionStates(ReversionManager reversionManager, String fileName) throws IOException;

/**
* Loads SQLite DB file from a PolyGlot file.
*
* @param translationManager Translation manager to retrieve settings of DB file.
* @param filePath full path of polyglot archive
* @throws IOException on read error
*/
public void loadTranslationDB(TranslationManager translationManager, String filePath) throws IOException;

/**
* Exports font in PGD to external file
Expand Down
Loading