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

aspectran / aspectran / #4057

11 Feb 2025 06:00AM CUT coverage: 35.269% (+1.9%) from 33.377%
#4057

push

github

topframe
Update

13 of 42 new or added lines in 7 files covered. (30.95%)

4 existing lines in 3 files now uncovered.

14247 of 40395 relevant lines covered (35.27%)

0.35 hits per line

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

43.48
/core/src/main/java/com/aspectran/core/activity/response/transform/XmlTransformResponse.java
1
/*
2
 * Copyright (c) 2008-2025 The Aspectran Project
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 com.aspectran.core.activity.response.transform;
17

18
import com.aspectran.core.activity.Activity;
19
import com.aspectran.core.activity.process.result.ProcessResult;
20
import com.aspectran.core.activity.response.Response;
21
import com.aspectran.core.activity.response.transform.xml.ContentsInputSource;
22
import com.aspectran.core.activity.response.transform.xml.ContentsXMLReader;
23
import com.aspectran.core.adapter.ResponseAdapter;
24
import com.aspectran.core.context.rule.TransformRule;
25
import com.aspectran.utils.StringifyContext;
26
import com.aspectran.utils.annotation.jsr305.NonNull;
27

28
import javax.xml.transform.OutputKeys;
29
import javax.xml.transform.Source;
30
import javax.xml.transform.Transformer;
31
import javax.xml.transform.TransformerException;
32
import javax.xml.transform.TransformerFactory;
33
import javax.xml.transform.sax.SAXSource;
34
import javax.xml.transform.stream.StreamResult;
35
import java.io.Writer;
36

37
/**
38
 * XML Transform Response converts the response data to XML and outputs it.
39
 *
40
 * <p>Created: 2008. 03. 22 PM 5:51:58</p>
41
 */
42
public class XmlTransformResponse extends TransformResponse {
43

44
    private static final String OUTPUT_METHOD_XML = "xml";
45

46
    private static final String INDENT_NUMBER_KEY = "indent-number";
47

48
    private static final Integer DEFAULT_INDENT_SIZE = 2;
1✔
49

50
    private static final String YES = "yes";
51

52
    private final String contentType;
53

54
    private final String encoding;
55

56
    private final Boolean pretty;
57

58
    /**
59
     * Instantiates a new XmlTransformResponse.
60
     * @param transformRule the transform rule
61
     */
62
    public XmlTransformResponse(TransformRule transformRule) {
63
        super(transformRule);
×
64

65
        this.contentType = transformRule.getContentType();
×
66
        this.encoding = transformRule.getEncoding();
×
67
        this.pretty = transformRule.getPretty();
×
68
    }
×
69

70
    @Override
71
    protected void transform(@NonNull Activity activity) throws Exception {
72
        ResponseAdapter responseAdapter = activity.getResponseAdapter();
×
73

74
        String encoding;
75
        if (this.encoding != null) {
×
76
            encoding = this.encoding;
×
77
        } else {
78
            encoding = responseAdapter.getEncoding();
×
79
            if (encoding == null) {
×
80
                encoding = activity.getTranslet().getDefinitiveResponseEncoding();
×
81
            }
82
        }
83
        if (encoding != null) {
×
84
            responseAdapter.setEncoding(encoding);
×
85
        }
86

87
        if (contentType != null) {
×
88
            responseAdapter.setContentType(contentType);
×
89
        }
90

91
        ProcessResult processResult = activity.getProcessResult();
×
92
        Writer writer = responseAdapter.getWriter();
×
93

94
        StringifyContext stringifyContext = activity.getStringifyContext();
×
NEW
95
        if (pretty != null && stringifyContext.isPrettyPrint() != pretty) {
×
96
            stringifyContext = stringifyContext.clone();
×
NEW
97
            stringifyContext.setPrettyPrint(pretty);
×
98
        }
99

100
        transform(processResult, writer, encoding, stringifyContext, pretty);
×
101
    }
×
102

103
    @Override
104
    public Response replicate() {
105
        return new XmlTransformResponse(getTransformRule().replicate());
×
106
    }
107

108
    public static void transform(
109
            Object object, Writer writer, String encoding,
110
            StringifyContext stringifyContext) throws TransformerException {
111
        transform(object, writer, encoding, stringifyContext, null);
1✔
112
    }
1✔
113

114
    private static void transform(
115
            Object object, Writer writer, String encoding,
116
            StringifyContext stringifyContext, Boolean prettyForce) throws TransformerException {
117
        boolean pretty = (prettyForce != null ? prettyForce : (stringifyContext == null || stringifyContext.isPrettyPrint()));
1✔
118

119
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
1✔
120
        if (pretty) {
1✔
121
            if (stringifyContext != null && stringifyContext.hasIndentSize()) {
1✔
122
                transformerFactory.setAttribute(INDENT_NUMBER_KEY, stringifyContext.getIndentSize());
×
123
            } else {
124
                transformerFactory.setAttribute(INDENT_NUMBER_KEY, DEFAULT_INDENT_SIZE);
1✔
125
            }
126
        }
127

128
        Transformer transformer = transformerFactory.newTransformer();
1✔
129
        transformer.setOutputProperty(OutputKeys.METHOD, OUTPUT_METHOD_XML);
1✔
130
        if (encoding != null) {
1✔
131
            transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
×
132
        }
133
        if (pretty) {
1✔
134
            transformer.setOutputProperty(OutputKeys.INDENT, YES);
1✔
135
            transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, YES);
1✔
136
        }
137

138
        ContentsXMLReader xmlReader = new ContentsXMLReader();
1✔
139
        xmlReader.setStringifyContext(stringifyContext);
1✔
140

141
        ContentsInputSource inputSource = new ContentsInputSource(object);
1✔
142
        Source source = new SAXSource(xmlReader, inputSource);
1✔
143
        transformer.transform(source, new StreamResult(writer));
1✔
144
    }
1✔
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