[Castore-commits] PointToPointSchema.java NONE 1.1

Céline BENOIT cbenoit at adullact1.hosting.cri74.org
Mer 22 Mar 15:09:39 CET 2006


Update of /cvsroot/castore/castore-core/src/java/fr/emn/castor/documents/metier/textes/retro/pointtopointschema
In directory adullact1:/tmp/cvs-serv7150/src/java/fr/emn/castor/documents/metier/textes/retro/pointtopointschema

Added Files:
	PointToPointSchema.java 
Log Message:
déplacement du fichier

--- NEW FILE: PointToPointSchema.java ---
/*
 * $Id: PointToPointSchema.java,v 1.1 2006/03/22 14:09:37 cbenoit Exp $
 *
 * Plateforme CASTORE
 * CeCILL Copyright (C) 2005-2006 by EMN
 * Made by Stéphane Bouchet, Xerox
 * Web site = http://www.emn.fr/castore
 * Contact = Cédric Dumas, e-mail = Cedric.Dumas at emn.fr
 *
 * Version 1.0 (1er mars 2005)
 *
 * Ce logiciel est un programme informatique servant à créer une plateforme
 * open-source de bibliothèque numérique XML pour Conserver, Valoriser et
 * Diffuser le patrimoine documentaire de votre institut.
 *
 * Ce logiciel est régi par la licence CeCILL soumise au droit français et
 * respectant les principes de diffusion des logiciels libres. Vous pouvez
 * utiliser, modifier et/ou redistribuer ce programme sous les conditions de la
 * licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA sur le site
 * "http://www.cecill.info". En contrepartie de l'accessibilité au code source
 * et des droits de copie, de modification et de redistribution accordés par
 * cette licence, il n'est offert aux utilisateurs qu'une garantie limitée. Pour
 * les mêmes raisons, seule une responsabilité restreinte pèse sur l'auteur du
 * programme, le titulaire des droits patrimoniaux et les concédants successifs.
 * A cet égard l'attention de l'utilisateur est attirée sur les risques associés
 * au chargement, à l'utilisation, à la modification et/ou au développement et à
 * la reproduction du logiciel par l'utilisateur étant donné sa spécificité de
 * logiciel libre, qui peut le rendre complexe à manipuler et qui le réserve
 * donc à des développeurs et des professionnels avertis possédant des
 * connaissances informatiques approfondies. Les utilisateurs sont donc invités
 * à charger et tester l'adéquation du logiciel à leurs besoins dans des
 * conditions permettant d'assurer la sécurité de leurs systèmes et ou de leurs
 * données et, plus généralement, à l'utiliser et l'exploiter dans les mêmes
 * conditions de sécurité. Le fait que vous puissiez accéder à cet en-tête
 * signifie que vous avez pris connaissance de la licence CeCILL, et que vous en
 * avez accepté les termes.
 *
 */
package fr.emn.castor.documents.metier.textes.retro.pointtopointschema;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Namespace;

import fr.emn.castor.documents.metier.textes.retro.tools.XMLUtils;

/**
 * @author  rdelambe  Java class corresponding to the PointToPoint xml file
 */
public class PointToPointSchema {

    /** le logger.*/
    private static Log logger = LogFactory.getLog(PointToPointSchema.class);

    /**
     * Default constructor
     *  
     */
    public PointToPointSchema() {
        // initialize members
        ppsXMLFilePath = "";
        allowedRootElements = new TreeMap<String, PpsElement>();
        elements = new TreeMap<String, PpsElement>();
    }

    /**
     * Constructor
     * 
     * @param theFilePath
     */
    public PointToPointSchema(String theFilePath) {
        // initialize members
        ppsXMLFilePath = theFilePath;
        allowedRootElements = new TreeMap<String, PpsElement>();
        elements = new TreeMap<String, PpsElement>();
    }

    /**
     * Class members
     */

    // the XML file path
    protected String ppsXMLFilePath;

    // the allowed root elements (as a Map)
    /**
     */
    protected Map<String, PpsElement> allowedRootElements;

    // the elements of the document (as a Map)
    /**
     */
    protected Map<String, PpsElement> elements;

    /**
     * Methods
     */

    /**
     * load XML File into the java class
     *  
     */
    public void load() {

        Document ppsDoc = XMLUtils.getDocumentFromXMLFile(ppsXMLFilePath);

        // Get the node list of this document
        // it looks like <AllowedRootElements><Element>...<Element>

        Element ppsRoot = ppsDoc.getRootElement();

        // /PointToPointSchema/AllowedRootElements        
        Element theAllowedRootElementsNode = ppsRoot.getChild(
            ALLOWED_ROOT_ELEMENTS, NAMESPACE);

        // populate "allowedRootElements" attribute list; there is only one
        // "AllowRootElements" node
        if (theAllowedRootElementsNode != null)
            populateAllowedRootElements(theAllowedRootElementsNode);

        // the following nodes are "Element" nodes
        // create a vector containing all "Element" nodes, and only these nodes
        List ElementsDefinition = ppsRoot.getChildren(
            ELEMENT_DEFINITION, NAMESPACE);
        int elementsDefinitionCount = ElementsDefinition.size();

        // First pass to create empty PpsElement in order
        // to be able, in a second pass, to create the lists 
        // of element associated with an element (allowedChild,
        // allowedFirstChild, ...) as lists of PPsELement
        PpsElement ppsElem;
        for (int i = 1; i < elementsDefinitionCount; i++) {
            Element ithElement = (Element) ElementsDefinition.get(i);
            String ithElementName = ithElement.getAttributeValue(ATTR_NAME);
            // Create ppsElement for this element
            ppsElem = new PpsElement(this);
            // Add it to the "elements" attribute list
            elements.put(ithElementName, ppsElem);
        }
        // Add the Text element (named '_Text')
        ppsElem = new PpsElement(this);
        elements.put(TEXT_NAME, ppsElem);

        // Second pass to load information from input PointToPointSchema 
        // into PpsElements created in first pass        
        for (int i = 1; i < elementsDefinitionCount; i++) {
            Element ithElement = (Element) ElementsDefinition.get(i);
            String ithElementName = ithElement.getAttributeValue(ATTR_NAME);
            ppsElem = (PpsElement) elements.get(ithElementName);
            // Load information from input PointToPointSchema
            ppsElem.load(ithElement);
        }
    }

    /**
     * populate the allowedRootElements list
     * 
     * @param theAllowedRootElementsNode
     */
    private void populateAllowedRootElements(
            Element theAllowedRootElementsElement) {

        // <allowedRootElements><element>XXX</element><element>YYY</element></allowedRootElements>
        List theAllowedRootElementList = theAllowedRootElementsElement
            .getChildren(ELEMENT, NAMESPACE);

        for (int i = 0; i < theAllowedRootElementList.size(); i++) {
            Element ithAllowedRootElement = (Element) theAllowedRootElementList
                .get(i);
            // Create a PpsElement for this element
            PpsElement ppsElem = new PpsElement(this);
            // Load it
            ppsElem.load(ithAllowedRootElement);
            // Add it to the "allowedRootElements" attribute list
            allowedRootElements.put(ithAllowedRootElement
                .getAttributeValue(ATTR_NAME), ppsElem);
        }
    }

    /**
     * getter method for ppsXMLFilePath class member
     * @return
     */
    public String getPpsXMLFilePath() {
        return ppsXMLFilePath;
    }

    /**
     * setter method for ppsXMLFilePath class member
     * @param  thePath
     */
    public void setPpsXMLFilePath(String thePath) {
        ppsXMLFilePath = thePath;
    }

    /**
     * getter method for allowedRootElements class member
     * @return  the list of the allowed root elements
     */
    public Collection getAllowedRootElements() {
        return allowedRootElements.values();
    }

    /**
     * getter method for elements class member
     * @return  the list of the elements
     */
    public Collection getElements() {
        return elements.values();
    }

    /**
     * 
     * @param elementName
     * @return the element if it is found, null otherwise
     */
    public PpsElement getElement(String elementName) {
        if (elements.containsKey(elementName)) {
            return (PpsElement) elements.get(elementName);
        } else {
            logger.info(ELEMENT_NOT_FOUND + elementName);
            return null;
        }
    }

    /**
     * 
     * @param elementName
     * @return true if an element named "elementName" can be root element, false
     *         otherwise
     */
    public boolean allowedRootElement(String elementName) {
        return allowedRootElements.containsKey(elementName);
    }

    public static String getName(Element element) {
        String name;
        if (element.getName() == TEXT) {
            name = TEXT_NAME;
        } else {
            name = element.getAttributeValue(ATTR_NAME);
        }
        return name;
    }

    public void display() {
        logger.debug("PointToPointSchema");
        displayAllowedRootElements();
        displayElements();
        logger.debug("end of PointToPointSchema");
    }

    /**
     * Display allowed root elements
     *  
     */
    private void displayAllowedRootElements() {
        logger.debug("@allowedRootElements");
        for (Iterator iter = allowedRootElements.entrySet().iterator(); iter
            .hasNext();) {
            PpsElement element = (PpsElement) iter.next();
            element.display();
        }
        logger.debug("end of @allowedRootElements");
    }

    /**
     * Display elements
     *  
     */
    private void displayElements() {
        logger.debug("@elements");
        for (Iterator iter = elements.entrySet().iterator(); iter.hasNext();) {
            PpsElement element = (PpsElement) iter.next();
            element.display();
        }
        logger.debug("end of @elements");
    }

    /**
     * Class constants
     */
    private static final String ALLOWED_ROOT_ELEMENTS = "AllowedRootElements";

    private static final String ELEMENT_DEFINITION = "ElementDefinition";

    private static final String ELEMENT = "Element";

    private static final String ELEMENT_NOT_FOUND = "PointToPointSchema does not contain : ";

    // Attribute names
    private static final String ATTR_NAME = "name";

    // XML Tags
    private static final String TEXT = "Text";

    // Misc
    public static final String TEXT_NAME = "_Text";

    public static Namespace NAMESPACE = Namespace
        .getNamespace("urn:xgs:names:reverse:xmlns:pps:1.0");
}




Plus d'informations sur la liste de diffusion Castore-commits