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

oracle / opengrok / #3669

01 Nov 2023 10:10AM UTC coverage: 75.13% (-0.03%) from 75.16%
#3669

push

web-flow
Fix Sonar codesmell issues (#4460)

Signed-off-by: Gino Augustine <ginoaugustine@gmail.com>

308 of 308 new or added lines in 27 files covered. (100.0%)

44029 of 58604 relevant lines covered (75.13%)

0.75 hits per line

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

0.0
/opengrok-indexer/src/main/java/org/opengrok/indexer/history/SubversionAnnotationParser.java
1
/*
2
 * CDDL HEADER START
3
 *
4
 * The contents of this file are subject to the terms of the
5
 * Common Development and Distribution License (the "License").
6
 * You may not use this file except in compliance with the License.
7
 *
8
 * See LICENSE.txt included in this distribution for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing Covered Code, include this CDDL HEADER in each
12
 * file and include the License file at LICENSE.txt.
13
 * If applicable, add the following below this CDDL HEADER, with the
14
 * fields enclosed by brackets "[]" replaced with your own identifying
15
 * information: Portions Copyright [yyyy] [name of copyright owner]
16
 *
17
 * CDDL HEADER END
18
 */
19

20
/*
21
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
22
 * Portions Copyright (c) 2020, Chris Fraire <cfraire@me.com>.
23
 */
24
package org.opengrok.indexer.history;
25

26
import java.io.BufferedInputStream;
27
import java.io.IOException;
28
import java.io.InputStream;
29
import java.util.logging.Level;
30
import java.util.logging.Logger;
31
import javax.xml.XMLConstants;
32
import javax.xml.parsers.ParserConfigurationException;
33
import javax.xml.parsers.SAXParser;
34
import javax.xml.parsers.SAXParserFactory;
35
import org.opengrok.indexer.logger.LoggerFactory;
36
import org.opengrok.indexer.util.Executor;
37
import org.xml.sax.Attributes;
38
import org.xml.sax.SAXException;
39
import org.xml.sax.ext.DefaultHandler2;
40

41
/**
42
 * handles parsing the output of the {@code svn annotate}
43
 * command into an annotation object.
44
 */
45
public class SubversionAnnotationParser implements Executor.StreamHandler {
46
    private static final Logger LOGGER = LoggerFactory.getLogger(SubversionAnnotationParser.class);
×
47

48
    /**
49
     * Store annotation created by processStream.
50
     */
51
    private final Annotation annotation;
52

53
    private final String fileName;
54

55
    /**
56
     * @param fileName the name of the file being annotated
57
     */
58
    public SubversionAnnotationParser(String fileName) {
×
59
        annotation = new Annotation(fileName);
×
60
        this.fileName = fileName;
×
61
    }
×
62

63
    /**
64
     * Returns the annotation that has been created.
65
     *
66
     * @return annotation an annotation object
67
     */
68
    public Annotation getAnnotation() {
69
        return annotation;
×
70
    }
71

72
    @Override
73
    public void processStream(InputStream input) throws IOException {
74
        SAXParserFactory factory = SAXParserFactory.newInstance();
×
75
        SAXParser saxParser;
76
        try {
77
            saxParser = factory.newSAXParser();
×
78
            saxParser.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, ""); // Compliant
×
79
            saxParser.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, ""); // compliant
×
80
        } catch (ParserConfigurationException | SAXException ex) {
×
81
            throw new IOException("Failed to create SAX parser", ex);
×
82
        }
×
83

84
        AnnotateHandler handler = new AnnotateHandler(fileName, annotation);
×
85
        try (BufferedInputStream in
×
86
                = new BufferedInputStream(input)) {
87
            saxParser.parse(in, handler);
×
88
        } catch (Exception e) {
×
89
            LOGGER.log(Level.SEVERE,
×
90
                    "An error occurred while parsing the xml output", e);
91
        }
×
92
    }
×
93

94
    private static class AnnotateHandler extends DefaultHandler2 {
95

96
        String rev;
97
        String author;
98
        final Annotation annotation;
99
        final StringBuilder sb;
100

101
        AnnotateHandler(String filename, Annotation annotation) {
×
102
            this.annotation = annotation;
×
103
            sb = new StringBuilder();
×
104
        }
×
105

106
        @Override
107
        public void startElement(String uri, String localName, String qname,
108
                Attributes attr) {
109
            sb.setLength(0);
×
110
            if ("entry".equals(qname)) {
×
111
                rev = null;
×
112
                author = null;
×
113
            } else if ("commit".equals(qname)) {
×
114
                rev = attr.getValue("revision");
×
115
            }
116
        }
×
117

118
        @Override
119
        public void endElement(String uri, String localName, String qname) {
120
            if ("author".equals(qname)) {
×
121
                author = sb.toString();
×
122
            } else if ("entry".equals(qname)) {
×
123
                annotation.addLine(rev, author, true);
×
124
            }
125
        }
×
126

127
        @Override
128
        public void characters(char[] arg0, int arg1, int arg2) {
129
            sb.append(arg0, arg1, arg2);
×
130
        }
×
131
    }
132
}
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