1 package org.coolreader.crengine;
2 
3 import org.coolreader.R;
4 
5 public enum DocumentFormat {
6 	/// lvtinydom.h: source document formats
7 	// Add new types of formats only at the end of this enum to save the correct format number in the history file/database!
8 	//typedef enum {
9 	NONE("fb2.css", R.raw.fb2, R.drawable.cr3_browser_book, false, false, 0,
10 			new String[] {},
11 			new String[] {}),// doc_format_none,
12 	FB2("fb2.css", R.raw.fb2, R.drawable.cr3_browser_book_fb2, true, true, 12,
13 			new String[] {".fb2", ".fb2.zip"},
14 			new String[] {"application/fb2+zip"}), // doc_format_fb2,
15 	FB3("fb3.css", R.raw.fb3, R.drawable.cr3_browser_book_fb3, true, true, 11,
16 			new String[] {".fb3" },
17 			new String[] {"application/fb3"}), // doc_format_fb3,
18 	TXT("txt.css", R.raw.txt, R.drawable.cr3_browser_book_txt, false, false, 3,
19 			new String[] {".txt", ".tcr", ".pml"},
20 			new String[] {"text/plain"}), // doc_format_txt,
21 	RTF("rtf.css", R.raw.rtf, R.drawable.cr3_browser_book_rtf, false, false, 8,
22 			new String[] {".rtf"},
23 			new String[] {}), // doc_format_rtf,
24 	EPUB("epub.css", R.raw.epub, R.drawable.cr3_browser_book_epub, true, true, 10,
25 			new String[] {".epub"},
26 			new String[] {"application/epub+zip"}),// doc_format_epub,
27 	HTML("htm.css", R.raw.htm, R.drawable.cr3_browser_book_html, false, false, 9,
28 			new String[] {".htm", ".html", ".shtml", ".xhtml"},
29 			new String[] {"text/html"}),// doc_format_html,
30 	TXT_BOOKMARK("fb2.css", R.raw.fb2, R.drawable.cr3_browser_book_fb2, false, false, 0,
31 			new String[] {".txt.bmk"},
32 			new String[] {}), // doc_format_txt_bookmark, // coolreader TXT format bookmark
33 	CHM("chm.css", R.raw.chm, R.drawable.cr3_browser_book_chm, false, false, 4,
34 			new String[] {".chm"},
35 			new String[] {}), //  doc_format_chm,
36 	DOC("doc.css", R.raw.doc, R.drawable.cr3_browser_book_doc, false, false, 5,
37 			new String[] {".doc"},
38 			new String[] {}), // doc_format_doc,
39 	DOCX("docx.css", R.raw.docx, R.drawable.cr3_browser_book_doc, true, false, 6,
40 			new String[] {".docx"},
41 			new String[] {}), // doc_format_docx,
42 	PDB("htm.css", R.raw.htm, R.drawable.cr3_browser_book_pdb, false, true, 2,
43 			new String[] {".pdb", ".prc", ".mobi", ".azw"},
44 			new String[] {}), // doc_format_txt/html/...,
45 	ODT("docx.css", R.raw.docx, R.drawable.cr3_browser_book_odt, true, false, 7,
46 			new String[] {".odt"},
47 			new String[] {"application/vnd.oasis.opendocument.text"}), // doc_format_odt,
48 	;
49 	// don't forget update getDocFormatName() when changing this enum
50 	// Add new types of formats only at the end of this enum to save the correct format number in the history file/database!
51 	//} doc_format_t;
52 
getCssName()53 	public String getCssName()
54 	{
55 		return cssFileName;
56 	}
57 
getPriority()58 	public int getPriority()
59 	{
60 		return priority;
61 	}
62 
getExtensions()63 	public String[] getExtensions()
64 	{
65 		return extensions;
66 	}
67 
getCSSResourceId()68 	public int getCSSResourceId()
69 	{
70 		return cssResourceId;
71 	}
72 
getIconResourceId()73 	public int getIconResourceId()
74 	{
75 		return iconResourceId;
76 	}
77 
getMimeFormats()78 	public String[] getMimeFormats()
79 	{
80 		return mimeFormats;
81 	}
82 
getMimeFormat()83 	public String getMimeFormat()
84 	{
85 		return mimeFormats.length>0 ? mimeFormats[0] : null;
86 	}
87 
canParseProperties()88 	public boolean canParseProperties()
89 	{
90 		return canParseProperties;
91 	}
92 
canParseCoverpages()93 	public boolean canParseCoverpages()
94 	{
95 		return canParseCoverpages;
96 	}
97 
needCoverPageCaching()98 	public boolean needCoverPageCaching()
99 	{
100 		return this == FB2;
101 	}
102 
byId( int i )103 	public static DocumentFormat byId( int i )
104 	{
105 		if ( i>=0 && i<DocumentFormat.values().length )
106 			return values()[i];
107 		return null;
108 	}
109 
matchExtension( String filename )110 	public boolean matchExtension( String filename )
111 	{
112 		for ( String ext : extensions )
113 			if ( filename.endsWith(ext) )
114 				return true;
115 		return false;
116 	}
117 
matchMimeType( String type )118 	public boolean matchMimeType( String type )
119 	{
120 		for ( String s : mimeFormats ) {
121 			if ( type.equals(s) || type.startsWith(s+";"))
122 				return true;
123 		}
124 		return false;
125 	}
126 
byExtension( String filename )127 	public static DocumentFormat byExtension( String filename )
128 	{
129 		String s = filename.toLowerCase();
130 		for ( int i=0; i<DocumentFormat.values().length; i++ )
131 			if ( values()[i].matchExtension(s))
132 				return values()[i];
133 		return null;
134 	}
135 
byMimeType( String format )136 	public static DocumentFormat byMimeType( String format )
137 	{
138 		if ( format==null )
139 			return null;
140 		String s = format.toLowerCase();
141 		for ( int i=0; i<DocumentFormat.values().length; i++ )
142 			if ( values()[i].matchMimeType(s))
143 				return values()[i];
144 		return null;
145 	}
146 
getSupportedExtension( String filename )147 	public static String getSupportedExtension( String filename ) {
148 		String s = filename.toLowerCase();
149 		for ( int i=0; i<DocumentFormat.values().length; i++ ) {
150 			for ( String ext : values()[i].extensions ) {
151 				if (s.endsWith(ext))
152 					return ext;
153 			}
154 		}
155 		return null;
156 	}
157 
DocumentFormat( String cssFileName, int cssResourceId, int iconResourceId, boolean canParseProperties, boolean canParseCoverpages, int priority, String extensions[], String mimeFormats[] )158 	private DocumentFormat( String cssFileName, int cssResourceId, int iconResourceId, boolean canParseProperties, boolean canParseCoverpages, int priority, String extensions[], String mimeFormats[] )
159 	{
160 		this.cssFileName = cssFileName;
161 		this.cssResourceId = cssResourceId;
162 		this.iconResourceId = iconResourceId;
163 		this.extensions = extensions;
164 		this.canParseProperties = canParseProperties;
165 		this.canParseCoverpages = canParseCoverpages;
166 		this.mimeFormats = mimeFormats;
167 		this.priority = priority;
168 	}
169 	final private String cssFileName;
170 	final private int cssResourceId;
171 	final private int iconResourceId;
172 	final private String[] extensions;
173 	final boolean canParseProperties;
174 	final boolean canParseCoverpages;
175 	final private String[] mimeFormats;
176 	final private int priority;
177 }
178