View Javadoc

1   package pl.psnc.dl.ege.tei;
2   
3   import java.io.File;
4   import java.util.ArrayList;
5   import java.util.List;
6   
7   import pl.psnc.dl.ege.types.ConversionActionArguments;
8   import pl.psnc.dl.ege.types.DataType;
9   
10  final class ConverterConfiguration
11  {
12  
13  	public static final String DEFAULT_PROFILE = "default";
14  
15  	public static final String PROFILE_NOT_FOUND_MSG = "Profile not found, setting default profile...";
16  
17  	public static final String SLASH = "/";
18  
19  	public static final String TEI = "TEI";
20  
21  	public static final String XML_MIME = "text/xml";
22  	
23  	public static final String ZIP_MIME = "application/zip";
24  	
25  	public static final String PATH;
26  
27  	public static final String STYLESHEETS_PATH;
28  
29  	public static final List<ConversionActionArguments> CONVERSIONS = new ArrayList<ConversionActionArguments>();
30  
31  	public static final String PROFILE_KEY = "pl.psnc.dl.ege.tei.profileNames";
32  
33  	static {
34  		String pref = TEIConverter.class.getProtectionDomain().getCodeSource()
35  				.getLocation().getPath();
36  		PATH = pref.substring(0, pref.lastIndexOf(SLASH));
37  		STYLESHEETS_PATH = PATH + File.separator + "tei-config"
38  				+ File.separator + "stylesheets" + File.separator;
39  		File basePath = new File(PATH + File.separator + "tei-config"
40  				+ File.separator + "stylesheets" + File.separator + "profiles"
41  				+ File.separator);
42  		if (basePath.exists()) {
43  			for (Format format : Format.values()) {
44  				StringBuffer sbParams = new StringBuffer();
45  				sbParams
46  						.append("<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">");
47  				sbParams.append("<properties>");
48  				sbParams.append("<entry key=\"");
49  				sbParams.append(PROFILE_KEY);
50  				sbParams.append("\">");
51  				for (String profileName : basePath.list()) {
52  					File profileDir = new File(basePath + File.separator
53  							+ profileName + File.separator);
54  					for (String profConv : profileDir.list()) {
55  						if (profConv.equals(format.getProfile())) {
56  							sbParams.append(profileName);
57  							sbParams.append(",");
58  						}
59  					}
60  				}
61  				sbParams.deleteCharAt(sbParams.length() - 1);
62  				sbParams.append("</entry><entry key=\"" + PROFILE_KEY
63  						+ ".type\">array</entry></properties>");
64  				ConversionActionArguments caa = new ConversionActionArguments(
65  						new DataType(TEI, XML_MIME), new DataType(format
66  								.getFormatName(), format.getMimeType()),
67  						sbParams.toString());
68  				CONVERSIONS.add(caa);
69  				if (format.equals(Format.DOCX)) {
70  					ConversionActionArguments caa2 = new ConversionActionArguments(
71  							new DataType(format.getFormatName(), format
72  									.getMimeType()),
73  							new DataType(TEI, XML_MIME), sbParams.toString());
74  					CONVERSIONS.add(caa2);
75  				}
76  
77  			}
78  		}
79  		else {
80  			throw new RuntimeException();
81  		}
82  	}
83  
84  
85  	/**
86  	 * Check if profile of conversion for chosen formatId exists.
87  	 * Returns boolean value of 'true' if profile was found.
88  	 * 
89  	 * @param profileName
90  	 * @param formatId
91  	 * @return
92  	 */
93  	public static boolean checkProfile(String profileName, String formatId)
94  	{
95  		if (profileName != null && profileName.equals("")
96  				|| profileName == null) {
97  			return false;
98  		}
99  		File profile = new File(STYLESHEETS_PATH + File.separator + "profiles"
100 				+ File.separator + profileName + File.separator + formatId
101 				+ File.separator);
102 		return profile.exists();
103 	}
104 
105 
106 	private ConverterConfiguration()
107 	{
108 
109 	}
110 
111 }