Java XML Transform Usage createUnixStreamResultForEntry(OutputStream outputEntry)

Here you can find the source of createUnixStreamResultForEntry(OutputStream outputEntry)

Description

Creates a StreamResult by wrapping the given outputEntry in an OutputStreamWriter that transforms Windows line endings (\r\n) into Unix line endings (\n) on Windows for consistency with Roo's templates.

License

Apache License

Parameter

Parameter Description
outputEntry a parameter

Exception

Parameter Description
UnsupportedEncodingException an exception

Return

StreamResult

Declaration

private static StreamResult createUnixStreamResultForEntry(OutputStream outputEntry)
        throws UnsupportedEncodingException 

Method Source Code

//package com.java2s;
/**/*from   ww  w.  j a va 2  s  .  com*/
 * Copyright (C) 2005-2009 Alfresco Software Limited.
 *
 * This file is part of the Spring Surf Extension project.
 *
 * Licensed 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.
 */

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;

import javax.xml.transform.stream.StreamResult;

public class Main {
    /**
     * Creates a {@link StreamResult} by wrapping the given outputEntry in an
     * {@link OutputStreamWriter} that transforms Windows line endings (\r\n) 
     * into Unix line endings (\n) on Windows for consistency with Roo's templates.  
     * @param outputEntry
     * @return StreamResult 
     * @throws UnsupportedEncodingException 
     */
    private static StreamResult createUnixStreamResultForEntry(OutputStream outputEntry)
            throws UnsupportedEncodingException {
        final Writer writer;
        if (System.getProperty("line.separator").equals("\r\n")) {
            writer = new OutputStreamWriter(outputEntry, "ISO-8859-1") {
                public void write(char[] cbuf, int off, int len) throws IOException {
                    for (int i = off; i < off + len; i++) {
                        if (cbuf[i] != '\r' || (i < cbuf.length - 1 && cbuf[i + 1] != '\n')) {
                            super.write(cbuf[i]);
                        }
                    }
                }

                public void write(int c) throws IOException {
                    if (c != '\r')
                        super.write(c);
                }

                public void write(String str, int off, int len) throws IOException {
                    String orig = str.substring(off, off + len);
                    String filtered = orig.replace("\r\n", "\n");
                    int lengthDiff = orig.length() - filtered.length();
                    if (filtered.endsWith("\r")) {
                        super.write(filtered.substring(0, filtered.length() - 1), 0, len - lengthDiff - 1);
                    } else {
                        super.write(filtered, 0, len - lengthDiff);
                    }
                }
            };
        } else {
            writer = new OutputStreamWriter(outputEntry, "ISO-8859-1");
        }
        return new StreamResult(writer);
    }
}

Related

  1. createSafeReader(InputStream inputStream)
  2. createSource(InputStream is)
  3. createStaxResult(XMLStreamWriter streamWriter)
  4. createStaxSource(XMLStreamReader streamReader)
  5. createStreamResult(File outFile)
  6. dateToGregorian(Date date)
  7. DateToXML(Date date)
  8. dumpMetadata(IIOMetadata meta)
  9. dumpMetadataToSystemOut(IIOMetadata iiometa)