Example usage for org.dom4j Element getTextTrim

List of usage examples for org.dom4j Element getTextTrim

Introduction

In this page you can find the example usage for org.dom4j Element getTextTrim.

Prototype

String getTextTrim();

Source Link

Document

DOCUMENT ME!

Usage

From source file:org.hibernate.cfg.annotations.reflection.JPAOverriddenAnnotationReader.java

License:LGPL

private static void copyStringElement(Element element, AnnotationDescriptor ad, String annotationAttribute) {
    String discr = element.getTextTrim();
    ad.setValue(annotationAttribute, discr);
}

From source file:org.hibernate.cfg.annotations.reflection.JPAOverridenAnnotationReader.java

License:Open Source License

private void getOrderBy(List<Annotation> annotationList, Element element) {
    Element subelement = element != null ? element.element("order-by") : null;
    if (subelement != null) {
        String orderByString = subelement.getTextTrim();
        AnnotationDescriptor ad = new AnnotationDescriptor(OrderBy.class);
        if (StringHelper.isNotEmpty(orderByString))
            ad.setValue("value", orderByString);
        annotationList.add(AnnotationFactory.create(ad));
    }/*from   w  w w .  ja v a 2s  . c  o  m*/
}

From source file:org.hibernate.cfg.annotations.reflection.JPAOverridenAnnotationReader.java

License:Open Source License

public static List buildNamedQueries(Element element, boolean isNative, XMLContext.Default defaults) {
    if (element == null)
        return new ArrayList();
    List namedQueryElementList = isNative ? element.elements("named-native-query")
            : element.elements("named-query");
    List namedQueries = new ArrayList();
    Iterator it = namedQueryElementList.listIterator();
    while (it.hasNext()) {
        Element subelement = (Element) it.next();
        AnnotationDescriptor ann = new AnnotationDescriptor(
                isNative ? NamedNativeQuery.class : NamedQuery.class);
        copyStringAttribute(ann, subelement, "name", false);
        Element queryElt = subelement.element("query");
        if (queryElt == null)
            throw new AnnotationException("No <query> element found." + SCHEMA_VALIDATION);
        ann.setValue("query", queryElt.getTextTrim());
        List<Element> elements = subelement.elements("hint");
        List<QueryHint> queryHints = new ArrayList<QueryHint>(elements.size());
        for (Element hint : elements) {
            AnnotationDescriptor hintDescriptor = new AnnotationDescriptor(QueryHint.class);
            String value = hint.attributeValue("name");
            if (value == null)
                throw new AnnotationException("<hint> without name. " + SCHEMA_VALIDATION);
            hintDescriptor.setValue("name", value);
            value = hint.attributeValue("value");
            if (value == null)
                throw new AnnotationException("<hint> without value. " + SCHEMA_VALIDATION);
            hintDescriptor.setValue("value", value);
            queryHints.add((QueryHint) AnnotationFactory.create(hintDescriptor));
        }/* www  . ja  v a 2 s  .c  o m*/
        ann.setValue("hints", queryHints.toArray(new QueryHint[queryHints.size()]));
        String clazzName = subelement.attributeValue("result-class");
        if (StringHelper.isNotEmpty(clazzName)) {
            Class clazz;
            try {
                clazz = ReflectHelper.classForName(XMLContext.buildSafeClassName(clazzName, defaults),
                        JPAOverridenAnnotationReader.class);
            } catch (ClassNotFoundException e) {
                throw new AnnotationException("Unable to find entity-class: " + clazzName, e);
            }
            ann.setValue("resultClass", clazz);
        }
        copyStringAttribute(ann, subelement, "result-set-mapping", false);
        namedQueries.add(AnnotationFactory.create(ann));
    }
    return namedQueries;
}

From source file:org.hibernate.cfg.annotations.reflection.JPAOverridenAnnotationReader.java

License:Open Source License

private static void buildUniqueConstraints(AnnotationDescriptor annotation, Element element) {
    List uniqueConstraintElementList = element.elements("unique-constraint");
    UniqueConstraint[] uniqueConstraints = new UniqueConstraint[uniqueConstraintElementList.size()];
    int ucIndex = 0;
    Iterator ucIt = uniqueConstraintElementList.listIterator();
    while (ucIt.hasNext()) {
        Element subelement = (Element) ucIt.next();
        List<Element> columnNamesElements = subelement.elements("column-name");
        String[] columnNames = new String[columnNamesElements.size()];
        int columnNameIndex = 0;
        Iterator it = columnNamesElements.listIterator();
        while (it.hasNext()) {
            Element columnNameElt = (Element) it.next();
            columnNames[columnNameIndex++] = columnNameElt.getTextTrim();
        }//w  w w.  j av a  2s.c  o m
        AnnotationDescriptor ucAnn = new AnnotationDescriptor(UniqueConstraint.class);
        ucAnn.setValue("columnNames", columnNames);
        uniqueConstraints[ucIndex++] = AnnotationFactory.create(ucAnn);
    }
    annotation.setValue("uniqueConstraints", uniqueConstraints);
}

From source file:org.hibernate.cfg.annotations.reflection.XMLContext.java

License:LGPL

/**
 * @param doc The xml document to add/*from   w w  w .ja  va2s  .  com*/
 * @return Add a xml document to this context and return the list of added class names.
 */
@SuppressWarnings("unchecked")
public List<String> addDocument(Document doc) {
    hasContext = true;
    List<String> addedClasses = new ArrayList<String>();
    Element root = doc.getRootElement();
    //global defaults
    Element metadata = root.element("persistence-unit-metadata");
    if (metadata != null) {
        if (globalDefaults == null) {
            globalDefaults = new Default();
            globalDefaults.setMetadataComplete(
                    metadata.element("xml-mapping-metadata-complete") != null ? Boolean.TRUE : null);
            Element defaultElement = metadata.element("persistence-unit-defaults");
            if (defaultElement != null) {
                Element unitElement = defaultElement.element("schema");
                globalDefaults.setSchema(unitElement != null ? unitElement.getTextTrim() : null);
                unitElement = defaultElement.element("catalog");
                globalDefaults.setCatalog(unitElement != null ? unitElement.getTextTrim() : null);
                unitElement = defaultElement.element("access");
                setAccess(unitElement, globalDefaults);
                unitElement = defaultElement.element("cascade-persist");
                globalDefaults.setCascadePersist(unitElement != null ? Boolean.TRUE : null);
                unitElement = defaultElement.element("delimited-identifiers");
                globalDefaults.setDelimitedIdentifiers(unitElement != null ? Boolean.TRUE : null);
                defaultEntityListeners.addAll(addEntityListenerClasses(defaultElement, null, addedClasses));
            }
        } else {
            LOG.duplicateMetadata();
        }
    }

    //entity mapping default
    Default entityMappingDefault = new Default();
    Element unitElement = root.element("package");
    String packageName = unitElement != null ? unitElement.getTextTrim() : null;
    entityMappingDefault.setPackageName(packageName);
    unitElement = root.element("schema");
    entityMappingDefault.setSchema(unitElement != null ? unitElement.getTextTrim() : null);
    unitElement = root.element("catalog");
    entityMappingDefault.setCatalog(unitElement != null ? unitElement.getTextTrim() : null);
    unitElement = root.element("access");
    setAccess(unitElement, entityMappingDefault);
    defaultElements.add(root);

    setLocalAttributeConverterDefinitions(root.elements("converter"));

    List<Element> entities = root.elements("entity");
    addClass(entities, packageName, entityMappingDefault, addedClasses);

    entities = root.elements("mapped-superclass");
    addClass(entities, packageName, entityMappingDefault, addedClasses);

    entities = root.elements("embeddable");
    addClass(entities, packageName, entityMappingDefault, addedClasses);
    return addedClasses;
}

From source file:org.hibernate.cfg.annotations.reflection.XMLContext.java

License:LGPL

private void setAccess(Element unitElement, Default defaultType) {
    if (unitElement != null) {
        String access = unitElement.getTextTrim();
        setAccess(access, defaultType);//  ww  w  . j a  v a 2  s  .  c  o m
    }
}

From source file:org.hibernate.ejb.Ejb3Configuration.java

License:Open Source License

private void addXMLEntities(List<String> xmlFiles, PersistenceUnitInfo info, List<String> entities) {
    //TODO handle inputstream related hbm files
    ClassLoader newTempClassLoader = info.getNewTempClassLoader();
    if (newTempClassLoader == null) {
        log.warn(//from  w w w  . j  a va 2s .com
                "Persistence provider caller does not implement the EJB3 spec correctly. PersistenceUnitInfo.getNewTempClassLoader() is null.");
        return;
    }
    XMLHelper xmlHelper = new XMLHelper();
    List errors = new ArrayList();
    SAXReader saxReader = xmlHelper.createSAXReader("XML InputStream", errors, cfg.getEntityResolver());
    try {
        saxReader.setFeature("http://apache.org/xml/features/validation/schema", true);
        //saxReader.setFeature( "http://apache.org/xml/features/validation/dynamic", true );
        //set the default schema locators
        saxReader.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation",
                "http://java.sun.com/xml/ns/persistence/orm orm_1_0.xsd");
    } catch (SAXException e) {
        saxReader.setValidation(false);
    }

    for (String xmlFile : xmlFiles) {

        InputStream resourceAsStream = newTempClassLoader.getResourceAsStream(xmlFile);
        if (resourceAsStream == null)
            continue;
        BufferedInputStream is = new BufferedInputStream(resourceAsStream);
        try {
            errors.clear();
            org.dom4j.Document doc = saxReader.read(is);
            if (errors.size() != 0) {
                throw new MappingException("invalid mapping: " + xmlFile, (Throwable) errors.get(0));
            }
            Element rootElement = doc.getRootElement();
            if (rootElement != null && "entity-mappings".equals(rootElement.getName())) {
                Element element = rootElement.element("package");
                String defaultPackage = element != null ? element.getTextTrim() : null;
                List<Element> elements = rootElement.elements("entity");
                for (Element subelement : elements) {
                    String classname = XMLContext.buildSafeClassName(subelement.attributeValue("class"),
                            defaultPackage);
                    if (!entities.contains(classname)) {
                        entities.add(classname);
                    }
                }
                elements = rootElement.elements("mapped-superclass");
                for (Element subelement : elements) {
                    String classname = XMLContext.buildSafeClassName(subelement.attributeValue("class"),
                            defaultPackage);
                    if (!entities.contains(classname)) {
                        entities.add(classname);
                    }
                }
                elements = rootElement.elements("embeddable");
                for (Element subelement : elements) {
                    String classname = XMLContext.buildSafeClassName(subelement.attributeValue("class"),
                            defaultPackage);
                    if (!entities.contains(classname)) {
                        entities.add(classname);
                    }
                }
            } else if (rootElement != null && "hibernate-mappings".equals(rootElement.getName())) {
                //FIXME include hbm xml entities to enhance them but entities is also used to collect annotated entities
            }
        } catch (DocumentException e) {
            throw new MappingException("Could not parse mapping document in input stream", e);
        } finally {
            try {
                is.close();
            } catch (IOException ioe) {
                log.warn("Could not close input stream", ioe);
            }
        }
    }
}

From source file:org.hibernate.metamodel.source.hbm.RootEntityBinder.java

License:Open Source License

private void basicTableBinding(Element entityElement, EntityBinding entityBinding) {
    final Schema schema = getHibernateXmlBinder().getMetadata().getDatabase().getSchema(getSchemaName());

    final String subSelect = HbmHelper.getSubselect(entityElement);
    if (subSelect != null) {
        final String logicalName = entityBinding.getEntity().getName();
        InLineView inLineView = schema.getInLineView(logicalName);
        if (inLineView == null) {
            inLineView = schema.createInLineView(logicalName, subSelect);
        }/*  w  ww.  j  av  a 2 s.  co m*/
        entityBinding.setBaseTable(inLineView);
    } else {
        final Identifier tableName = Identifier
                .toIdentifier(getClassTableName(entityElement, entityBinding, null));
        org.hibernate.metamodel.relational.Table table = schema.getTable(tableName);
        if (table == null) {
            table = schema.createTable(tableName);
        }
        entityBinding.setBaseTable(table);
        Element comment = entityElement.element("comment");
        if (comment != null) {
            table.addComment(comment.getTextTrim());
        }
        Attribute checkAttribute = entityElement.attribute("check");
        if (checkAttribute != null) {
            table.addCheckConstraint(checkAttribute.getValue());
        }
    }
}

From source file:org.hibernate.metamodel.source.hbm.state.relational.HbmColumnRelationalState.java

License:Open Source License

public String getComment() {
    Element comment = getElement().element("comment");
    return comment == null ? null : comment.getTextTrim();
}

From source file:org.hudsonci.plugins.team.cli.CopyTeamCommand.java

License:Open Source License

private InputStream fixConfigFile(XmlFile file, String oldTeam, String newTeam, String email) {
    InputStream in = null;/*from ww w.ja  v a2 s  .co  m*/
    String oldPrefix = oldTeam + TeamManager.TEAM_SEPARATOR;
    String newPrefix = newTeam + TeamManager.TEAM_SEPARATOR;
    try {
        in = new FileInputStream(file.getFile());
        SAXReader reader = new SAXReader();
        Document doc = reader.read(in);

        Element root = doc.getRootElement();
        // The root element name varies by project type, e.g.,
        // project, matrix-project, etc. Code assumes that
        // following elements are common to all project types.

        // Cascading
        Element cascadingParent = root.element("cascadingProjectName");
        if (cascadingParent != null) {
            fixTeamName(cascadingParent, oldPrefix, newPrefix);
        }
        Element cascadingChildren = root.element("cascadingChildrenNames");
        if (cascadingChildren != null) {
            for (Object elem : cascadingChildren.elements("string")) {
                fixTeamName((Element) elem, oldPrefix, newPrefix);
            }
        }
        // Properties (open-ended problem)
        Element properties = root.element("project-properties");
        if (properties == null) {
            throw new Failure("Project has no <project-properties>");
        }
        List<Element> removeEntries = new ArrayList<Element>();
        for (Object ent : properties.elements("entry")) {
            Element entry = (Element) ent;
            Element extProp = entry.element("external-property");
            Element origValue = extProp != null ? extProp.element("originalValue") : null;
            Element str = entry.element("string");
            if (str != null) {
                String propName = str.getTextTrim();
                if ("hudson-tasks-Mailer".equals(propName)) {
                    // email
                    if (extProp != null) {
                        if (email == null) {
                            // A recent fix removes entries that are not specified
                            removeEntries.add(entry);
                            /*
                            // Replace entire entry
                            entry.remove(extProp);
                            extProp = entry.addElement("external-property");
                            Element propOver = extProp.addElement("propertyOverridden");
                            propOver.setText("false");
                            Element modified = extProp.addElement("modified");
                            modified.setText("false");
                            */
                        } else if (origValue != null) {
                            fixEmailProperty(origValue, email);
                        }
                    }
                } else if ("hudson-tasks-BuildTrigger".equals(propName)) {
                    // trigger
                    if (origValue != null) {
                        fixTriggerProperty(origValue, oldPrefix, newPrefix);
                    }
                } else if ("builders".equals(propName)) {
                    // can be many of these
                    Element describableList = entry.element("describable-list-property");
                    origValue = describableList != null ? describableList.element("originalValue") : null;
                    // copyartifact
                    List artifacts = origValue != null
                            ? origValue.elements("hudson.plugins.copyartifact.CopyArtifact")
                            : null;
                    if (artifacts != null) {
                        for (Object obj : artifacts) {
                            Element copyArtifact = (Element) obj;
                            Element projectName = copyArtifact.element("projectName");
                            fixTeamName(projectName, oldPrefix, newPrefix);
                        }
                    }
                    // multijob
                    List multiJob = origValue != null
                            ? origValue.elements("com.tikal.jenkins.plugins.multijob.MultiJobBuilder")
                            : null;
                    for (Object obj : multiJob) {
                        Element builder = (Element) obj;
                        List jobs = builder.elements("phaseJobs");
                        if (jobs != null) {
                            for (Object j : jobs) {
                                Element job = (Element) j;
                                List configs = job
                                        .elements("com.tikal.jenkins.plugins.multijob.PhaseJobsConfig");
                                if (configs != null) {
                                    for (Object c : configs) {
                                        Element config = (Element) c;
                                        Element jobName = config.element("jobName");
                                        fixTeamName(jobName, oldPrefix, newPrefix);
                                    }
                                }
                            }
                        }
                    }

                } else if ("hudson-plugins-redmine-RedmineProjectProperty".equals(propName)) {
                    Element baseProp = entry.element("base-property");
                    Element projectName = baseProp != null ? baseProp.element("projectName") : null;
                    if (projectName != null) {
                        fixTeamName(projectName, oldPrefix, newPrefix);
                    }
                }
            }
        }
        for (Element entry : removeEntries) {
            properties.remove(entry);
        }

        StringWriter writer = new StringWriter();
        doc.write(writer);
        return new ByteArrayInputStream(writer.toString().getBytes("UTF-8"));
    } catch (FileNotFoundException ex) {
        throw new Failure("File not found");
    } catch (DocumentException ex) {
        throw new Failure("Unable to parse document");
    } catch (IOException ex) {
        throw new Failure("Document write failed");
    } finally {
        try {
            in.close();
        } catch (IOException ex) {
            ;
        }
    }
}