Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

//@@3RD PARTY CODE@@

package org.glassfish.jaxb.runtime.v2.runtime.output;

import java.io.Serializable;
import java.io.Writer;

/**
* <p>
Copy link
Contributor Author

@winfriedgerlach winfriedgerlach Feb 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like Groovy, I kept the copyright notice and JavaDoc intact but removed the dependency on IOUtils.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Build is KO due to copyright missing I guess

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@laurentschoelens yes - how shall I handle this? is it possible to add a jaxb-ri copyright to a class copied from another project? I am not a lawyer...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind, I found the copyright-exclude file...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That’s for code which either went through legal or is non-source file. This does not satisfy any of those conditions and likely breaks existing project’s license.

* Copied from
* <a href="https://github.com/apache/commons-io/blob/master/src/main/java/org/apache/commons/io/output/StringBuilderWriter.java">Apache commons-io</a>.
* <p>
* {@link Writer} implementation that outputs to a {@link StringBuilder}.
* <p>
* <strong>NOTE:</strong> This implementation, as an alternative to {@link java.io.StringWriter}, provides an
* <em>un-synchronized</em> implementation for better performance for use in a single thread. For safe usage with
* multiple {@link Thread}s, a {@link java.io.StringWriter} should be used.
*/
public class StringBuilderWriter extends Writer implements Serializable {

private static final long serialVersionUID = -146927496096066153L;

/** The append target. */
private final StringBuilder builder;

/**
* Constructs a new {@link StringBuilder} instance with default capacity.
*/
public StringBuilderWriter() {
this.builder = new StringBuilder();
}

/**
* Constructs a new {@link StringBuilder} instance with the specified capacity.
*
* @param capacity The initial capacity of the underlying {@link StringBuilder}
*/
public StringBuilderWriter(final int capacity) {
this.builder = new StringBuilder(capacity);
}

/**
* Constructs a new instance with the specified {@link StringBuilder}.
*
* <p>If {@code builder} is null a new instance with default capacity will be created.</p>
*
* @param builder The String builder. May be null.
*/
public StringBuilderWriter(final StringBuilder builder) {
this.builder = builder != null ? builder : new StringBuilder();
}

/**
* Appends a single character to this Writer.
*
* @param value The character to append
* @return This writer instance
*/
@Override
public Writer append(final char value) {
builder.append(value);
return this;
}

/**
* Appends a character sequence to this Writer.
*
* @param value The character to append
* @return This writer instance
*/
@Override
public Writer append(final CharSequence value) {
builder.append(value);
return this;
}

/**
* Appends a portion of a character sequence to the {@link StringBuilder}.
*
* @param value The character to append
* @param start The index of the first character
* @param end The index of the last character + 1
* @return This writer instance
*/
@Override
public Writer append(final CharSequence value, final int start, final int end) {
builder.append(value, start, end);
return this;
}

/**
* Closing this writer has no effect.
*/
@Override
public void close() {
// no-op
}

/**
* Flushing this writer has no effect.
*/
@Override
public void flush() {
// no-op
}

/**
* Gets the underlying builder.
*
* @return The underlying builder
*/
public StringBuilder getBuilder() {
return builder;
}

/**
* Returns {@link StringBuilder#toString()}.
*
* @return The contents of the String builder.
*/
@Override
public String toString() {
return builder.toString();
}

/**
* Writes a portion of a character array to the {@link StringBuilder}.
*
* @param value The value to write
* @param offset The index of the first character
* @param length The number of characters to write
*/
@Override
public void write(final char[] value, final int offset, final int length) {
if (value != null) {
builder.append(value, offset, length);
}
}

/**
* Writes a String to the {@link StringBuilder}.
*
* @param value The value to write
*/
@Override
public void write(final String value) {
if (value != null) {
builder.append(value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import javax.xml.stream.XMLStreamException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringWriter;

/**
* {@link XmlOutput} implementation specialized for UTF-8.
Expand Down Expand Up @@ -160,7 +159,7 @@ private int pushNsDecls() {

Encoded e = prefixes[i];

if(p.length()==0) {
if(p.isEmpty()) {
e.buf = EMPTY_BYTE_ARRAY;
e.len = 0;
} else {
Expand All @@ -186,9 +185,8 @@ protected void writeNsDecls(int base) throws IOException {
protected final void writeNsDecl(int prefixIndex) throws IOException {
String p = nsContext.getPrefix(prefixIndex);

if(p.length()==0) {
if(nsContext.getCurrent().isRootElement()
&& nsContext.getNamespaceURI(prefixIndex).length()==0)
if(p.isEmpty()) {
if(nsContext.getCurrent().isRootElement() && nsContext.getNamespaceURI(prefixIndex).isEmpty())
return; // no point in declaring xmlns="" on the root element
write(XMLNS_EQUALS);
} else {
Expand Down Expand Up @@ -288,7 +286,7 @@ public void text(Pcdata value, boolean needSP) throws IOException {

private void doText(String value,boolean isAttribute) throws IOException {
if (escapeHandler != null) {
StringWriter sw = new StringWriter();
StringBuilderWriter sw = new StringBuilderWriter();
escapeHandler.escape(value.toCharArray(), 0, value.length(), isAttribute, sw);
textBuffer.set(sw.toString());
} else {
Expand Down
1 change: 1 addition & 0 deletions jaxb-ri/tools/config/copyright-exclude
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ jing-copying.html
/rngom/src/test/java/Main.java

/runtime/impl/src/main/java/com/sun/xml/bind/marshaller/XMLWriter.java
/runtime/impl/src/main/java/org/glassfish/jaxb/runtime/v2/runtime/output/StringBuilderWriter.java

/tools/pretty-printer/src/com/sun/tools/xmlpp/DataWriter.java
/tools/pretty-printer/src/com/sun/tools/xmlpp/XMLWriter.java
Expand Down
Loading