com.huawei.streaming.cql.executor.PhysicalPlanWriter.java Source code

Java tutorial

Introduction

Here is the source code for com.huawei.streaming.cql.executor.PhysicalPlanWriter.java

Source

/**
 * 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.
 */
package com.huawei.streaming.cql.executor;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.huawei.streaming.api.Application;
import com.huawei.streaming.api.PhysicalPlan;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.core.ClassLoaderReference;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.mapper.DefaultMapper;

/**
 * ??
 * 
 */
public class PhysicalPlanWriter {
    private static final Logger LOG = LoggerFactory.getLogger(PhysicalPlanLoader.class);

    /**
     * xml ??
     */
    private static final String XML_CHARSET = "utf-8";

    /**
     * XStreamDOMDriver?xml??
     */
    private static final String XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";

    /**
     * ?
     * @param app ?
     * @param file 
     */
    public static void write(Application app, String file) {
        PhysicalPlan plan = new PhysicalPlan();
        plan.setApploication(app);
        write(plan, file);
    }

    /**
     * 
     * @param plan 
     * @param file 
     */
    public static void write(PhysicalPlan plan, String file) {
        String s = createStringPlan(plan);
        try {
            FileUtils.writeStringToFile(new File(file), XML_HEADER + s, XML_CHARSET);
        } catch (IOException e) {
            LOG.error("failed to write physical plan to file {} for io error.", file);
        }
    }

    /**
     * ??
     * @param plan ?
     * @return ?
     */
    public static String createStringPlan(PhysicalPlan plan) {
        XStream xstream = new XStream(new DomDriver(XML_CHARSET));
        xstream.autodetectAnnotations(true);
        PhysicalPlanLoader.setAlias(xstream);
        xstream.registerConverter(new MapConverter(
                new DefaultMapper(new ClassLoaderReference(PhysicalPlanWriter.class.getClassLoader()))));
        return xstream.toXML(plan);
    }

    /**
     * ??
     * @param app ?
     * @return ?
     */
    public static String createStringPlan(Application app) {
        PhysicalPlan plan = new PhysicalPlan();
        plan.setApploication(app);
        return createStringPlan(plan);
    }

}