Example usage for org.jdom2 Element getAttributeValue

List of usage examples for org.jdom2 Element getAttributeValue

Introduction

In this page you can find the example usage for org.jdom2 Element getAttributeValue.

Prototype

public String getAttributeValue(final String attname) 

Source Link

Document

This returns the attribute value for the attribute with the given name and within no namespace, null if there is no such attribute, and the empty string if the attribute value is empty.

Usage

From source file:delfos.io.xml.parameterowner.ParameterOwnerXML.java

License:Open Source License

/**
 * Construye el objeto en memoria que representa al sistema de recomendacin descrito en el elemento que se pasa por
 * parmetro./*from   w  w  w  . j a  va 2s.  c  o m*/
 *
 * @param parameterOwnerElement Objeto XML con la informacin para recuperar el objeto.
 *
 * @return Sistema de recomendacin generado a partir de la descripcin pasada por parmetro
 */
public static ParameterOwner getParameterOwner(Element parameterOwnerElement) {

    String className = parameterOwnerElement.getAttributeValue(PARAMETER_OWNER_ATTRIBUTE_NAME);
    String typeName = parameterOwnerElement.getAttributeValue(PARAMETER_OWNER_ATTRIBUTE_TYPE);
    ParameterOwnerType parameterOwnerType = ParameterOwnerType.valueOf(typeName);

    ParameterOwner parameterOwner = parameterOwnerType.createObjectFromClassName(className);

    for (Element parameterElement : parameterOwnerElement.getChildren(ParameterXML.PARAMETER_ELEMENT_NAME)) {
        final String parameterName = parameterElement.getAttributeValue(ParameterXML.PARAMETER_NAME);

        Parameter parameter = parameterOwner.getParameterByName(parameterName);
        if (parameter != null) {
            Object parameterValue = ParameterXML.getParameterValue(parameterOwner, parameterElement);
            if (parameterValue == null) {
                parameterValue = ParameterXML.getParameterValue(parameterOwner, parameterElement);
            }

            parameterOwner.setParameterValue(parameter, parameterValue);
        }
    }
    return parameterOwner;

}

From source file:delfos.io.xml.persistencemethod.PersistenceMethodXML.java

License:Open Source License

/**
 * Devuelve el mtodo de persistencia que hay descrito en el elemento XML
 * que se pasa por parmetro/*from   w  w  w  .  j  a  v  a  2s  .c  o m*/
 *
 * @param persistenceElement Elemento XML con la informacin para recuperar
 * el mtodo de persistencia
 * @return Mtodo de persistencia que se encuentra descrito en el elemento
 */
public static PersistenceMethod getPersistenceMethod(Element persistenceElement) {

    String persistenceType = persistenceElement.getAttributeValue(PERSISTENCE_METHOD_TYPE_ATTRIBUTE);

    if (DatabasePersistence.class.getSimpleName().equals(persistenceType)) {
        return getDatabasePersistence(persistenceElement);
    }

    if (FilePersistence.class.getSimpleName().equals(persistenceType)) {
        return getFilePersistence(persistenceElement);
    }

    throw new IllegalArgumentException("Unknow persistence method '" + persistenceType + "'");
}

From source file:delfos.io.xml.persistencemethod.PersistenceMethodXML.java

License:Open Source License

/**
 * Devuelve la persistencia en base de datos que hay descrita en el elemento
 * XML que se pasa por parmetro//from w  w  w.  jav  a2 s. c o m
 *
 * @param databasePersistenceElement Elemento XML con la informacin para
 * recuperar la persistencia en base de datos.
 * @return Persistencia en base de datosque se encuentra descrito en el
 * elemento
 */
public static DatabasePersistence getDatabasePersistence(Element databasePersistenceElement) {
    String persistenceType = databasePersistenceElement.getAttributeValue(PERSISTENCE_METHOD_TYPE_ATTRIBUTE);
    if (!persistenceType.equals(DatabasePersistence.class.getSimpleName())) {
        throw new IllegalArgumentException(
                "Wrong element, not a '" + DatabasePersistence.class.getSimpleName() + "'");
    }

    String serverName = databasePersistenceElement.getAttribute(DATABASE_PERSISTENCE_SERVER_NAME).getValue();
    int port = Integer.parseInt(databasePersistenceElement.getAttribute(DATABASE_PERSISTENCE_PORT).getValue());
    String databaseName = databasePersistenceElement.getAttribute(DATABASE_PERSISTENCE_DATABASE_NAME)
            .getValue();
    String prefix = databasePersistenceElement.getAttribute(DATABASE_PERSISTENCE_PREFIX).getValue();
    String user = databasePersistenceElement.getAttribute(DATABASE_PERSISTENCE_USER).getValue();
    String pass = databasePersistenceElement.getAttribute(DATABASE_PERSISTENCE_PASSWORD).getValue();

    return new DatabasePersistence(user, pass, databaseName, serverName, port, prefix);
}

From source file:delfos.io.xml.recommendationcandidatesselector.RecommendationCandidatesSelectorXML.java

License:Open Source License

public static RecommendationCandidatesSelector getRecommendationsCandidatesSelector(
        Element recommendationCandidatesSelectorElement) {
    String name = recommendationCandidatesSelectorElement
            .getAttributeValue(ParameterOwnerXML.PARAMETER_OWNER_ATTRIBUTE_NAME);
    String parameterOwnerType = recommendationCandidatesSelectorElement
            .getAttributeValue(ParameterOwnerXML.PARAMETER_OWNER_ATTRIBUTE_TYPE);
    if (parameterOwnerType == null) {
        recommendationCandidatesSelectorElement.setAttribute(ParameterOwnerXML.PARAMETER_OWNER_ATTRIBUTE_TYPE,
                ParameterOwnerType.RECOMMENDATIONS_OUTPUT_METHOD.name());
    }//from  www.  j  a  v  a2  s. co  m
    ParameterOwner parameterOwner = ParameterOwnerXML
            .getParameterOwner(recommendationCandidatesSelectorElement);
    if (parameterOwner instanceof RecommendationCandidatesSelector) {
        RecommendationCandidatesSelector recommendationCandidatesSelector = (RecommendationCandidatesSelector) parameterOwner;
        return recommendationCandidatesSelector;
    } else {
        IllegalStateException ex = new IllegalStateException(
                "The XML does not have the expected structure: The loaded parameter owner is not a recommendation candidate selector["
                        + parameterOwner + "]");
        ERROR_CODES.CANNOT_LOAD_CONFIG_FILE.exit(ex);
        throw ex;
    }
}

From source file:delfos.io.xml.recommendations.RecommdendationsOutputMethodXML.java

License:Open Source License

/**
 * Construye el objeto en memoria que representa el mtodo de salida de
 * recomendaciones descrito en el elemento que se pasa por parmetro.
 *
 * @param recommdendationsOutputMethodElement Objeto XML que contienen la
 * descripcin.//w  ww. ja  v  a2 s.co  m
 * @return Mtodo de salida de recomendaciones obtenido a partir del
 * elemento XML.
 */
public static RecommendationsOutputMethod getRecommdendationsOutputMethod(
        Element recommdendationsOutputMethodElement) {
    String name = recommdendationsOutputMethodElement
            .getAttributeValue(ParameterOwnerXML.PARAMETER_OWNER_ATTRIBUTE_NAME);
    String parameterOwnerType = recommdendationsOutputMethodElement
            .getAttributeValue(ParameterOwnerXML.PARAMETER_OWNER_ATTRIBUTE_TYPE);
    if (parameterOwnerType == null) {
        recommdendationsOutputMethodElement.setAttribute(ParameterOwnerXML.PARAMETER_OWNER_ATTRIBUTE_TYPE,
                ParameterOwnerType.RECOMMENDATIONS_OUTPUT_METHOD.name());
    }
    ParameterOwner parameterOwner = ParameterOwnerXML.getParameterOwner(recommdendationsOutputMethodElement);
    if (parameterOwner instanceof RecommendationsOutputMethod) {
        RecommendationsOutputMethod recommendationsOutputMethod = (RecommendationsOutputMethod) parameterOwner;
        return recommendationsOutputMethod;
    } else {
        IllegalStateException ex = new IllegalStateException(
                "The XML does not have the expected structure: The loaded parameter owner is not a recommendation output method ["
                        + parameterOwner + "]");
        ERROR_CODES.CANNOT_LOAD_CONFIG_FILE.exit(ex);
        throw ex;
    }
}

From source file:delfos.io.xml.recommendations.RecommendationsToXML.java

License:Open Source License

/**
 * Convierte el elemento XML indicado en un objeto que representa las recomendaciones al usuario.
 *
 * @param element Elemento XML a convertir.
 * @return Recomendaciones./*  w  ww  .j  a  va 2s  .  com*/
 *
 * @throws IllegalArgumentException Si el elemento no contiene la informacin necesaria para recuperar un objeto
 * {@link Recommendations}.
 *
 * @see RecommendationsToXML#getRecommendationsElement(delfos.RS.Recommendation.Recommendations)
 *
 */
public static Recommendations getRecommendations(Element element) {

    if (!element.getName().equals(RECOMMENDATIONS_ELEMENT_NAME)) {
        throw new IllegalArgumentException("Element name doesn't match this reader: found '" + element.getName()
                + "' expected '" + RECOMMENDATIONS_ELEMENT_NAME + "'");
    }

    String idTarget = element.getAttributeValue(ID_TARGET_ATTRIBUTE_NAME);

    Map<DetailField, Object> details = new TreeMap<>();
    for (Attribute attribute : element.getAttributes()) {

        if (ID_TARGET_ATTRIBUTE_NAME.equals(attribute.getName())) {
            continue;
        }

        DetailField detailField = DetailField.valueOfNoCase(attribute.getName());

        String detailFieldValueString = attribute.getValue();
        Object detailFieldValue = detailField.parseValue(detailFieldValueString);

        details.put(detailField, detailFieldValue);

    }

    RecommendationComputationDetails recommendationComputationDetails = new RecommendationComputationDetails(
            details);
    List<Recommendation> recommendations = new LinkedList<>();
    for (Object r : element.getChildren(RECOMMENDATION_ELEMENT_NAME)) {
        Element recommendationElement = (Element) r;

        if (!recommendationElement.getName().equals(RECOMMENDATION_ELEMENT_NAME)) {
            throw new IllegalArgumentException("Element name doesn't match this reader: found '"
                    + recommendationElement.getName() + "' expected '" + RECOMMENDATION_ELEMENT_NAME + "'");
        }
        int idItem = Integer.parseInt(recommendationElement.getAttributeValue(ID_ITEM_ATTRIBUTE_NAME));
        double preference = Double
                .parseDouble(recommendationElement.getAttributeValue(PREFERENCE_ATTRIBUTE_NAME));
        recommendations.add(new Recommendation(idItem, preference));
    }

    return RecommendationsFactory.createRecommendations(idTarget, recommendations,
            recommendationComputationDetails);
}