Example usage for org.xml.sax InputSource getByteStream

List of usage examples for org.xml.sax InputSource getByteStream

Introduction

In this page you can find the example usage for org.xml.sax InputSource getByteStream.

Prototype

public InputStream getByteStream() 

Source Link

Document

Get the byte stream for this input source.

Usage

From source file:org.regenstrief.util.XMLUtil.java

public final static Reader toReader(final InputSource source) {
    try {/*  w w w .  j  a  v  a 2 s.c  om*/
        final InputStream in = source.getByteStream();
        if (in != null) {
            return Util.getReader(in);
        } else {
            final Reader r = source.getCharacterStream();
            if (r != null) {
                return r;
            } else {
                final String systemId = source.getSystemId();
                if (systemId != null) {
                    return Util.getReader(systemId);
                }
            }
        }
    } catch (final Exception e) {
        throw Util.toRuntimeException(e);
    }

    throw new RuntimeException("Unsupported InputSource: " + source);
}

From source file:org.wso2.carbon.mediation.initializer.RegistryXmlSchemaURIResolver.java

/**
 * Used in validate mediator to validate the schemas
 * /* ww w.ja v a2 s.  c o m*/
 * @param type
 *            The type of the resource being resolved. For XML [XML 1.0]
 *            resources (i.e. entities), applications must use the value
 *            "http://www.w3.org/TR/REC-xml". For XML Schema [XML Schema
 *            Part 1] , applications must use the value
 *            "http://www.w3.org/2001/XMLSchema". Other types of resources
 *            are outside the scope of this specification and therefore
 *            should recommend an absolute URI in order to use this method.
 * @param namespaceURI
 *            The namespace of the resource being resolved, e.g. the
 *            target namespace of the XML Schema [XML Schema Part 1] when
 *            resolving XML Schema resources.
 * @param publicId
 *            The public identifier of the external entity being
 *            referenced, or null if no public identifier was supplied or if
 *            the resource is not an entity.
 * @param systemId
 *            The system identifier, a URI reference [IETF RFC 2396], of
 *            the external resource being referenced, or null if no system
 *            identifier was supplied.
 * @param baseURI
 *            The absolute base URI of the resource being parsed, or null
 *            if there is no base URI.
 * 
 * @return A LSInput,
 *         object describing the new input source, or null to
 *         request that the parser open a regular URI connection to the
 *         resource.
 * 
 */
public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId,
        String baseURI) {

    InputSource inputSource = null;
    if (log.isDebugEnabled()) {
        log.debug("Resolving Schema resource " + systemId);
    }

    // check with the registry schema resolver
    if (resourceMap == null) {
        for (int i = 0; i < schemaRegKeys.size(); i++) {
            RegistryDependency regWSDLDep = new RegistryDependency(schemaRegKeys.get(i).getKeyValue());
            Map<String, String> dependencyMap = regWSDLDep.getDependencies();
            if (dependencyMap.size() > 0) {
                Set<String> keys = dependencyMap.keySet();
                for (Iterator<String> itr = keys.iterator(); itr.hasNext();) {
                    String key = (String) itr.next();
                    String value = dependencyMap.get(key);
                    String constructedPath = regWSDLDep.constructRegistryPathToRelativePath(systemId);

                    if (value.endsWith(constructedPath)) {
                        if (resourceMap == null) {
                            resourceMap = new ResourceMap();
                        }
                        resourceMap.addResource(systemId, value);
                        break;
                    }
                }
                inputSource = resourceMap.resolve(synCfg, systemId);
            } else {
                // It comes here since there is no any other 'include'
                // schemas.
                // We need to resolve this schema(base schema without any
                // includes) also.
                resourceMap.addResource(systemId, schemaRegKeys.get(i).getKeyValue());
            }
        }

    } else {
        inputSource = resourceMap.resolve(synCfg, systemId);
    }
    if (inputSource == null) {
        log.warn("Unable to resolve schema resource " + systemId);
        return null;
    }
    SchemaResourceLSInput schemaResourceLSInput = new SchemaResourceLSInput();
    schemaResourceLSInput.setByteStream(inputSource.getByteStream());
    return schemaResourceLSInput;
}

From source file:org.xchain.framework.servlet.XChainManager.java

private static void close(InputSource inputSource) {
    try {// w ww .  j a v a2s.  c om
        if (inputSource != null && inputSource.getByteStream() != null) {
            close(inputSource.getByteStream());
        } else if (inputSource != null && inputSource.getCharacterStream() != null) {
            close(inputSource.getCharacterStream());
        }
    } catch (Throwable t) {
        if (log.isWarnEnabled()) {
            log.warn("Exception thrown while closing input source.");
        }
    }
}

From source file:org.zanata.adapter.glossary.GlossaryPoReader.java

static MessageStreamParser createParser(InputSource inputSource) {
    MessageStreamParser messageParser;//from w ww  .j a va2s  .c  o m
    if (inputSource.getCharacterStream() != null)
        messageParser = new MessageStreamParser(inputSource.getCharacterStream());
    else if (inputSource.getByteStream() != null) {
        if (inputSource.getEncoding() != null)
            messageParser = new MessageStreamParser(inputSource.getByteStream(),
                    Charset.forName(inputSource.getEncoding()));
        else
            messageParser = new MessageStreamParser(inputSource.getByteStream(), Charset.forName("UTF-8"));
    } else if (inputSource.getSystemId() != null) {
        try {
            URL url = new URL(inputSource.getSystemId());

            if (inputSource.getEncoding() != null)
                messageParser = new MessageStreamParser(url.openStream(),
                        Charset.forName(inputSource.getEncoding()));
            else
                messageParser = new MessageStreamParser(url.openStream(), Charset.forName("UTF-8"));
        } catch (IOException e) {
            throw new RuntimeException("failed to get input from url in inputSource", e);
        }
    } else
        throw new RuntimeException("not a valid inputSource");

    return messageParser;
}

From source file:org.zanata.adapter.po.PoReader2.java

static MessageStreamParser createParser(InputSource inputSource) {
    MessageStreamParser messageParser;/*from w  w  w.j a v  a  2 s. c  o m*/
    if (inputSource.getCharacterStream() != null)
        messageParser = new MessageStreamParser(inputSource.getCharacterStream());
    else if (inputSource.getByteStream() != null) {
        if (inputSource.getEncoding() != null)
            messageParser = new MessageStreamParser(inputSource.getByteStream(),
                    Charset.forName(inputSource.getEncoding()));
        else
            messageParser = new MessageStreamParser(inputSource.getByteStream(), Charset.forName("UTF-8"));
    } else if (inputSource.getSystemId() != null) {
        try {
            URL url = new URL(inputSource.getSystemId());

            if (inputSource.getEncoding() != null)
                messageParser = new MessageStreamParser(url.openStream(),
                        Charset.forName(inputSource.getEncoding()));
            else
                messageParser = new MessageStreamParser(url.openStream(), Charset.forName("UTF-8"));
        } catch (IOException e) {
            // TODO throw stronger typed exception
            throw new RuntimeException("failed to get input from url in inputSource", e);
        }
    } else
        // TODO throw stronger typed exception
        throw new RuntimeException("not a valid inputSource");

    return messageParser;
}

From source file:uk.ac.ebi.arrayexpress.utils.saxon.FlatFileXMLReader.java

public void parse(InputSource input) throws IOException, SAXException {
    int headerRows = getIntOptionValue(OPTION_HEADER_ROWS, 0);
    int page = getIntOptionValue(OPTION_PAGE, 0);
    int pageSize = getIntOptionValue(OPTION_PAGE_SIZE, -1);
    Integer sortBy = getIntOptionValue(OPTION_SORT_BY, null);
    String sortOrder = getStringOptionValue(OPTION_SORT_ORDER, "a");

    ContentHandler ch = getContentHandler();
    if (null == ch) {
        return;// w  w  w.j a  v  a2s  .  com
    }

    Reader inStream;
    if (input.getCharacterStream() != null) {
        inStream = input.getCharacterStream();
    } else if (input.getByteStream() != null) {
        inStream = new InputStreamReader(input.getByteStream());
    } else if (input.getSystemId() != null) {
        URL url = new URL(input.getSystemId());
        inStream = new InputStreamReader(url.openStream());
    } else {
        throw new SAXException("Invalid InputSource object");
    }

    CSVReader ffReader = new CSVReader(new BufferedReader(inStream), this.columnDelimiter,
            this.columnQuoteChar);

    List<String[]> ff = ffReader.readAll();
    int cols = ff.size() > 0 ? ff.get(0).length : 0;

    // verify that sort by column is with in range of columns
    // if not then sort will not be performed
    // else - switch from 1-based to 0-based index
    if (null != sortBy) {
        if (sortBy < 1 || sortBy > cols) {
            sortBy = null;
        } else {
            sortBy = sortBy - 1;
        }
    }

    // 1. removes all dodgy rows (that have less columns than the first one)
    // 2. determines if column to be sorted is numeric
    ColDataType sortColDataType = ColDataType.INTEGER;
    int colTypeSkipRows = headerRows;
    for (Iterator<String[]> iterator = ff.iterator(); iterator.hasNext();) {
        String[] row = iterator.next();
        if (row.length != cols || isRowBlank(row)) {
            iterator.remove();
        } else {
            if (null != sortBy && 0 == colTypeSkipRows && ColDataType.STRING != sortColDataType) {
                ColDataType dataType = getColDataType(row[sortBy]);

                // downgrade from int to decimal or string
                if (ColDataType.INTEGER == sortColDataType && ColDataType.INTEGER != dataType) {
                    sortColDataType = dataType;
                }
                // downgrade from decimal to string only
                if (ColDataType.DECIMAL == sortColDataType && ColDataType.STRING == dataType) {
                    sortColDataType = dataType;
                }
            }
            if (colTypeSkipRows > 0) {
                colTypeSkipRows--;
            }
        }
    }

    int rows = ff.size() > 0 ? ff.size() - headerRows : 0;

    if (-1 == pageSize) {
        page = 1;
        pageSize = rows;
    }

    ch.startDocument();

    AttributesImpl tableAttrs = new AttributesImpl();
    tableAttrs.addAttribute(EMPTY_NAMESPACE, "rows", "rows", CDATA_TYPE, String.valueOf(rows));
    ch.startElement(EMPTY_NAMESPACE, "table", "table", tableAttrs);

    for (Iterator<String[]> iterator = ff.iterator(); iterator.hasNext() && headerRows > 0; headerRows--) {
        String[] row = iterator.next();
        outputRow(ch, true, null, row);
        iterator.remove();
    }

    if (null != sortBy) {
        Collections.sort(ff, new SortColumnComparator(sortBy, sortOrder, sortColDataType));
    }

    int rowSeq = 1;
    for (String[] row : ff) {
        if (rowSeq > (pageSize * (page - 1)) && rowSeq <= (pageSize * page)) {
            outputRow(ch, false, String.valueOf(rowSeq), row);
        }
        ++rowSeq;
    }

    ch.endElement(EMPTY_NAMESPACE, "table", "table");
    ch.endDocument();
}