001/*
002// This software is subject to the terms of the Eclipse Public License v1.0
003// Agreement, available at the following URL:
004// http://www.eclipse.org/legal/epl-v10.html.
005// You must accept the terms of that agreement to use this software.
006//
007// Copyright (C) 2007-2009 Pentaho
008// All Rights Reserved.
009*/
010package mondrian.spi.impl;
011
012import mondrian.olap.Util;
013import mondrian.spi.DynamicSchemaProcessor;
014
015import java.io.*;
016
017/**
018 * Implementation of {@link DynamicSchemaProcessor} which allows a derived class
019 * to easily process a schema file.
020 *
021 * <p>Mondrian's default mechanism for loading schema files, if no
022 * DynamicSchemaProcessor is specified, is to use Apache VFS (virtual file
023 * system) to resolve the URL to a stream, and to read the contents of the
024 * stream into a string.
025 *
026 * <p>FilterDynamicSchemaProcessor implements exactly the
027 * same mechanism, but makes it easy for a derived class to override the
028 * mechanism. For example:<ul>
029 *
030 * <li>To redirect to a different URL, override the
031 * {@link #processSchema(String, mondrian.olap.Util.PropertyList)} method.
032 *
033 * <li>To process the contents of the URL, override the
034 * {@link #filter(String, mondrian.olap.Util.PropertyList, java.io.InputStream)}
035 * method.
036 *
037 * </ul>
038 *
039 * @author jhyde
040 * @since Mar 30, 2007
041 */
042public class FilterDynamicSchemaProcessor implements DynamicSchemaProcessor {
043
044    /**
045     * {@inheritDoc}
046     *
047     * <p>FilterDynamicSchemaProcessor's implementation of this method reads
048     * from the URL supplied (that is, it does not perform URL translation)
049     * and passes it through the {@link #filter} method.
050     */
051    public String processSchema(
052        String schemaUrl,
053        Util.PropertyList connectInfo) throws Exception
054    {
055        InputStream in = Util.readVirtualFile(schemaUrl);
056        return filter(schemaUrl, connectInfo, in);
057    }
058
059    /**
060     * Reads the contents of a schema as a stream and returns the result as
061     * a string.
062     *
063     * <p>The default implementation returns the contents of the schema
064     * unchanged.
065     *
066     * @param schemaUrl the URL of the catalog
067     * @param connectInfo Connection properties
068     * @param stream Schema contents represented as a stream
069     * @return the modified schema
070     * @throws Exception if an error occurs
071     */
072    protected String filter(
073        String schemaUrl,
074        Util.PropertyList connectInfo,
075        InputStream stream)
076        throws Exception
077    {
078        BufferedReader in =
079            new BufferedReader(
080                new InputStreamReader(stream));
081        try {
082            StringBuilder builder = new StringBuilder();
083            char[] buf = new char[2048];
084            int readCount;
085            while ((readCount = in.read(buf, 0, buf.length)) >= 0) {
086                builder.append(buf, 0, readCount);
087            }
088            return builder.toString();
089        } finally {
090            in.close();
091        }
092    }
093}
094
095// End FilterDynamicSchemaProcessor.java