FBReader Code Analysis: When creating Bookmodel, how to get the corresponding Formatplugin according to book?

Source: Internet
Author: User

This article is only for the moment I look at the code process memo records, not detailed collation, the organization is not too clear.

Bookmodel-Createmodel (book book);

Getplugin (Zlfile), book-Getplugin (), is actually obtained by the Plugincollection class:

Plugincollection, Getplugin (Zlfile), Getplugin (file, FormatPlugin.Type.ANY)

   Formatplugin getplugin (zlfile file, Formatplugin.type formattype) {final FileType FileType  = FileTypeCollection.Instance.typeForFile (file);        Final Formatplugin plugin  = Getplugin (FileType, formattype);  if  (plugin! = null  && Plugin.type () == FormatPlugin.Type.EXTERNAL) { return  File = = File.getphysicalfile ()?        Plugin: null   return   plugin; }

The Filetypecollection class initializes all acceptable file types:filetypecollection

/** * Store the value for key,filetype with filetype ID */private final TREEMAP<String, FileType>Mytypes = new TreeMap<String, FileType>();        Private Filetypecollection () {AddType (New FileTypeFB2 ());        AddType (New Filetypeepub ());        AddType (New Filetypemobipocket ());        AddType (New filetypehtml ());        AddType (New Simplefiletype ("Plain text", "TXT", mimetype.types_txt));        AddType (New Simplefiletype ("RTF", "RTF", Mimetype.types_rtf));        AddType (New Simplefiletype ("PDF", "PDF", mimetype.types_pdf));        AddType (New Filetypedjvu ());        AddType (New Simplefiletype ("Zip archive", "Zip", collections.singletonlist (Mimetype.app_zip)));    AddType (New Simplefiletype ("MS Word document", "Doc", Mimetype.types_doc)); The private void AddType (FileType type) {mytypes.put (type).    Id.tolowercase (), type); } public Collection<FileType>types () {return mytypes.values (); }    

These acceptable sets of file types include:

table 1: Supported filetype
FileType Corresponding ID The actual accepted file suffix (acceptsfile) Mimetypes
FileTypeFB2 Fb2 . FB2,. Fb2.zip Application/x-fictionbook, Application/x-fictionbook+xml, Application/fb2+xml, Text/fb2+xml, Application/fb2+zip
Filetypeepub EPub epub, Oebzip, Opf Application/epub+zip, Application/epub
Filetypemobipocket Mobipocket PDB, PRC, Mobi, AZW3 Application/x-mobipocket-ebook
Filetypehtml Html HTML, HTM Text/html, application/html, application/html+htm
Filetypedjvu DjVu DjVu, DJV Image/vnd.djvu, Image/x-djvu, Application/djvu
Simplefiletype Plain text Txt Text/plain, Application/txt
Simplefiletype Rtf Rtf Application/rtf, Text/rtf
Simplefiletype Pdf Pdf Application/pdf
Simplefiletype ZIP Archive Zip Application/zip
Simplefiletype MS Word Document Doc Application/msword, Application/doc

The FileType object supported by the Zlfile can be obtained according to the incoming zlfile: filetypecollection- typeforfile(zlfile file)

 Public FileType typeforfile (zlfile file)    {        for  (FileType type:types ()) {            if  (type.acceptsfile (file)) {                 return  type;            }        }         return NULL ;    }

The actual method of obtaining Formatplugin Plugincollection, Getplugin (FileType FileType, Formatplugin.type formattype)

 Publicformatplugin Getplugin (FileType FileType, Formatplugin.type formattype) {if(FileType = =NULL|| Formattype = =NULL) {            return NULL; }        Switch(formattype) { CaseAny : {formatplugin p = getplugin (FileType, FormatPlugin.Type.NATIVE);// Preferential access to native Formatplugin 
                if (p = = null) {//loading Java formatplugin
                  when not loaded    p = Getplugin (FileType, FormatPlugin.Type.JAVA);
                }
                if (p = = null) {//cannot be loaded again, Load extensions for Formatplugin
                    p = Getplugin (FileType, FormatPlugin.Type.EXTERNAL);
                }
                return p; } default: { Finallist<formatplugin> list =Myplugins.get (formattype); if(List = =NULL) { return NULL; } for(Formatplugin p:list) {
P.supportedfiletype () actually returns the Myfiletype that was passed in when the Formatplugin was initialized (see table 2);
The ID value of the filetype (viewed from table 1) will be matched on the myfiletype of formatplugin , matching the upper edge and returning the corresponding formatplugin if(FileType.Id.equalsIgnoreCase (P.supportedfiletype ())) {returnp; } } return NULL; } } }

1. First enter any by default, recursively enter again, carry formattype for FormatPlugin.Type.NATIVE , and then go to default.
2. Then it's up to the formattype to get list<formatplugin>. Now you need to know how the Myplugins is initialized?
// ======================= The following code is the Myplugins initialization process =======================
Private final map<formatplugin.type,list<formatplugin>> myplugins =
New Hashmap<formatplugin.type,list<formatplugin>> ();

Public Staticplugincollection Instance () {if(Ourinstance = =NULL) {
All supported Formatplugin instances are initialized here
1.MobipocketPlugin-FormatPlugin.Type.Java
// 2.Djvuplugin -FormatPlugin.Type.EXTERNAL
3. pdfplugin - FormatPlugin.Type.EXTERNAL
Ourinstance=Newplugincollection ();
       //This code can isn't moved to constructor because Nativeplugins () is a native method for(Nativeformatplugin p:ourinstance.nativeplugins ()) {ourinstance.addplugin (P); System.err.println ("Native plugin:" +p); } } returnourinstance; }Privateplugincollection () {Addplugin (NewMobipocketplugin ()); if(Build.VERSION.SDK_INT >= 8) {Addplugin (NewDjvuplugin ()); Addplugin (NewPdfplugin ()); } }Private voidaddplugin (Formatplugin plugin) {FinalFormatplugin.type Type =Plugin.type (); List<FormatPlugin> list =myplugins.get (type); if(List = =NULL) {List=NewArraylist<formatplugin>(); Myplugins.put (type, list); } list.add (plugin); }

Private void Nativeformatplugin[] Nativeplugins ();
Myplugins Stores all formatplugin of the corresponding Formatplugin.type with formatplugin.type as key.

All types of Formatplugin.type : Formatplugin, enum type:
 Public enum Type {Any        ,        JAVA,        NATIVE,        EXTERNAL;    };
Table 2: Supported Formatplugin
Some Formatplugin sub-class The Myfiletype of this class Its corresponding parent class The type of the class
Builtinformatplugin (abstract) Formatplugin (abstract)
Javaformatplugin (abstract) Builtinformatplugin (abstract) Type.java
Nativeformatplugin Builtinformatplugin (abstract) Type.native
Fb2nativeplugin Fb2 Nativeformatplugin Type.native
Oebnativeplugin EPub Nativeformatplugin Type.native
Mobipocketplugin Mobipocket Javaformatplugin (abstract) Type.java
Externalformatplugin (abstract) Formatplugin (abstract) Type.external
Djvuplugin DjVu Externalformatplugin (abstract) Type.external
Pdfplugin Pdf Externalformatplugin (abstract) Type.external

Open an EPUB resource example.epub, when the breakpoint enters, the following is the value of all filetype:

{DjVu=[email protected], epub=[email protected], FB2=[email protected], html= [email protected], Mobipocket=[email protected]a4e8, MS Word Document=Simplefiletype [MS Word Document], PDF=Simplefiletype [PDF], plain text=Simplefiletype [plain text], rtf=  Simplefiletype [RTF], zip archive=Simplefiletype [zip archive]}

Here are the values for all Formatplugin:

It can be seen that there are six types of formatplugin currently supported.
Therefore, the example.epub resource above, the corresponding filetype is epub=org.geometerplus.zlibrary.core.filetypes.filetypeepub[epub], Formatplugin is oebnativeplugin[epub].



  

FBReader Code Analysis: When creating Bookmodel, how to get the corresponding Formatplugin according to book?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.