<commit_before>/**
 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
 * Licensed under the Common Development and Distribution License,
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.sun.com/cddl/
 *   
 * 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.sun.facelets.compiler;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.sun.facelets.FaceletHandler;
import com.sun.facelets.tag.Tag;
import com.sun.facelets.tag.TagAttribute;
import com.sun.facelets.tag.TagAttributeException;
import com.sun.facelets.tag.TagAttributes;
import com.sun.facelets.tag.TagDecorator;
import com.sun.facelets.tag.TagLibrary;
import com.sun.facelets.tag.ui.CompositionHandler;
import com.sun.facelets.tag.ui.UILibrary;

/**
 * Compilation unit for managing the creation of a single FaceletHandler based
 * on events from an XML parser.
 * 
 * @see com.sun.facelets.compiler.Compiler
 * 
 * @author <NAME>
 * @version $Id: CompilationManager.java,v 1.2 2005-07-20 05:27:45 jhook Exp $
 */
final class CompilationManager {

    private final static Logger log = Logger.getLogger("facelets.compiler");

    private final Compiler compiler;

    private final TagLibrary tagLibrary;

    private final TagDecorator tagDecorator;

    private final NamespaceManager namespaceManager;

    private final Stack units;

    private int tagId;
    
    private boolean finished;

    public CompilationManager(Compiler compiler) {

        // grab compiler state
        this.compiler = compiler;
        this.tagDecorator = compiler.createTagDecorator();
        this.tagLibrary = compiler.createTagLibrary();

        // namespace management
        this.namespaceManager = new NamespaceManager();

        // tag uids
        this.tagId = 0;
        
        // for composition use
        this.finished = false;

        // our compilationunit stack
        this.units = new Stack();
        this.units.push(new CompilationUnit());
    }

    public void writeText(String value) {
        TextUnit unit;
        if (this.currentUnit() instanceof TextUnit) {
            unit = (TextUnit) this.currentUnit();
        } else {
            unit = new TextUnit();
            this.startUnit(unit);
        }
        unit.write(value);
    }

    public void writeComment(String text) {
        if (!this.compiler.isTrimmingComments()) {
            this.writeText("<!-- " + text + " -->");
        }
    }

    public void writeWhitespace(String text) {
        if (!this.compiler.isTrimmingWhitespace()) {
            this.writeText(text);
        }
    }

    private String nextTagId() {
        return "_tagId" + (this.tagId++);
    }

    public void pushTag(Tag orig) {
        Tag t = this.tagDecorator.decorate(orig);
        t = this.processAttributes(t);

        boolean handled = false;

        if (isComposition(t)) {
            this.units.clear();
            NamespaceUnit nsUnit = this.namespaceManager
                    .toNamespaceUnit(this.tagLibrary);
            this.units.push(nsUnit);
            this.startUnit(new TagUnit(this.tagLibrary, t, this.nextTagId()));
        } else if (isRemove(t)) {
            this.units.push(new RemoveUnit());
        } else if (this.tagLibrary.containsTagHandler(t.getNamespace(), t
                .getLocalName())) {
            this.startUnit(new TagUnit(this.tagLibrary, t, this.nextTagId()));
        } else {
            TextUnit unit;
            if (this.currentUnit() instanceof TextUnit) {
                unit = (TextUnit) this.currentUnit();
            } else {
                unit = new TextUnit();
                this.startUnit(unit);
            }
            unit.startTag(t);
        }
    }

    public void popTag() {
        
        if (this.finished) {
            return;
        }
        
        CompilationUnit unit = this.currentUnit();

        if (unit instanceof TextUnit) {
            TextUnit t = (TextUnit) unit;
            if (t.isClosed()) {
                this.finishUnit();
            } else {
                t.endTag();
                return;
            }
        }
        
        unit = this.currentUnit();
        if (unit instanceof TagUnit) {
            TagUnit t = (TagUnit) unit;
            if (isComposition(t.getTag())) {
                this.finished = true;
            }
        }

        this.finishUnit();
    }

    public void popNamespace(String ns) {
        this.namespaceManager.popNamespace(ns);
        if (this.currentUnit() instanceof NamespaceUnit) {
            this.finishUnit();
        }
    }

    public void pushNamespace(String prefix, String uri) {
        this.namespaceManager.pushNamespace(prefix, uri);
        NamespaceUnit unit;
        if (this.currentUnit() instanceof NamespaceUnit) {
            unit = (NamespaceUnit) this.currentUnit();
        } else {
            unit = new NamespaceUnit(this.tagLibrary);
            this.startUnit(unit);
        }
        unit.setNamespace(prefix, uri);
    }

    public FaceletHandler createFaceletHandler() {
        return ((CompilationUnit) this.units.get(0)).createFaceletHandler();
    }

    private CompilationUnit currentUnit() {
        return (CompilationUnit) this.units.peek();
    }

    private void finishUnit() {
        this.units.pop();
    }

    private CompilationUnit searchUnits(Class type) {<commit_msg>fixed issue with compositions<commit_after>/**
 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
 * Licensed under the Common Development and Distribution License,
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.sun.com/cddl/
 *   
 * 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.sun.facelets.compiler;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.sun.facelets.FaceletHandler;
import com.sun.facelets.tag.Tag;
import com.sun.facelets.tag.TagAttribute;
import com.sun.facelets.tag.TagAttributeException;
import com.sun.facelets.tag.TagAttributes;
import com.sun.facelets.tag.TagDecorator;
import com.sun.facelets.tag.TagLibrary;
import com.sun.facelets.tag.ui.CompositionHandler;
import com.sun.facelets.tag.ui.UILibrary;

/**
 * Compilation unit for managing the creation of a single FaceletHandler based
 * on events from an XML parser.
 * 
 * @see com.sun.facelets.compiler.Compiler
 * 
 * @author <NAME>
 * @version $Id: CompilationManager.java,v 1.3 2005-07-21 02:08:57 jhook Exp $
 */
final class CompilationManager {

    private final static Logger log = Logger.getLogger("facelets.compiler");

    private final Compiler compiler;

    private final TagLibrary tagLibrary;

    private final TagDecorator tagDecorator;

    private final NamespaceManager namespaceManager;

    private final Stack units;

    private int tagId;
    
    private boolean finished;

    public CompilationManager(Compiler compiler) {

        // grab compiler state
        this.compiler = compiler;
        this.tagDecorator = compiler.createTagDecorator();
        this.tagLibrary = compiler.createTagLibrary();

        // namespace management
        this.namespaceManager = new NamespaceManager();

        // tag uids
        this.tagId = 0;
        
        // for composition use
        this.finished = false;

        // our compilationunit stack
        this.units = new Stack();
        this.units.push(new CompilationUnit());
    }

    public void writeText(String value) {
        TextUnit unit;
        if (this.currentUnit() instanceof TextUnit) {
            unit = (TextUnit) this.currentUnit();
        } else {
            unit = new TextUnit();
            this.startUnit(unit);
        }
        unit.write(value);
    }

    public void writeComment(String text) {
        if (!this.compiler.isTrimmingComments()) {
            this.writeText("<!-- " + text + " -->");
        }
    }

    public void writeWhitespace(String text) {
        if (!this.compiler.isTrimmingWhitespace()) {
            this.writeText(text);
        }
    }

    private String nextTagId() {
        return "_tagId" + (this.tagId++);
    }

    public void pushTag(Tag orig) {
        
        if (this.finished) {
            return;
        }
        
        if (log.isLoggable(Level.FINE)) {
            log.fine("Tag Pushed: "+orig);
        }
        
        Tag t = this.tagDecorator.decorate(orig);
        t = this.processAttributes(t);

        boolean handled = false;

        if (isComposition(t)) {
            log.fine("Composition Found, Popping Parent Tags");
            this.units.clear();
            NamespaceUnit nsUnit = this.namespaceManager
                    .toNamespaceUnit(this.tagLibrary);
            this.units.push(nsUnit);
            this.startUnit(new TagUnit(this.tagLibrary, t, this.nextTagId()));
            log.fine("New Namespace and [Composition] TagUnit pushed");
        } else if (isRemove(t)) {
            this.units.push(new RemoveUnit());
        } else if (this.tagLibrary.containsTagHandler(t.getNamespace(), t
                .getLocalName())) {
            this.startUnit(new TagUnit(this.tagLibrary, t, this.nextTagId()));
        } else {
            TextUnit unit;
            if (this.currentUnit() instanceof TextUnit) {
                unit = (TextUnit) this.currentUnit();
            } else {
                unit = new TextUnit();
                this.startUnit(unit);
            }
            unit.startTag(t);
        }
    }

    public void popTag() {
        
        if (this.finished) {
            return;
        }
        
        CompilationUnit unit = this.currentUnit();

        if (unit instanceof TextUnit) {
            TextUnit t = (TextUnit) unit;
            if (t.isClosed()) {
                this.finishUnit();
            } else {
                t.endTag();
                return;
            }
        }
        
        unit = this.currentUnit();
        if (unit instanceof TagUnit) {
            TagUnit t = (TagUnit) unit;
            if (isComposition(t.getTag())) {
                this.finished = true;
                return;
            }
        }

        this.finishUnit();
    }

    public void popNamespace(String ns) {
        this.namespaceManager.popNamespace(ns);
        if (this.currentUnit() instanceof NamespaceUnit) {
            this.finishUnit();
        }
    }

    public void pushNamespace(String prefix, String uri) {
        
        if (log.isLoggable(Level.FINE)) {
            log.fine("Namespace Pushed "+prefix+": "+uri);
        }
        
        this.namespaceManager.pushNamespace(prefix, uri);
        NamespaceUnit unit;
        if (this.currentUnit() instanceof NamespaceUnit) {
            unit = (NamespaceUnit) this.currentUnit();
        } else {
            unit = new NamespaceUnit(this.tagLibrary);
            this.startUnit(unit);
        }
        unit.setNamespace(prefix, uri);
    }

    public FaceletHandler createFaceletHandler() {
        return ((CompilationUnit) this.units.get(0)).createFaceletHandler();
    }

    private CompilationUnit currentUnit() {
        if (!this.units.isEmpty()) {
            return (CompilationUnit) this.units.peek();
        }
        return null;
    }

    private void finishUnit() {
        this.units.pop();
    }

    private CompilationUnit searchUnits(Class type) {
{'fraction_non_alphanumeric': 0.10331191300049432, 'fraction_numerical': 0.003624979403526116, 'mean_word_length': 2.1173600410888547, 'pattern_counts': {'":': 1, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3225017', 'n_tokens_mistral': 3384, 'n_tokens_neox': 3249, 'n_words': 833}