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

Ekryd / sortpom / 3770

07 Oct 2024 08:39AM CUT coverage: 99.841%. Remained the same
3770

push

circleci

web-flow
build(deps): bump com.spotify.fmt:fmt-maven-plugin from 2.24 to 2.25 (#450)

Bumps [com.spotify.fmt:fmt-maven-plugin](https://github.com/spotify/fmt-maven-plugin) from 2.24 to 2.25.
- [Release notes](https://github.com/spotify/fmt-maven-plugin/releases)
- [Commits](https://github.com/spotify/fmt-maven-plugin/compare/2.24...2.25)

---
updated-dependencies:
- dependency-name: com.spotify.fmt:fmt-maven-plugin
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

1256 of 1258 relevant lines covered (99.84%)

1.0 hits per line

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

100.0
/sorter/src/main/java/sortpom/output/PatchedXMLWriter.java
1
package sortpom.output;
2

3
import static java.util.Optional.ofNullable;
4

5
import java.io.IOException;
6
import java.io.Writer;
7
import org.dom4j.Attribute;
8
import org.dom4j.Document;
9
import org.dom4j.Node;
10
import org.dom4j.ProcessingInstruction;
11
import org.dom4j.io.OutputFormat;
12
import org.dom4j.io.XMLWriter;
13
import org.dom4j.tree.DefaultText;
14
import sortpom.content.NewlineText;
15
import sortpom.parameter.IndentAttribute;
16

17
/** Overriding XMLWriter to be able to handle SortPom formatting options */
18
class PatchedXMLWriter extends XMLWriter {
19

20
  private final OutputFormat format;
21
  private final boolean indentBlankLines;
22
  private final IndentAttribute indentAttribute;
23
  private final boolean spaceBeforeCloseEmptyElement;
24
  private final boolean endWithNewline;
25

26
  public PatchedXMLWriter(
27
      Writer writer,
28
      OutputFormat format,
29
      boolean spaceBeforeCloseEmptyElement,
30
      boolean indentBlankLines,
31
      IndentAttribute indentAttribute,
32
      boolean endWithNewline) {
33
    super(writer, format);
1✔
34
    this.format = format;
1✔
35
    this.indentBlankLines = indentBlankLines;
1✔
36
    this.indentAttribute = indentAttribute;
1✔
37
    this.spaceBeforeCloseEmptyElement = spaceBeforeCloseEmptyElement;
1✔
38
    this.endWithNewline = endWithNewline;
1✔
39
  }
1✔
40

41
  @Override
42
  public void write(Document doc) throws IOException {
43
    writeDeclaration();
1✔
44

45
    if (doc.getDocType() != null) {
1✔
46
      indent();
1✔
47
      writeDocType(doc.getDocType());
1✔
48
    }
49

50
    for (int i = 0, size = doc.nodeCount(); i < size; i++) {
1✔
51
      var node = doc.node(i);
1✔
52
      writeNode(node);
1✔
53
    }
54

55
    if (endWithNewline) {
1✔
56
      writePrintln();
1✔
57
    }
58
  }
1✔
59

60
  /** Handle spaceBeforeCloseEmptyElement option */
61
  @Override
62
  protected void writeEmptyElementClose(String qualifiedName) throws IOException {
63
    if (!format.isExpandEmptyElements() && spaceBeforeCloseEmptyElement) {
1✔
64
      // add an extra place before closing tag
65
      writer.write(' ');
1✔
66
    }
67
    super.writeEmptyElementClose(qualifiedName);
1✔
68
  }
1✔
69

70
  /** Fixing a bug with processing instructions */
71
  @Override
72
  protected void writeProcessingInstruction(ProcessingInstruction pi) throws IOException {
73
    // Place the processing instruction on own line (instead of same line as previous element)
74
    writePrintln();
1✔
75
    indent();
1✔
76
    writer.write("<?");
1✔
77
    writer.write(pi.getName());
1✔
78
    writer.write(" ");
1✔
79
    writer.write(pi.getText());
1✔
80
    writer.write("?>");
1✔
81

82
    lastOutputNodeType = Node.PROCESSING_INSTRUCTION_NODE;
1✔
83
  }
1✔
84

85
  /** Handle Custom NewLineTest node and potential indent of empty line */
86
  @Override
87
  protected void writeNodeText(Node node) throws IOException {
88
    if (node instanceof NewlineText) {
1✔
89
      // Handle our own NewlineText
90
      writePrintln();
1✔
91
      if (indentBlankLines) {
1✔
92
        // If blank lines should be indented
93
        indent();
1✔
94
      }
95
    } else {
96
      // Check if attribute xml:preserve is used
97
      if (isElementSpacePreserved(node.getParent())) {
1✔
98
        super.writeNodeText(node);
1✔
99
      } else {
100
        writeTrimmedText(node);
1✔
101
      }
102
    }
103
  }
1✔
104

105
  private void writeTrimmedText(Node node) throws IOException {
106
    var text = ofNullable(node.getText()).map(String::trim).filter(s -> !s.isEmpty());
1✔
107

108
    if (text.isPresent()) {
1✔
109
      // Test if this text node has siblings in the parent node
110
      if (node.getParent().content().size() > 1) {
1✔
111
        writePrintln();
1✔
112
        indent();
1✔
113
      }
114

115
      super.write(new DefaultText(text.orElseThrow()));
1✔
116
    }
117
  }
1✔
118

119
  /** Handle indentAttribute option */
120
  @Override
121
  protected void writeAttribute(Attribute attribute) throws IOException {
122
    var qualifiedName = attribute.getQualifiedName();
1✔
123
    if (indentAttribute == IndentAttribute.ALL
1✔
124
        || (indentAttribute == IndentAttribute.SCHEMA_LOCATION
125
            && "xsi:schemaLocation".equals(qualifiedName))) {
1✔
126
      writePrintln();
1✔
127
      indent();
1✔
128
      writeString(format.getIndent());
1✔
129
      writeString(format.getIndent());
1✔
130
    } else {
131
      writer.write(" ");
1✔
132
    }
133
    writer.write(qualifiedName);
1✔
134
    writer.write("=");
1✔
135

136
    var quote = format.getAttributeQuoteCharacter();
1✔
137
    writer.write(quote);
1✔
138

139
    writeEscapeAttributeEntities(attribute.getValue());
1✔
140

141
    writer.write(quote);
1✔
142
    lastOutputNodeType = Node.ATTRIBUTE_NODE;
1✔
143
  }
1✔
144

145
  @Override
146
  protected void writeNamespace(String prefix, String uri) throws IOException {
147
    if (indentAttribute == IndentAttribute.ALL) {
1✔
148
      writePrintln();
1✔
149
      indent();
1✔
150
      writeString(format.getIndent());
1✔
151
      writeString(format.getIndent());
1✔
152
    } else {
153
      writer.write(" ");
1✔
154
    }
155

156
    if ((prefix != null) && (!prefix.isEmpty())) {
1✔
157
      writer.write("xmlns:");
1✔
158
      writer.write(prefix);
1✔
159
      writer.write("=\"");
1✔
160
    } else {
161
      writer.write("xmlns=\"");
1✔
162
    }
163

164
    writer.write(uri);
1✔
165
    writer.write("\"");
1✔
166
  }
1✔
167
}
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