• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

LearnLib / automatalib / 7848080436

09 Feb 2024 06:23PM UTC coverage: 89.895% (+0.05%) from 89.841%
7848080436

push

github

web-flow
Unify parsing exceptions and hide implementation details (#74)

* unify parsing exceptions and hide implementation details

* add changelog entry

* add missing throws statements

9 of 18 new or added lines in 8 files covered. (50.0%)

3 existing lines in 1 file now uncovered.

15808 of 17585 relevant lines covered (89.89%)

1.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

36.36
/serialization/taf/src/main/java/net/automatalib/serialization/taf/parser/TAFParser.java
1
/* Copyright (C) 2013-2024 TU Dortmund University
2
 * This file is part of AutomataLib, http://www.automatalib.net/.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package net.automatalib.serialization.taf.parser;
17

18
import java.io.File;
19
import java.io.IOException;
20
import java.io.InputStream;
21
import java.io.Reader;
22
import java.io.StringReader;
23

24
import net.automatalib.automaton.FiniteAlphabetAutomaton;
25
import net.automatalib.automaton.fsa.impl.CompactDFA;
26
import net.automatalib.automaton.transducer.impl.CompactMealy;
27
import net.automatalib.common.util.IOUtil;
28
import net.automatalib.exception.FormatException;
29

30
/**
31
 * Facade for TAF parsing. This class provides several static methods to read TAF descriptions for DFA and Mealy
32
 * machines.
33
 */
34
public final class TAFParser {
35

36
    private TAFParser() {}
37

38
    public static CompactDFA<String> parseDFA(InputStream stream, TAFParseDiagnosticListener listener)
39
            throws IOException, FormatException {
40
        try (Reader r = IOUtil.asUncompressedBufferedNonClosingUTF8Reader(stream)) {
2✔
41
            return parseDFA(r, listener);
2✔
42
        }
43
    }
44

45
    public static CompactDFA<String> parseDFA(Reader reader, TAFParseDiagnosticListener listener)
46
            throws FormatException {
47
        InternalTAFParser parser = new InternalTAFParser(reader);
2✔
48
        parser.setDiagnosticListener(listener);
2✔
49
        DefaultTAFBuilderDFA builder = new DefaultTAFBuilderDFA(parser);
2✔
50
        try {
51
            parser.dfa(builder);
2✔
52
        } catch (ParseException ex) {
×
NEW
53
            throw new FormatException(ex);
×
54
        }
2✔
55
        return builder.finish();
2✔
56
    }
57

58
    public static CompactDFA<String> parseDFA(File file, TAFParseDiagnosticListener listener)
59
            throws IOException, FormatException {
60
        try (InputStream is = IOUtil.asBufferedInputStream(file)) {
×
61
            return parseDFA(is, listener);
×
62
        }
63
    }
64

65
    public static CompactDFA<String> parseDFA(String string, TAFParseDiagnosticListener listener)
66
            throws FormatException {
UNCOV
67
        return parseDFA(new StringReader(string), listener);
×
68
    }
69

70
    public static CompactMealy<String, String> parseMealy(InputStream stream, TAFParseDiagnosticListener listener)
71
            throws IOException, FormatException {
72
        try (Reader r = IOUtil.asUncompressedBufferedNonClosingUTF8Reader(stream)) {
2✔
73
            return parseMealy(r, listener);
2✔
74
        }
75
    }
76

77
    public static CompactMealy<String, String> parseMealy(Reader reader, TAFParseDiagnosticListener listener)
78
            throws FormatException {
79
        InternalTAFParser parser = new InternalTAFParser(reader);
2✔
80
        parser.setDiagnosticListener(listener);
2✔
81
        DefaultTAFBuilderMealy builder = new DefaultTAFBuilderMealy(parser);
2✔
82
        try {
83
            parser.mealy(builder);
2✔
84
        } catch (ParseException ex) {
×
NEW
85
            throw new FormatException(ex);
×
86
        }
2✔
87
        return builder.finish();
2✔
88
    }
89

90
    public static CompactMealy<String, String> parseMealy(File file, TAFParseDiagnosticListener listener)
91
            throws IOException, FormatException {
92
        try (InputStream is = IOUtil.asBufferedInputStream(file)) {
×
93
            return parseMealy(is, listener);
×
94
        }
95
    }
96

97
    public static CompactMealy<String, String> parseMealy(String string, TAFParseDiagnosticListener listener)
98
            throws FormatException {
UNCOV
99
        return parseMealy(new StringReader(string), listener);
×
100
    }
101

102
    public static FiniteAlphabetAutomaton<?, String, ?> parseAny(InputStream stream,
103
                                                                 TAFParseDiagnosticListener listener)
104
            throws IOException, FormatException {
105
        try (Reader r = IOUtil.asUncompressedBufferedNonClosingUTF8Reader(stream)) {
×
106
            return parseAny(r, listener);
×
107
        }
108
    }
109

110
    public static FiniteAlphabetAutomaton<?, String, ?> parseAny(Reader reader, TAFParseDiagnosticListener listener)
111
            throws FormatException {
112
        InternalTAFParser parser = new InternalTAFParser(reader);
×
113
        parser.setDiagnosticListener(listener);
×
114
        try {
115
            Type type = parser.type();
×
116
            switch (type) {
×
117
                case DFA: {
118
                    DefaultTAFBuilderDFA builder = new DefaultTAFBuilderDFA(parser);
×
119
                    parser.dfaBody(builder);
×
120
                    return builder.finish();
×
121
                }
122
                case MEALY: {
123
                    DefaultTAFBuilderMealy builder = new DefaultTAFBuilderMealy(parser);
×
124
                    parser.mealyBody(builder);
×
125
                    return builder.finish();
×
126
                }
127
                default:
128
                    throw new IllegalStateException("Unknown type " + type);
×
129
            }
130
        } catch (ParseException ex) {
×
NEW
131
            throw new FormatException(ex);
×
132
        }
133
    }
134

135
    public static FiniteAlphabetAutomaton<?, String, ?> parseAny(File file, TAFParseDiagnosticListener listener)
136
            throws IOException, FormatException {
137
        try (InputStream is = IOUtil.asBufferedInputStream(file)) {
×
138
            return parseAny(is, listener);
×
139
        }
140
    }
141

142
    public static FiniteAlphabetAutomaton<?, String, ?> parseAny(String string, TAFParseDiagnosticListener listener)
143
            throws FormatException {
UNCOV
144
        return parseAny(new StringReader(string), listener);
×
145
    }
146
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc