Implement a part of the "info" command (A5)
Bug: 7388857 Change-Id: Id1bba7a33ab6195d5711bebb1df5c18c9c08925c
This commit is contained in:
parent
3862b9e819
commit
f1d35ac5dc
2 changed files with 73 additions and 3 deletions
|
@ -39,8 +39,8 @@ public final class BinaryDictOffdeviceUtils {
|
|||
private final static String PREFIX = "dicttool";
|
||||
private final static String SUFFIX = ".tmp";
|
||||
|
||||
public final static String COMPRESSION = "compression";
|
||||
public final static String ENCRYPTION = "encryption";
|
||||
public final static String COMPRESSION = "compressed";
|
||||
public final static String ENCRYPTION = "encrypted";
|
||||
|
||||
public static class DecoderChainSpec {
|
||||
ArrayList<String> mDecoderSpec = new ArrayList<String>();
|
||||
|
@ -49,6 +49,14 @@ public final class BinaryDictOffdeviceUtils {
|
|||
mDecoderSpec.add(stepDescription);
|
||||
return this;
|
||||
}
|
||||
public String describeChain() {
|
||||
final StringBuilder s = new StringBuilder("raw");
|
||||
for (final String step : mDecoderSpec) {
|
||||
s.append(" > ");
|
||||
s.append(step);
|
||||
}
|
||||
return s.toString();
|
||||
}
|
||||
}
|
||||
|
||||
public static void copy(final InputStream input, final OutputStream output) throws IOException {
|
||||
|
|
|
@ -16,6 +16,22 @@
|
|||
|
||||
package com.android.inputmethod.latin.dicttool;
|
||||
|
||||
import com.android.inputmethod.latin.dicttool.BinaryDictOffdeviceUtils.DecoderChainSpec;
|
||||
import com.android.inputmethod.latin.makedict.BinaryDictInputOutput;
|
||||
import com.android.inputmethod.latin.makedict.FusionDictionary;
|
||||
import com.android.inputmethod.latin.makedict.UnsupportedFormatException;
|
||||
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
|
||||
public class Info extends Dicttool.Command {
|
||||
public static final String COMMAND = "info";
|
||||
|
||||
|
@ -26,11 +42,57 @@ public class Info extends Dicttool.Command {
|
|||
return "info <filename>: prints various information about a dictionary file";
|
||||
}
|
||||
|
||||
private static void crash(final String filename, final Exception e) {
|
||||
throw new RuntimeException("Can't read file " + filename, e);
|
||||
}
|
||||
|
||||
private static FusionDictionary getDictionary(final String filename) {
|
||||
final File file = new File(filename);
|
||||
System.out.println("Dictionary : " + file.getAbsolutePath());
|
||||
System.out.println("Size : " + file.length() + " bytes");
|
||||
try {
|
||||
if (XmlDictInputOutput.isXmlUnigramDictionary(filename)) {
|
||||
System.out.println("Format : XML unigram list");
|
||||
return XmlDictInputOutput.readDictionaryXml(
|
||||
new BufferedInputStream(new FileInputStream(file)),
|
||||
null /* shortcuts */, null /* bigrams */);
|
||||
} else if (CombinedInputOutput.isCombinedDictionary(filename)) {
|
||||
System.out.println("Format : Combined format");
|
||||
return CombinedInputOutput.readDictionaryCombined(
|
||||
new BufferedInputStream(new FileInputStream(file)));
|
||||
} else {
|
||||
final DecoderChainSpec decodedSpec =
|
||||
BinaryDictOffdeviceUtils.getRawBinaryDictionaryOrNull(file);
|
||||
if (null == decodedSpec) {
|
||||
crash(filename, new RuntimeException(
|
||||
filename + " does not seem to be a dictionary file"));
|
||||
}
|
||||
final FileInputStream inStream = new FileInputStream(decodedSpec.mFile);
|
||||
final ByteBuffer buffer = inStream.getChannel().map(
|
||||
FileChannel.MapMode.READ_ONLY, 0, decodedSpec.mFile.length());
|
||||
System.out.println("Format : Binary dictionary format");
|
||||
System.out.println("Packaging : " + decodedSpec.describeChain());
|
||||
return BinaryDictInputOutput.readDictionaryBinary(
|
||||
new BinaryDictInputOutput.ByteBufferWrapper(buffer), null);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
crash(filename, e);
|
||||
} catch (SAXException e) {
|
||||
crash(filename, e);
|
||||
} catch (ParserConfigurationException e) {
|
||||
crash(filename, e);
|
||||
} catch (UnsupportedFormatException e) {
|
||||
crash(filename, e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
// TODO: implement this
|
||||
if (mArgs.length < 1) {
|
||||
throw new RuntimeException("Not enough arguments for command " + COMMAND);
|
||||
}
|
||||
System.out.println("Not implemented yet");
|
||||
final String filename = mArgs[0];
|
||||
final FusionDictionary dict = getDictionary(filename);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue