All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.asteriskjava.config.dialplan.ExtensionsConfigFileReader Maven / Gradle / Ivy

There is a newer version: 3.39.0
Show newest version
package org.asteriskjava.config.dialplan;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.asteriskjava.config.ConfigElement;
import org.asteriskjava.config.ConfigFileReader;
import org.asteriskjava.config.ConfigParseException;
import org.asteriskjava.config.ConfigVariable;

/*
 *   Interprets extensions.conf as a special kind of config file, the dialplan.
 *   - Line numbers correspond with pbx_config.c, tags/1.4.19 revision 96024
 */
public class ExtensionsConfigFileReader extends ConfigFileReader
{
    /*
     * This method corresponds to an iteration of the loop at line 2212 Notes:
     *      1. [general] and [globals] are allowed to be a context here if they contain only configvariables
     *      2. switch and ignorepat are treated like regular ConfigVariable.
     */
    @Override
    protected ConfigElement processTextLine(String configfile, int lineno, String line) throws ConfigParseException
    {
        ConfigElement configElement;
        
        if(
                (line.trim().startsWith("exten") || line.trim().startsWith("include")) &&
                currentCategory != null && 
                (currentCategory.getName().equals("general") || currentCategory.getName().equals("globals"))
        )
            throw new ConfigParseException(configfile, lineno, "cannot have 'exten' or 'include' in global or general sections");

        
        /*
         * Goal here is to break out anything unique that we might want to
         * look at and parse separately. For now, only exten and include fit
         * that criteria. Eventually, I could see parsing sections for things
         * from macros, contexts to differentiate them from categories, switch
         * for realtime, and more. 
         */
        if (line.trim().startsWith("exten"))
        {
            configElement = parseExtension(configfile, lineno, line);
            currentCategory.addElement(configElement);
            return configElement;
        }
        else if(line.trim().startsWith("include"))
        {
            // use parseVariable since we have access to it
            ConfigVariable configvar = parseVariable(configfile, lineno, line);
            configElement = new ConfigInclude(configfile, lineno, configvar.getValue());
            currentCategory.addElement(configElement);
            return configElement;
        }
        
        // leave everything else the same
        configElement = super.processTextLine(configfile, lineno, line);
        
        return configElement;
    }
    
    /* Roughly corresponds to pbx_config.c:2222 */
    protected ConfigExtension parseExtension(String configfile, int lineno, String line) throws ConfigParseException
    {
        ConfigVariable initialVariable = parseVariable(configfile, lineno, line);
        
        if(!initialVariable.getName().equals("exten"))
            throw new ConfigParseException(configfile, lineno, "missing 'exten' near " + line);
        line = initialVariable.getValue().trim();

        int nameIndex = line.indexOf(",", 0);
        if(nameIndex == -1)
            throw new ConfigParseException(configfile, lineno, "missing extension name near " + line);
        String name = line.substring(0, nameIndex);
        line = line.substring(name.length()+1, line.length()).trim();
        
        int priorityDelimiter = line.indexOf(",", 0);
        if(priorityDelimiter == -1)
            throw new ConfigParseException(configfile, lineno, "missing extension priority near " + line);
        String priority = line.substring(0, priorityDelimiter);
        line = line.substring(priority.length()+1, line.length()).trim();

        String [] application = harvestApplicationWithArguments(line);
        
        return new ConfigExtension(configfile,lineno,name,priority,application);
    }
    
    /* Roughly corresponds to pbx_config.c:2276 */
    private static String [] harvestApplicationWithArguments(String arg)
    {
        List args = new ArrayList();
        
        if(args != null && arg.trim().length() >= 0)
        {
            String appl = "", data = "";
            
            /* Find the first occurrence of either '(' or ',' */
            int firstc = arg.indexOf(',');
            int firstp = arg.indexOf('(');
            
            if (firstc != -1 && (firstp == -1 || firstc < firstp)) {
                /* comma found, no parenthesis */
                /* or both found, but comma found first */
                String [] split = arg.split(",");
                appl = split[0];
                for(int i = 1; i < split.length; i++)
                    data += split[i] + (i+1




© 2015 - 2024 Weber Informatics LLC | Privacy Policy