Search in sources :

Example 1 with SourceCrawlerXMLFolder

use of org.pentaho.di.ui.i18n.SourceCrawlerXMLFolder in project pentaho-kettle by pentaho.

the class Translator2 method loadConfiguration.

public void loadConfiguration(String configFile, String sourceFolder) throws Exception {
    // What are the locale to handle?
    // 
    localeList = new ArrayList<String>();
    rootDirectories = new ArrayList<String>();
    filesToAvoid = new ArrayList<String>();
    xmlFolders = new ArrayList<SourceCrawlerXMLFolder>();
    FileObject file = KettleVFS.getFileObject(configFile);
    if (file.exists()) {
        try {
            Document doc = XMLHandler.loadXMLFile(file);
            Node configNode = XMLHandler.getSubNode(doc, "translator-config");
            referenceLocale = XMLHandler.getTagValue(configNode, "reference-locale");
            singleMessagesFile = XMLHandler.getTagValue(configNode, "single-messages-file");
            Node localeListNode = XMLHandler.getSubNode(configNode, "locale-list");
            int nrLocale = XMLHandler.countNodes(localeListNode, "locale");
            if (nrLocale > 0) {
                localeList.clear();
            }
            for (int i = 0; i < nrLocale; i++) {
                Node localeNode = XMLHandler.getSubNodeByNr(localeListNode, "locale", i);
                String locale = XMLHandler.getTagValue(localeNode, "code");
                localeList.add(locale);
            }
            Node phrasesNode = XMLHandler.getSubNode(configNode, "scan-phrases");
            int nrPhrases = XMLHandler.countNodes(phrasesNode, "scan-phrase");
            scanPhrases = new String[nrPhrases];
            for (int i = 0; i < nrPhrases; i++) {
                Node phraseNode = XMLHandler.getSubNodeByNr(phrasesNode, "scan-phrase", i);
                scanPhrases[i] = XMLHandler.getNodeValue(phraseNode);
            }
            Node rootsNode = XMLHandler.getSubNode(configNode, "source-directories");
            int nrRoots = XMLHandler.countNodes(rootsNode, "root");
            if (nrRoots > 0) {
                rootDirectories.clear();
            }
            for (int i = 0; i < nrRoots; i++) {
                Node rootNode = XMLHandler.getSubNodeByNr(rootsNode, "root", i);
                String directory = XMLHandler.getNodeValue(rootNode);
                directory = sourceFolder + File.separator + directory;
                rootDirectories.add(directory);
            }
            Node filesNode = XMLHandler.getSubNode(configNode, "files-to-avoid");
            int nrFiles = XMLHandler.countNodes(filesNode, "filename");
            if (nrFiles > 0) {
                filesToAvoid.clear();
            }
            for (int i = 0; i < nrFiles; i++) {
                Node fileNode = XMLHandler.getSubNodeByNr(filesNode, "filename", i);
                String filename = XMLHandler.getNodeValue(fileNode);
                filesToAvoid.add(filename);
            }
            Node foldersToScanNode = XMLHandler.getSubNode(configNode, "xml-folders-to-scan");
            int nrFolders = XMLHandler.countNodes(foldersToScanNode, "xml-folder-to-scan");
            if (nrFolders > 0) {
                xmlFolders.clear();
            }
            for (int i = 0; i < nrFolders; i++) {
                Node folderToScanNode = XMLHandler.getSubNodeByNr(foldersToScanNode, "xml-folder-to-scan", i);
                String folderName = sourceFolder + File.separator + XMLHandler.getTagValue(folderToScanNode, "folder");
                String wildcard = XMLHandler.getTagValue(folderToScanNode, "wildcard");
                String keyPrefix = XMLHandler.getTagValue(folderToScanNode, "key-prefix");
                SourceCrawlerXMLFolder xmlFolder = new SourceCrawlerXMLFolder(folderName, wildcard, keyPrefix);
                Node elementsNode = XMLHandler.getSubNode(folderToScanNode, "elements-to-scan");
                int nrElements = XMLHandler.countNodes(elementsNode, "element-to-scan");
                for (int j = 0; j < nrElements; j++) {
                    Node elementNode = XMLHandler.getSubNodeByNr(elementsNode, "element-to-scan", j);
                    String element = XMLHandler.getTagValue(elementNode, "element");
                    String tag = XMLHandler.getTagValue(elementNode, "tag");
                    String attribute = XMLHandler.getTagValue(elementNode, "attribute");
                    xmlFolder.getElements().add(new SourceCrawlerXMLElement(element, tag, attribute));
                }
                String defaultPackage = XMLHandler.getTagValue(folderToScanNode, "package-default");
                xmlFolder.setDefaultPackage(defaultPackage);
                String defaultSourceFolder = XMLHandler.getTagValue(folderToScanNode, "package-source-folder");
                xmlFolder.setDefaultSourceFolder(sourceFolder + File.separator + defaultSourceFolder);
                Node packageExceptionsNode = XMLHandler.getSubNode(folderToScanNode, "package-exceptions");
                int nrExceptions = XMLHandler.countNodes(packageExceptionsNode, "package-exception");
                for (int j = 0; j < nrExceptions; j++) {
                    Node packageExceptionNode = XMLHandler.getSubNodeByNr(packageExceptionsNode, "package-exception", j);
                    String startsWith = XMLHandler.getTagValue(packageExceptionNode, "starts-with");
                    String packageName = XMLHandler.getTagValue(packageExceptionNode, "package");
                    xmlFolder.getPackageExceptions().add(new SourceCrawlerPackageException(startsWith, packageName));
                }
                xmlFolders.add(xmlFolder);
            }
            System.out.println(BaseMessages.getString(PKG, "i18n.Log.XMLFoldersToScan", xmlFolders.size()));
            for (SourceCrawlerXMLFolder xmlFolder : xmlFolders) {
                System.out.println(BaseMessages.getString(PKG, "i18n.Log.Folder", xmlFolder.getFolder(), xmlFolder.getWildcard(), xmlFolder.getElements().size()));
            }
        } catch (Exception e) {
            log.logError("Translator", "Error reading translator.xml", e);
        }
    }
}
Also used : SourceCrawlerPackageException(org.pentaho.di.ui.i18n.SourceCrawlerPackageException) Node(org.w3c.dom.Node) SourceCrawlerXMLFolder(org.pentaho.di.ui.i18n.SourceCrawlerXMLFolder) FileObject(org.apache.commons.vfs2.FileObject) Document(org.w3c.dom.Document) SourceCrawlerXMLElement(org.pentaho.di.ui.i18n.SourceCrawlerXMLElement) KettleFileException(org.pentaho.di.core.exception.KettleFileException) KettleException(org.pentaho.di.core.exception.KettleException) SourceCrawlerPackageException(org.pentaho.di.ui.i18n.SourceCrawlerPackageException)

Aggregations

FileObject (org.apache.commons.vfs2.FileObject)1 KettleException (org.pentaho.di.core.exception.KettleException)1 KettleFileException (org.pentaho.di.core.exception.KettleFileException)1 SourceCrawlerPackageException (org.pentaho.di.ui.i18n.SourceCrawlerPackageException)1 SourceCrawlerXMLElement (org.pentaho.di.ui.i18n.SourceCrawlerXMLElement)1 SourceCrawlerXMLFolder (org.pentaho.di.ui.i18n.SourceCrawlerXMLFolder)1 Document (org.w3c.dom.Document)1 Node (org.w3c.dom.Node)1