All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.flex.compiler.tools.unknowntreehandler.UnknownTreePatternInputOutput Maven / Gradle / Ivy

The newest version!
/*
 *  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 org.apache.flex.compiler.tools.unknowntreehandler;

import java.io.PrintWriter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.ext.DefaultHandler2;

/**
 *  UnknownTreePatternInputOutput is a support class that reads
 *  an XML file with template data and populates a map of finding templates,
 *  and may also write this map out as Java code to be copied into
 *  the UnknownTreeHandler as hard-coded patterns.
 */
class UnknownTreePatternInputOutput extends DefaultHandler2
{
    /**
     *  The map of templates by node ID to read into.
     */
    Map> destination;

    /**
     *  Package name to use.  Hard-coded for now.
     */
    String packageName = "org.apache.flex.compiler.internal.as.codegen";

    /**
     *  Class name to use.  Also hard-coded.
     */
    String className = "UnknownTreeHandlerPatterns";

    /**
     *  Emitter to use.  Hard-coded.
     */
    String emitterName = "org.apache.flex.compiler.internal.as.codegen.CmcEmitter";

    /**
     *  Load a map of templates from an XML file.
     *  @param pattern_file - the path of the XML pattern file.
     *  @param dest - the destination map.
     */
    boolean load(String pattern_file, Map> dest)
    {
        this.destination = dest;

        try
        {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            factory.setNamespaceAware(true);

            SAXParser parser = factory.newSAXParser();
            parser.parse( new java.io.FileInputStream(pattern_file), this);
            return true;
        }
        catch ( Throwable load_failed )
        {
            System.err.println("Load of " + pattern_file + " failed!");
            load_failed.printStackTrace();
            return false;
        }
    }

    /**
     *  This stack tracks UnknownTreeFindingTemplate objects from 
     *  their startElement event to their endElement event; 
     *  it's used to create the pattern/subpattern hierarchy.
     */
    Stack