Search in sources :

Example 1 with SyntaxException

use of uk.me.parabola.mkgmap.scan.SyntaxException in project mkgmap by openstreetmap.

the class StyleImpl method readStyle.

/**
 * Evaluate the style options and try to read the style.
 *
 * The option --style-file give the location of an alternate file or
 * directory containing styles rather than the default built in ones.
 *
 * The option --style gives the name of a style, either one of the
 * built in ones or selects one from the given style-file.
 *
 * If there is no name given, but there is a file then the file should
 * just contain one style.
 *
 * @param props the program properties
 * @return A style instance or null in case of error.
 */
public static Style readStyle(EnhancedProperties props) {
    String loc = props.getProperty("style-file");
    if (loc == null)
        loc = props.getProperty("map-features");
    String name = props.getProperty("style");
    if (loc == null && name == null)
        name = "default";
    if (name == null) {
        StyleFileLoader loader = null;
        try {
            loader = StyleFileLoader.createStyleLoader(loc, null);
            int numEntries = loader.list().length;
            if (numEntries > 1)
                throw new ExitException("Style file " + loc + " contains multiple styles, use option --style to select one.");
        } catch (FileNotFoundException e) {
            throw new ExitException("Could not open style file " + loc);
        } finally {
            Utils.closeFile(loader);
        }
    }
    Style style;
    try {
        style = new StyleImpl(loc, name, props, WITHOUT_CHECKS);
    } catch (SyntaxException e) {
        System.err.println("Error in style: " + e.getMessage());
        throw new ExitException("Could not open style " + (name == null ? "" : name));
    } catch (FileNotFoundException e) {
        String msg = "Could not open style ";
        if (name != null) {
            msg += name;
            if (loc != null)
                msg += " in " + loc;
        } else
            msg += loc + " . Make sure that it points to a style or add the --style option.";
        throw new ExitException(msg);
    }
    return style;
}
Also used : SyntaxException(uk.me.parabola.mkgmap.scan.SyntaxException) FileNotFoundException(java.io.FileNotFoundException) Style(uk.me.parabola.mkgmap.reader.osm.Style) ExitException(uk.me.parabola.imgfmt.ExitException)

Example 2 with SyntaxException

use of uk.me.parabola.mkgmap.scan.SyntaxException in project mkgmap by openstreetmap.

the class TypeReader method setLevel.

/**
 * Read a level spec, which is either the max level or a min to max range.
 * This is immediately converted to resolution(s).
 */
private void setLevel(TokenScanner ts, GType gt) {
    String str = ts.nextWord();
    try {
        if (str.indexOf('-') >= 0) {
            String[] minmax = HYPHEN_PATTERN.split(str, 2);
            int val1 = toResolution(Integer.parseInt(minmax[0]));
            int val2 = toResolution(Integer.parseInt(minmax[1]));
            if (val1 > val2) {
                // Previously there was a bug where the order was reversed, so we swap the numbers if they are
                // the wrong way round.
                int h = val1;
                val1 = val2;
                val2 = h;
            }
            gt.setMinResolution(val1);
            gt.setMaxResolution(val2);
        } else {
            gt.setMinResolution(toResolution(Integer.parseInt(str)));
        }
    } catch (NumberFormatException e) {
        throw new SyntaxException(ts, "Invalid value for level: '" + str + '\'');
    }
}
Also used : SyntaxException(uk.me.parabola.mkgmap.scan.SyntaxException)

Example 3 with SyntaxException

use of uk.me.parabola.mkgmap.scan.SyntaxException in project mkgmap by openstreetmap.

the class TypCompiler method makeMap.

/**
 * The integration with mkgmap.
 *
 * @param args The options that are in force.
 * @param filename The input filename.
 * @return Returns the name of the file that was written. It depends on the family id.
 */
public String makeMap(CommandArgs args, String filename) {
    assert filename.toLowerCase().endsWith(".txt");
    CharsetProbe probe = new CharsetProbe();
    String readCharset = probe.probeCharset(filename);
    TypData data;
    try {
        data = compile(filename, readCharset, args.getSort());
    } catch (SyntaxException e) {
        throw new MapFailedException("Compiling TYP txt file: " + e.getMessage());
    } catch (FileNotFoundException e) {
        throw new MapFailedException("Could not open TYP file " + filename + " to read");
    }
    TypParam param = data.getParam();
    int family = args.get("family-id", -1);
    int product = args.get("product-id", -1);
    int cp = args.get("code-page", -1);
    if (family != -1)
        param.setFamilyId(family);
    if (product != -1)
        param.setProductId(product);
    if (cp != -1)
        param.setCodePage(cp);
    File outFile = new File(filename);
    String outName = outFile.getName();
    int last;
    if (outName.length() > 4 && (last = outName.lastIndexOf('.')) > 0)
        outName = outName.substring(0, last);
    outName += ".typ";
    outFile = new File(args.getOutputDir(), outName);
    try {
        writeTyp(data, outFile);
    } catch (TypLabelException e) {
        throw new MapFailedException("TYP file cannot be written in code page " + data.getSort().getCodepage());
    } catch (IOException e) {
        throw new MapFailedException("Error while writing typ file", e);
    }
    return outFile.getPath();
}
Also used : TypLabelException(uk.me.parabola.imgfmt.app.typ.TypLabelException) TypData(uk.me.parabola.imgfmt.app.typ.TypData) MapFailedException(uk.me.parabola.imgfmt.MapFailedException) SyntaxException(uk.me.parabola.mkgmap.scan.SyntaxException) FileNotFoundException(java.io.FileNotFoundException) TypParam(uk.me.parabola.imgfmt.app.typ.TypParam) IOException(java.io.IOException) TYPFile(uk.me.parabola.imgfmt.app.typ.TYPFile) File(java.io.File)

Example 4 with SyntaxException

use of uk.me.parabola.mkgmap.scan.SyntaxException in project mkgmap by openstreetmap.

the class ExpressionArranger method getKeystring.

/**
 * Get the key string for this expression.
 *
 * We use a literal string such as highway=primary to index the rules. If it is not possible to find a key string,
 * then the expression is not allowed.  This should only happen with expression that could match an element with no
 * tags.
 */
public String getKeystring(TokenScanner scanner, Op op) {
    Op first = op.getFirst();
    Op second = op.getSecond();
    String keystring = null;
    if (op.isType(EQUALS) && first.isType(FUNCTION) && second.isType(VALUE)) {
        keystring = first.getKeyValue() + "=" + second.getKeyValue();
    } else if (op.isType(EXISTS)) {
        keystring = first.getKeyValue() + "=*";
    } else if (op.isType(AND)) {
        if (first.isType(EQUALS)) {
            keystring = first.getFirst().getKeyValue() + "=" + first.getSecond().getKeyValue();
        } else if (first.isType(EXISTS)) {
            keystring = first.getFirst().getKeyValue() + "=*";
        } else if (first.isType(NOT_EXISTS)) {
            throw new SyntaxException(scanner, "Cannot start rule with tag!=*");
        }
    }
    if (keystring == null)
        throw new SyntaxException(scanner, "Invalid rule expression: " + op);
    return keystring;
}
Also used : NotExistsOp(uk.me.parabola.mkgmap.osmstyle.eval.NotExistsOp) AbstractOp(uk.me.parabola.mkgmap.osmstyle.eval.AbstractOp) Op(uk.me.parabola.mkgmap.osmstyle.eval.Op) GTEOp(uk.me.parabola.mkgmap.osmstyle.eval.GTEOp) ValueOp(uk.me.parabola.mkgmap.osmstyle.eval.ValueOp) LTEOp(uk.me.parabola.mkgmap.osmstyle.eval.LTEOp) LTOp(uk.me.parabola.mkgmap.osmstyle.eval.LTOp) NotEqualOp(uk.me.parabola.mkgmap.osmstyle.eval.NotEqualOp) EqualsOp(uk.me.parabola.mkgmap.osmstyle.eval.EqualsOp) RegexOp(uk.me.parabola.mkgmap.osmstyle.eval.RegexOp) ExistsOp(uk.me.parabola.mkgmap.osmstyle.eval.ExistsOp) BinaryOp(uk.me.parabola.mkgmap.osmstyle.eval.BinaryOp) GTOp(uk.me.parabola.mkgmap.osmstyle.eval.GTOp) NotRegexOp(uk.me.parabola.mkgmap.osmstyle.eval.NotRegexOp) OrOp(uk.me.parabola.mkgmap.osmstyle.eval.OrOp) AndOp(uk.me.parabola.mkgmap.osmstyle.eval.AndOp) LinkedOp(uk.me.parabola.mkgmap.osmstyle.eval.LinkedOp) SyntaxException(uk.me.parabola.mkgmap.scan.SyntaxException)

Example 5 with SyntaxException

use of uk.me.parabola.mkgmap.scan.SyntaxException in project mkgmap by openstreetmap.

the class RuleFileReader method readFinalize.

private boolean readFinalize(TokenScanner scanner) {
    Token token = scanner.nextToken();
    if (scanner.checkToken("finalize")) {
        Token finalizeToken = scanner.nextToken();
        if (scanner.checkToken(">")) {
            if (inFinalizeSection) {
                // there are two finalize sections which is not allowed
                throw new SyntaxException(scanner, "There is only one finalize section allowed");
            } else {
                // consume the > token
                scanner.nextToken();
                // mark start of the finalize block
                inFinalizeSection = true;
                finalizeRules = new RuleSet();
                return true;
            }
        } else {
            scanner.pushToken(finalizeToken);
            scanner.pushToken(token);
        }
    } else {
        scanner.pushToken(token);
    }
    return false;
}
Also used : SyntaxException(uk.me.parabola.mkgmap.scan.SyntaxException) Token(uk.me.parabola.mkgmap.scan.Token)

Aggregations

SyntaxException (uk.me.parabola.mkgmap.scan.SyntaxException)42 Token (uk.me.parabola.mkgmap.scan.Token)8 FileNotFoundException (java.io.FileNotFoundException)5 TokenScanner (uk.me.parabola.mkgmap.scan.TokenScanner)5 EqualsOp (uk.me.parabola.mkgmap.osmstyle.eval.EqualsOp)4 Op (uk.me.parabola.mkgmap.osmstyle.eval.Op)4 ValueOp (uk.me.parabola.mkgmap.osmstyle.eval.ValueOp)4 File (java.io.File)3 ArrayList (java.util.ArrayList)3 NotOp (uk.me.parabola.mkgmap.osmstyle.eval.NotOp)3 IOException (java.io.IOException)2 StringReader (java.io.StringReader)2 Matcher (java.util.regex.Matcher)2 ExitException (uk.me.parabola.imgfmt.ExitException)2 MapFailedException (uk.me.parabola.imgfmt.MapFailedException)2 BitmapImage (uk.me.parabola.imgfmt.app.typ.BitmapImage)2 ColourInfo (uk.me.parabola.imgfmt.app.typ.ColourInfo)2 TYPFile (uk.me.parabola.imgfmt.app.typ.TYPFile)2 TypData (uk.me.parabola.imgfmt.app.typ.TypData)2 Xpm (uk.me.parabola.imgfmt.app.typ.Xpm)2