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

org.eclipse.jetty.webapp.FragmentDescriptor Maven / Gradle / Ivy

There is a newer version: 1.0-rc5
Show newest version
// ========================================================================
// Copyright (c) 2006-2010 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
// The Eclipse Public License is available at 
// http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
// You may elect to redistribute this code under either of these licenses. 
// ========================================================================

package org.eclipse.jetty.webapp;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.xml.XmlParser;


/**
 * Fragment
 *
 * A web-fragment.xml descriptor.
 */
public class FragmentDescriptor extends WebDescriptor
{
    public static final String NAMELESS = "@@-NAMELESS-@@"; //prefix for nameless Fragments
    public enum OtherType {None, Before, After};
    
    protected int _counter = 0;
    protected OtherType _otherType = OtherType.None;
   
    
    protected List _befores = new ArrayList();
    protected List _afters = new ArrayList();
    protected String _name;
    

    public FragmentDescriptor (Resource xml)
        throws Exception
    {
        super (xml);
    }       
    
    public String getName ()
    {
        return _name;
    }
    
    @Override
    public void parse () 
        throws Exception
    {
        super.parse();
        processName();
    }
    
    public void processName ()
    {
        XmlParser.Node root = getRoot();
        XmlParser.Node nameNode = root.get("name");
        _name = NAMELESS+(_counter++);
        if (nameNode != null)
        {
            String tmp = nameNode.toString(false,true);
            if (tmp!=null && tmp.length()>0)
                _name = tmp;
        }
    }
    @Override
    public void processOrdering ()
    {
        //Process a fragment jar's web-fragment.xml elements
        XmlParser.Node root = getRoot();       
        
        XmlParser.Node ordering = root.get("ordering");
        if (ordering == null)
            return; //No ordering for this fragment
        
        _isOrdered = true;
   
        processBefores(ordering);
        processAfters(ordering);
    }
    
    
    public void processBefores (XmlParser.Node ordering)
    {
        //Process the  elements, looking for an  clause and all of the  clauses
        XmlParser.Node before = ordering.get("before");
        if (before == null)
            return;

        Iterator iter = before.iterator();
        XmlParser.Node node = null;
        while (iter.hasNext())
        {
            Object o = iter.next();
            if (!(o instanceof XmlParser.Node)) continue;
            node = (XmlParser.Node) o;
            if (node.getTag().equalsIgnoreCase("others"))
            {
                if (_otherType != OtherType.None)
                    throw new IllegalStateException("Duplicate  clause detected in "+_xml.getURI());

                _otherType = OtherType.Before;
            }
            else if (node.getTag().equalsIgnoreCase("name"))
                _befores.add(node.toString(false,true));
        }
    }

    public void processAfters (XmlParser.Node ordering)
    {
        //Process the  elements, look for an  clause and all of the  clauses
        XmlParser.Node after = ordering.get("after");
        if (after == null)
            return;
        
        Iterator iter = after.iterator();
        XmlParser.Node node = null;
        while (iter.hasNext())
        {
            Object o = iter.next();
            if (!(o instanceof XmlParser.Node)) continue;
            node = (XmlParser.Node) o;
            if (node.getTag().equalsIgnoreCase("others"))
            {
                if (_otherType != OtherType.After)
                    throw new IllegalStateException("Duplicate  clause detected in "+_xml.getURI());

                _otherType = OtherType.After;

            }
            else if (node.getTag().equalsIgnoreCase("name"))
                _afters.add(node.toString(false,true));
        }
    }
    
    public List getBefores()
    {
        return Collections.unmodifiableList(_befores);
    }
    
    public List getAfters()
    {
        return Collections.unmodifiableList(_afters);
    }
    
    public OtherType getOtherType ()
    {
        return _otherType;
    }
    
    public List getOrdering()
    {
        return null; //only used for absolute-ordering in Descriptor
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy