Skip to content

PurlingNayuki/XML2MDict

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

XML2MDict

A Java Implement of More Than a XML to MDict Source Converter


Commandline Utility Usage

To convert a single xml file, just run

java -jar xml2mdict.jar --from OxfordXML --to MDict file.xml

If a bunch of files are to be converted, place them in a directory and run

java -jar xml2mdict.jar --from OxfordXML --to MDict file1.xml file2.xml ...

or

java -jar --from OxfordXML --to MDict xml2mdict.jar directory

The --from and --to options are used to specify the input format and output format to determine which provider and converter should be called.

If you need to assign css file, use -c or --css option:

java -jar --from OxfordXML --to MDict --css style.css xml2mdict.jar file.xml

Java Library Usage

A Java library is provided to read xml files and convert them to MDict source. The XMLSource class is extendable and may need to be modified to fit situations.

Convert Using Existing Rules

With the library added, you may create a XMLSource instance and use toMDictSource() method to convert:

import com.purlingnayuki.util.xml2mdict.Converter.MDictConverter;
import com.purlingnayuki.util.xml2mdict.Provider.OxfordCD.OxfordXMLSource;
import com.purlingnayuki.util.xml2mdict.datatype.Converter;
// ....
File[] files = new File[size];
// ....
Provider provider = new OxfordXMLSource(files, true);
Converter converter = new MDictConverter(provider);
String resultWithCss = converter.setParameter("css", youCssString).convert(); // if you need to assign css file;
String result = converter.convert(); // if no need to assign css file

Note that the Provider class won't handle directory, at least for now. If need to handle directories, add the files in them to the ArrayList:

// ....
if (in.isDirectory())
    for (String fn: in.list()) {
        File file = new File(fn);
        if (!file.isDirectory())
            arrayList.add(file);
    }
// ....

Convert by Original Rules

To create your own rules, you need to extends or implement one of or both Provider class and Converter interface. You need to implement method below:

public abstract class Provider {
    public abstract ArrayList<Element> getHeadwords(); // to get all headwords from ArrayList<File>
    public abstract String getContent(Element elem); // to get content body corresponds to input Element
}
public interface Converter {
    ArrayList<String> convert() throws DocumentException; // to generate formatted content from all input files
    Converter setParameter(String name, String value); // to specify parameter of this converter, for example to control output format
}

Once you complete the job, you can call your Provider and Converter:

File[] files;
// ....
Provider provider = new myProvider(files);
Converter converter = new myConverter(provider);
String result = converter.setParameter("setSomeParameters", "ifNeeded").converter;

If you create a new Provider or Converter, a pull request is always welcome.

About

A Java Implement of XML to more than MDict Dictionary Source Converter.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published