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

pmd / pmd / 196

16 Oct 2025 08:33AM UTC coverage: 78.642% (-0.02%) from 78.661%
196

push

github

web-flow
chore: fix dogfood issues from new rules (#6056)

18180 of 23973 branches covered (75.84%)

Branch coverage included in aggregate %.

2 of 27 new or added lines in 14 files covered. (7.41%)

2 existing lines in 1 file now uncovered.

39693 of 49617 relevant lines covered (80.0%)

0.81 hits per line

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

0.0
/pmd-core/src/main/java/net/sourceforge/pmd/util/treeexport/TreeExporter.java
1
/*
2
 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3
 */
4

5
package net.sourceforge.pmd.util.treeexport;
6

7
import java.io.BufferedReader;
8
import java.io.IOException;
9
import java.io.InputStreamReader;
10
import java.io.Reader;
11
import java.nio.charset.Charset;
12
import java.util.Collections;
13
import java.util.Map.Entry;
14
import java.util.Properties;
15

16
import net.sourceforge.pmd.internal.Slf4jSimpleConfiguration;
17
import net.sourceforge.pmd.lang.LanguageProcessor;
18
import net.sourceforge.pmd.lang.LanguageProcessorRegistry;
19
import net.sourceforge.pmd.lang.LanguagePropertyBundle;
20
import net.sourceforge.pmd.lang.LanguageRegistry;
21
import net.sourceforge.pmd.lang.LanguageVersion;
22
import net.sourceforge.pmd.lang.ast.Parser;
23
import net.sourceforge.pmd.lang.ast.Parser.ParserTask;
24
import net.sourceforge.pmd.lang.ast.RootNode;
25
import net.sourceforge.pmd.lang.ast.SemanticErrorReporter;
26
import net.sourceforge.pmd.lang.document.FileId;
27
import net.sourceforge.pmd.lang.document.TextDocument;
28
import net.sourceforge.pmd.lang.document.TextFile;
29
import net.sourceforge.pmd.lang.rule.xpath.Attribute;
30
import net.sourceforge.pmd.properties.PropertyDescriptor;
31
import net.sourceforge.pmd.properties.PropertySource;
32

33
public class TreeExporter {
34

35
    private final TreeExportConfiguration configuration;
36
    private final Io io;
37
    
38
    public TreeExporter(final TreeExportConfiguration configuration) {
39
        this(configuration, Io.system());
×
40
    }
×
41
    
42
    TreeExporter(final TreeExportConfiguration configuration, final Io io) {
×
43
        this.configuration = configuration;
×
44
        this.io = io;
×
45
    }
×
46
    
47
    public void export() throws IOException {
48
        TreeRendererDescriptor descriptor = TreeRenderers.findById(configuration.getFormat());
×
49
        if (descriptor == null) {
×
50
            throw this.bail("Unknown format '" + configuration.getFormat() + "'");
×
51
        }
52

53
        PropertySource bundle = parseProperties(descriptor.newPropertyBundle(), configuration.getProperties());
×
54
        LanguagePropertyBundle langProperties = parseProperties(configuration.getLanguage().newPropertyBundle(), configuration.getLanguageProperties());
×
55

56
        LanguageRegistry lang = LanguageRegistry.PMD.getDependenciesOf(configuration.getLanguage());
×
57
        try (LanguageProcessorRegistry lpRegistry = LanguageProcessorRegistry.create(lang,
×
58
                                                                                     Collections.singletonMap(configuration.getLanguage(), langProperties),
×
59
                                                                                     configuration.getMessageReporter())) {
×
60
            run(lpRegistry, descriptor.produceRenderer(bundle));
×
61
        }
62
    }
×
63
    
64
    private void run(LanguageProcessorRegistry langRegistry, final TreeRenderer renderer) throws IOException {
65
        LanguageVersion langVersion = configuration.getLanguage().getDefaultVersion();
×
66
        @SuppressWarnings("PMD.CloseResource")
67
        LanguageProcessor processor = langRegistry.getProcessor(configuration.getLanguage());
×
68
        Parser parser = processor.services().getParser();
×
69

70
        @SuppressWarnings("PMD.CloseResource")
71
        TextFile textFile;
72
        if (configuration.isReadStdin()) {
×
73
            io.stderr.println("Reading from stdin...");
×
74
            textFile = TextFile.forReader(readFromSystemIn(), FileId.STDIN, langVersion);
×
75
        } else {
76
            textFile = TextFile.forPath(configuration.getFile(), configuration.getSourceEncoding(), langVersion);
×
77
        }
78

79
        // disable warnings for deprecated attributes
80
        Slf4jSimpleConfiguration.disableLogging(Attribute.class);
×
81

82
        try (TextDocument textDocument = TextDocument.create(textFile)) {
×
83

84
            ParserTask task = new ParserTask(textDocument, SemanticErrorReporter.noop(), langRegistry);
×
85
            RootNode root = parser.parse(task);
×
86

87
            renderer.renderSubtree(root, io.stdout);
×
88
        }
89
    }
×
90

91
    private Reader readFromSystemIn() {
NEW
92
        return new BufferedReader(new InputStreamReader(io.stdin, Charset.defaultCharset()));
×
93
    }
94

95
    private <T extends PropertySource> T parseProperties(T bundle, Properties properties) {
96

97
        for (Entry<Object, Object> prop : properties.entrySet()) {
×
98
            PropertyDescriptor<?> d = bundle.getPropertyDescriptor(prop.getKey().toString());
×
99
            if (d == null) {
×
100
                throw bail("Unknown property '" + prop.getKey() + "'");
×
101
            }
102

103
            setProperty(d, bundle, prop.getValue().toString());
×
104
        }
×
105
        return bundle;
×
106
    }
107
    
108
    private <T> void setProperty(PropertyDescriptor<T> descriptor, PropertySource bundle, String value) {
109
        bundle.setProperty(descriptor, descriptor.serializer().fromString(value));
×
110
    }
×
111
    
112
    private AbortedException bail(String message) {
113
        io.stderr.println(message);
×
114
        io.stderr.println("Use --help for usage information");
×
115
        return new AbortedException();
×
116
    }
117

118
    private static final class AbortedException extends RuntimeException {
119

120
        private static final long serialVersionUID = -1925142332978792215L;
121
    }
122
}
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

© 2026 Coveralls, Inc