Java XML String Transform readURL(String urlStr)

Here you can find the source of readURL(String urlStr)

Description

read URL

License

Apache License

Declaration

public static String readURL(String urlStr) throws IOException 

Method Source Code


//package com.java2s;
/*/*from w  w w .j  ava2 s . co m*/
   Copyright 2006 OCLC Online Computer Library Center, Inc.
    
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.BufferedReader;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.StringWriter;

import java.net.HttpURLConnection;

import java.net.URL;

import javax.xml.transform.Transformer;

import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class Main {
    public static String readURL(String urlStr) throws IOException {
        return readURL(urlStr, false, false);
    }

    public static String readURL(String urlStr, boolean debug, boolean failIfNotXML) throws IOException {
        if (debug)
            System.out.print("    trying: " + urlStr + "\n");
        URL url;
        url = new URL(urlStr);
        HttpURLConnection huc;
        huc = (HttpURLConnection) url.openConnection();
        huc.addRequestProperty("Accept", "text/xml");
        String contentType = huc.getContentType();
        if (contentType == null || !contentType.contains("text/xml")) {
            System.out.print("*** Warning ***  Content-Type not set to text/xml");
            System.out.print('\n');
            System.out.print("    Content-type: ");
            System.out.print(contentType);
            System.out.print('\n');
            if (failIfNotXML)
                throw new IOException("Failed to get back XML for '" + urlStr + "'");
        }
        InputStream urlStream;
        urlStream = huc.getInputStream();
        BufferedReader in = new BufferedReader(new InputStreamReader(urlStream));
        boolean xml = true;
        String href = null, inputLine = null;
        StringBuilder content = new StringBuilder();
        Transformer transformer = null;
        try {
            inputLine = in.readLine();
        } catch (java.io.IOException e) {
            throw new IOException("failed: reading first line of response: ");
            //            throw new IOException("failed: reading first line of response: ", e);
        }
        if (inputLine == null) {
            System.out.print("test failed: No input read from URL");
            System.out.print('\n');
            return null;
        }
        if (!inputLine.startsWith("<?xml ")) {
            xml = false;
            content.append(inputLine);
        }

        if (xml) {
            // normally, you'd expect to read the next line of input here
            // but some servers don't put a newline after the initial <?xml ?>
            int offset = inputLine.indexOf('>');
            if (offset + 2 < inputLine.length()) {
                inputLine = inputLine.substring(offset + 1);
                offset = inputLine.indexOf('<');
                if (offset > 0)
                    inputLine = inputLine.substring(offset);
            } else
                try {
                    inputLine = in.readLine();
                } catch (java.io.IOException e) {
                    throw new IOException("failed: reading response: ");
                }

            content.append(inputLine);
        }

        try {
            while ((inputLine = in.readLine()) != null)
                content.append(inputLine);
        } catch (java.io.IOException e) {
            System.out.print("test failed: reading response: ");
            System.out.print(e.getMessage());
            System.out.print('\n');
            return null;
        }

        String contentStr = content.toString();
        if (transformer != null) {
            StreamSource streamXMLRecord = new StreamSource(new StringReader(contentStr));
            StringWriter xmlRecordWriter = new StringWriter();
            try {
                transformer.transform(streamXMLRecord, new StreamResult(xmlRecordWriter));
                transformer.reset();
                System.out.print("        successfully applied stylesheet '");
                System.out.print(href);
                System.out.print("'");
                System.out.print('\n');
            } catch (javax.xml.transform.TransformerException e) {
                transformer.reset();
                System.out.print("unable to apply stylesheet '");
                System.out.print(href);
                System.out.print("'to response: ");
                System.out.print(e.getMessage());
                System.out.print('\n');
            }
        }
        return contentStr;
    }
}

Related

  1. parseEventsToXML(String module, Hashtable keyvalues)
  2. parseToString(final Node node)
  3. parseXmlFile(String filename, boolean validating, boolean namespaceAware)
  4. readFile(String fileName)
  5. readFile(String systemId)
  6. readXMLFile(String xmlFileName)
  7. render(String name, byte[] xmldata)
  8. replaceLineSeparatorInternal(String string, String lineSeparator)
  9. save(Node doc, OutputStream stream, String encoding, boolean indent)