org.openxma.hbmfilemerge.HbmFileMergeInput Maven / Gradle / Ivy
package org.openxma.hbmfilemerge;
import java.io.File;
import java.io.IOException;
public class HbmFileMergeInput
{
private File resourcesFolder, resourcesGenFolder;
public File getResourcesFolder() {
return resourcesFolder;
}
public File getResourcesGenFolder() {
return resourcesGenFolder;
}
private HbmFileMergeInput(File resourcesFolder, File resourcesGenFolder) {
this.resourcesFolder = resourcesFolder;
this.resourcesGenFolder = resourcesGenFolder;
}
public static HbmFileMergeInput getInstance(File resourcesFolder, File resourcesGenFolder) {
if (null == resourcesFolder || null == resourcesGenFolder)
throw new IllegalArgumentException("missing resource folders");
return new HbmFileMergeInput(resourcesFolder, resourcesGenFolder);
}
public static boolean isGenHbmFile(String fileName) {
return (null != fileName && fileName.endsWith(HbmFileNameSuffix.HBM_GEN_XML.toString())) ? true : false;
}
public static boolean isFragmentHbmFile(String fileName) {
return (null != fileName && fileName.endsWith(HbmFileNameSuffix.HBM_FRAGMENT_XML.toString())) ? true : false;
}
/**
* @param fragmentHbm
* the input *.hbm.fragment.xml file
* @return a Map
with the Merge target XML as key and the xml
* fragment as value
* @throws IOException
* @since version_number
* @author s4676
*/
public HbmFilePair getMergeInputByFragmentHbm(File fragmentHbm) throws IOException {
if (null == fragmentHbm || !isFragmentHbmFile(fragmentHbm.getName()))
return null;
return createMergeTargetAndFragmentPairsByFragmentHbm(fragmentHbm);
}
public HbmFilePair getMergeInputByGenHbm(File genHbm) throws IOException {
if (null == genHbm || !isGenHbmFile(genHbm.getName()))
return null;
return createMergeTargetAndFragmentPairsByGenHbm(genHbm);
}
private final HbmFilePair createMergeTargetAndFragmentPairsByFragmentHbm(File fragmentHbm) throws IOException {
return HbmFilePair.valueOf(getGenHbm(fragmentHbm), fragmentHbm);
}
private final HbmFilePair createMergeTargetAndFragmentPairsByGenHbm(File genHbm) throws IOException {
return HbmFilePair.valueOf(genHbm, getFragmentHbm(genHbm));
}
private File getGenHbm(File fragmentHbm) throws IOException {
File genHbm = new File(createGenHbmUrl(fragmentHbm.getAbsolutePath()));
if (genHbm.exists() && genHbm.isFile())
return genHbm;
return null;
}
private File getFragmentHbm(File genHbm) throws IOException {
File fragmentHbm = new File(createFragmentHbmUrl(genHbm.getAbsolutePath()));
if (fragmentHbm.exists() && fragmentHbm.isFile())
return fragmentHbm;
return null;
}
private String createGenHbmUrl(String fragmentHbmUrl) {
// shift to genHbm resource folder
String genHbmUrl = fragmentHbmUrl.replace(resourcesFolder.getPath(), resourcesGenFolder.getPath());
// shift to genHbm file suffix
return genHbmUrl.replace(HbmFileNameSuffix.HBM_FRAGMENT_XML.toString(), HbmFileNameSuffix.HBM_GEN_XML
.toString());
}
private String createFragmentHbmUrl(String genHbmUrl) {
// shift to fragmentHbm resource folder
String fragmentHbmUrl = genHbmUrl.replace(resourcesGenFolder.getPath(), resourcesFolder.getPath());
// shift to fragmentHbm file suffix
return fragmentHbmUrl.replace(HbmFileNameSuffix.HBM_GEN_XML.toString(), HbmFileNameSuffix.HBM_FRAGMENT_XML
.toString());
}
}