Example usage for org.dom4j DocumentFactory getInstance

List of usage examples for org.dom4j DocumentFactory getInstance

Introduction

In this page you can find the example usage for org.dom4j DocumentFactory getInstance.

Prototype

public static synchronized DocumentFactory getInstance() 

Source Link

Document

Access to singleton implementation of DocumentFactory which is used if no DocumentFactory is specified when building using the standard builders.

Usage

From source file:fr.gouv.culture.vitam.database.DbField.java

License:Open Source License

public Element getElement() {
    DocumentFactory factory = DocumentFactory.getInstance();
    Element field = factory.createElement(name);
    field.addAttribute(DbSchema.DESCRIPTION_ATTRIBUTE, description);
    field.addAttribute(DbSchema.TYPE_ATTRIBUTE, GenericDBType.getType(type, length));
    field.addAttribute(DbSchema.LENGTH_ATTRIBUTE, Integer.toString(length));
    field.addAttribute(DbSchema.NULLABLE_ATTRIBUTE, Boolean.toString(nullable));
    return field;
}

From source file:fr.gouv.culture.vitam.database.DbFieldValue.java

License:Open Source License

public Element getElement() {
    DocumentFactory factory = DocumentFactory.getInstance();
    Element value = factory.createElement(field.name);
    try {/*  ww  w . j a va 2s.c o m*/
        String sval = getValueAsString();
        if (sval != null) {
            value.setText(sval);
        }
    } catch (WaarpDatabaseSqlException e) {
        value.addAttribute(DbSchema.ERROR_ATTRIBUTE, "value error");
    }
    return value;
}

From source file:fr.gouv.culture.vitam.database.DbSchema.java

License:Open Source License

public Element getElement(boolean recursive, boolean data) {
    DocumentFactory factory = DocumentFactory.getInstance();
    Element schema = factory.createElement(SCHEMA_FIELD);
    schema.addAttribute(NAME_ATTRIBUTE, name);
    schema.addAttribute(DESCRIPTION_ATTRIBUTE, description);
    if (identifier != null) {
        Element eltidentifier = factory.createElement(IDENTIFIER_FIELD);
        eltidentifier.addAttribute(TYPE_ATTRIBUTE, identifier.type.name());
        eltidentifier.addAttribute(DATAFILE_ATTRIBUTE, identifier.refDataFile);
        switch (identifier.type) {
        case CSVTYPE:
            eltidentifier.addAttribute(SEPARATOR_ATTRIBUTE, identifier.separator);
            break;
        case MULTIPLETYPE: {
            Element poslistm = factory.createElement(MULTIPLEPOSITIONS_FIELD);
            for (int[] posmul : identifier.positionsMultiple) {
                Element poslist = factory.createElement(POSITIONS_FIELD);
                for (int ipos : posmul) {
                    Element pos = factory.createElement(POSITION_FIELD);
                    pos.setText(Integer.toString(ipos));
                    poslist.add(pos);/* ww  w  .java2 s .co m*/
                }
                poslistm.add(poslist);
            }
            eltidentifier.add(poslistm);
        }
            break;
        case UNIQUETYPE: {
            Element poslist = factory.createElement(POSITIONS_FIELD);
            for (int ipos : identifier.positions) {
                Element pos = factory.createElement(POSITION_FIELD);
                pos.setText(Integer.toString(ipos));
                poslist.add(pos);
            }
            eltidentifier.add(poslist);
        }
            break;
        }
        schema.add(eltidentifier);
    }
    if (constructionOrder != null) {
        Element order = factory.createElement(ORDER_FIELD);
        int rank = 0;
        for (String table : constructionOrder) {
            Element etable = factory.createElement(TABLE_FIELD);
            etable.addAttribute(RANK_ATTRIBUTE, Integer.toString(rank));
            etable.setText(table);
            order.add(etable);
            rank++;
        }
        schema.add(order);
    }
    if (recursive) {
        schema.addAttribute(NB_ATTRIBUTE, Integer.toString(tables.size()));
        for (DbTable table : tables) {
            if (data && table.getRows() != null && table.getRows().size() > 0) {
                Element etable = table.getElement(data);
                schema.add(etable);
            } else {
                Element etable = table.getElement(false);
                if (data) {
                    // fill data using file
                    try {
                        XmlWriter.loadDbTableDataFromFile(this, table, etable);
                    } catch (IOException e) {
                        logger.warn(StaticValues.LBL.error_error.get() + e);
                    } catch (WaarpDatabaseSqlException e) {
                        logger.warn(StaticValues.LBL.error_error.get() + e);
                    }
                }
                schema.add(etable);
            }
        }
    }
    return schema;
}

From source file:fr.gouv.culture.vitam.database.DbTable.java

License:Open Source License

public Element getElement(boolean data) {
    DocumentFactory factory = DocumentFactory.getInstance();
    Element table = factory.createElement(DbSchema.TABLE_FIELD);
    table.addAttribute(DbSchema.NAME_ATTRIBUTE, name);
    table.addAttribute(DbSchema.DESCRIPTION_ATTRIBUTE, description);
    table.addAttribute(DbSchema.RANK_ATTRIBUTE, Integer.toString(rank));
    table.addAttribute(DbSchema.TYPE_ATTRIBUTE, type.toString());
    if (datafile != null) {
        table.addAttribute(DbSchema.DATAFILE_ATTRIBUTE, datafile);
    }/*  w w  w.  j a  v a 2 s  .  com*/
    if (fields != null) {
        Element efields = factory.createElement(DbSchema.FIELDS_FIELD);
        for (DbField field : fields) {
            Element efield = field.getElement();
            efields.add(efield);
        }
        efields.addAttribute(DbSchema.NB_ATTRIBUTE, Integer.toString(fields.size()));
        table.add(efields);
    }
    if (primaryKeys != null) {
        Element efields = factory.createElement(DbSchema.PRIMARY_KEYS_FIELD);
        for (DbField field : primaryKeys) {
            Element pk = factory.createElement(DbSchema.PRIMARY_KEY_FIELD);
            pk.addAttribute(DbSchema.NAME_ATTRIBUTE, field.name);
            efields.add(pk);
        }
        table.add(efields);
    }
    if (indexes != null) {
        Element efields = factory.createElement(DbSchema.INDEXES_FIELD);
        for (String idx : indexes.keySet()) {
            Element index = factory.createElement(DbSchema.INDEX_FIELD);
            index.addAttribute(DbSchema.NAME_ATTRIBUTE, idx);
            for (DbField field : indexes.get(idx)) {
                Element efield = factory.createElement(DbSchema.FIELD_FIELD);
                efield.addAttribute(DbSchema.NAME_ATTRIBUTE, field.name);
                index.add(efield);
            }
            efields.add(index);
        }
        efields.addAttribute(DbSchema.NB_ATTRIBUTE, Integer.toString(indexes.size()));
        table.add(efields);
    }
    if (constraints != null) {
        Element efields = factory.createElement(DbSchema.CONSTRAINTS_FIELD);
        for (String name : constraints.keySet()) {
            Element index = factory.createElement(DbSchema.CONSTRAINT_FIELD);
            index.addAttribute(DbSchema.FIELD_FIELD, name);
            DbField field = constraints.get(name);
            index.addAttribute(DbSchema.TARGETTABLE_ATTRIBUTE, field.table.name);
            index.addAttribute(DbSchema.TARGETFIELD_ATTRIBUTE, field.name);
            efields.add(index);
        }
        efields.addAttribute(DbSchema.NB_ATTRIBUTE, Integer.toString(constraints.size()));
        table.add(efields);
    }
    if (data && rows != null && rows.size() > 0) {
        Element erows = factory.createElement(DbSchema.ROWS_FIELD);
        erows.addAttribute(DbSchema.NB_ATTRIBUTE, Integer.toString(rows.size()));
        for (DbTableRow row : rows) {
            Element erow = row.getElement();
            erows.add(erow);
        }
        table.add(erows);
    }
    return table;
}

From source file:fr.gouv.culture.vitam.database.DbTableRow.java

License:Open Source License

public Element getElement() {
    DocumentFactory factory = DocumentFactory.getInstance();
    Element row = factory.createElement(DbSchema.ROW_FIELD);
    row.addAttribute(DbSchema.RANK_ATTRIBUTE, Integer.toString(rank));
    row.addAttribute(DbSchema.NB_ATTRIBUTE, Integer.toString(values.size()));
    for (DbFieldValue value : values) {
        Element evalue = value.getElement();
        row.add(evalue);//w  w  w.j a  v a 2s .  c  om
    }
    return row;
}

From source file:fr.gouv.culture.vitam.database.model.DbCondition.java

License:Open Source License

public Element toElement() {
    DocumentFactory factory = DocumentFactory.getInstance();
    Element root = factory.createElement(DbSchema.CONDITION_FIELD);
    root.addAttribute(DbSchema.OPERATOR_ATTRIBUTE, operator.name());
    for (int i = 0; i < operands.length; i++) {
        Element op = factory.createElement(DbSchema.OPERAND_FIELD);
        op.addAttribute("rank", Integer.toString(i));
        if (operands[i] instanceof DbField) {
            op.addAttribute(DbSchema.TYPE_ATTRIBUTE, DbField.class.getSimpleName());
        } else {/*from   w  ww  .  ja va2  s.  co m*/
            op.addAttribute(DbSchema.TYPE_ATTRIBUTE, operands[i].getClass().getSimpleName());
        }
        op.setText(operands[i].toString());
        root.add(op);
    }
    return root;
}

From source file:fr.gouv.culture.vitam.database.model.DbSelect.java

License:Open Source License

public Element toElement() {
    cleanTableFrom();// w ww. j a  va  2s  . c  o  m
    DocumentFactory factory = DocumentFactory.getInstance();
    Element root = factory.createElement(DbSchema.DBSELECT_FIELD);
    root.addAttribute(DbSchema.OFFSET_ATTRIBUTE, Integer.toString(offset));
    root.addAttribute(DbSchema.LIMIT_ATTRIBUTE, Integer.toString(limit));

    if (selected.isEmpty()) {
        return root;
    }
    Element select = factory.createElement(DbSchema.SELECTED_FIELD);
    for (DbField field : selected) {
        Element efield = factory.createElement(DbSchema.FIELD_FIELD);
        efield.setText(field.toString());
        select.add(efield);
    }
    root.add(select);
    Element from = factory.createElement(DbSchema.FROM_FIELD);
    for (DbTable table : fromTables.values()) {
        Element efield = factory.createElement(DbSchema.TABLE_FIELD);
        efield.setText(table.toString());
        from.add(efield);
    }
    root.add(from);
    if (!conditions.isEmpty()) {
        Element econd = factory.createElement(DbSchema.CONDITIONS_FIELD);
        for (DbCondition condition : conditions) {
            Element efield = condition.toElement();
            econd.add(efield);
        }
        root.add(econd);
    }
    if (!orderByAsc.isEmpty()) {
        Element eorder = factory.createElement(DbSchema.ORDERASC_FIELD);
        for (DbField field : orderByAsc.values()) {
            Element efield = factory.createElement(DbSchema.FIELD_FIELD);
            efield.setText(field.toString());
            eorder.add(efield);
        }
        root.add(eorder);
    }
    if (!orderByDesc.isEmpty()) {
        Element eorder = factory.createElement(DbSchema.ORDERDESC_FIELD);
        for (DbField field : orderByDesc.values()) {
            Element efield = factory.createElement(DbSchema.FIELD_FIELD);
            efield.setText(field.toString());
            eorder.add(efield);
        }
        root.add(eorder);
    }
    return root;
}

From source file:fr.gouv.culture.vitam.extract.ExtractInfo.java

License:Open Source License

/**
 * Export Metadata for one file//from w  w w . j a v a2  s  .c o  m
 * @param input file to scan
 * @param filename filename to show
 * @param config
 * @param writer could be null
 * @param format could be null
 * @return the base element as result
 */
public static Element exportMetadata(File input, String filename, ConfigLoader config, XMLWriter writer,
        DroidFileFormat format) {
    char otherSeperator = File.separatorChar == '/' ? '\\' : '/';
    filename.replace(otherSeperator, File.separatorChar);
    BodyContentHandler handler = new BodyContentHandler(-1);
    Metadata metadata = new Metadata();
    Parser parser = new AutoDetectParser(new DefaultDetector());

    Element root = DocumentFactory.getInstance().createElement(EMAIL_FIELDS.keywords.name);
    root.addAttribute(EMAIL_FIELDS.filename.name, filename);

    InputStream inputStream = null;
    try {
        boolean scan = true;
        if (format != null) {
            // check according to format the interest of scanning the document
            String mimeType = format.getMimeType();
            if (mimeType.equalsIgnoreCase(DroidFileFormat.Unknown)) {
                scan = false;
            } else if (mimeType.startsWith("audio") || mimeType.startsWith("image")
                    || mimeType.startsWith("example") || mimeType.startsWith("model")
                    || mimeType.startsWith("video") || mimeType.startsWith("multipart")) {
                scan = false;
            } else if (format.getMimeType().startsWith("text")) {
                scan = true;
            } else {
                // should be an application
                if (!format.getPUID().startsWith(StaticValues.FORMAT_XFMT)) {
                    // not x-fmt
                    //Should be ok but will depend...
                    scan = true;
                } else {
                    scan = false;
                }
            }
        }
        System.out.print("Scan " + filename);
        String result = null;
        if (scan) {
            inputStream = new BufferedInputStream(new FileInputStream(input));
            try {
                parser.parse(inputStream, handler, metadata, new ParseContext());
            } catch (NoSuchMethodError e) {
                XmlDom.addDate(config.argument, config, root);
                System.out.println();
                return root;
            }
            result = handler.toString();
            handler = null;
            metadata = null;
            parser = null;
        }
        // Title of file will be considered as Main Keywords
        return exportMetadata(root, result, input.getName(), config, writer);
    } catch (Exception e) {
        XmlDom.addDate(config.argument, config, root);
        System.err.println(StaticValues.LBL.error_error.get() + " Extract: " + e.toString());
        return root;
    } finally {
        close(inputStream);
    }
}

From source file:fr.gouv.culture.vitam.extract.ExtractInfo.java

License:Open Source License

/**
 * Export Metadata for one String//from www . ja  va  2s.  co m
 * @param root
 * @param words
 * @param config
 * @param writer could be null
 * @return the base element as result
 */
public static Element exportMetadata(Element root, String words, String filename, ConfigLoader config,
        XMLWriter writer) {
    try {
        HashMap<String, Integer> references = getWordRank(words);
        Integer MaxRank = references.remove(MAXRANK);
        int maxRank = 0;
        if (MaxRank != null) {
            maxRank = MaxRank;
        }
        // Title of file will be considered as Main Keywords
        String[] wordLists = convertToSearchWords(filename).split(" ");
        for (String word : wordLists) {
            if (word.trim().length() > 0) {
                references.put(word, maxRank + 1);
            }
        }
        System.out.print(" Words found: " + references.size());
        HashMap<Integer, HashSet<String>> rankedValues = new HashMap<Integer, HashSet<String>>();
        TreeSet<Integer> sortRanks = new TreeSet<Integer>();
        for (String word : references.keySet()) {
            Integer rank = references.get(word);
            HashSet<String> wordset = rankedValues.get(rank);
            if (wordset == null) {
                wordset = new HashSet<String>();
            }
            wordset.add(word);
            rankedValues.put(rank, wordset);
            sortRanks.add(rank);
        }
        System.out.print(" Ranks found: " + rankedValues.size());
        Iterator<Integer> iterator = sortRanks.descendingIterator();
        int foundRank = 0;
        int limitword = 0;
        while (iterator.hasNext()) {
            Integer integer = iterator.next();
            // minimum 5 levels + rankLimit
            if (foundRank > 5 && config.rankLimit > integer) {
                break;
            }
            foundRank++;
            Element rankedElement = DocumentFactory.getInstance().createElement(EMAIL_FIELDS.keywordRank.name);
            rankedElement.addAttribute(EMAIL_FIELDS.keywordOccur.name, integer.toString());
            for (String word : rankedValues.get(integer)) {
                Element wordElement = DocumentFactory.getInstance()
                        .createElement(EMAIL_FIELDS.keywordWord.name);
                wordElement.addAttribute(EMAIL_FIELDS.keywordValue.name, word);
                rankedElement.add(wordElement);
                limitword++;
            }
            XmlDom.addElement(writer, config.argument, root, rankedElement);
            if (config.wordLimit > 0 && limitword > config.wordLimit) {
                break;
            }
        }
        System.out.println(" Word kept: " + limitword + " Ranks kept: " + foundRank);
        rankedValues.clear();
        sortRanks.clear();
        rankedValues = null;
        sortRanks = null;
    } catch (Exception e) {
        XmlDom.addDate(config.argument, config, root);
        System.err.println(StaticValues.LBL.error_error.get() + " Extract: " + e.toString());
        return root;
    }
    XmlDom.addDate(config.argument, config, root);
    return root;
}

From source file:fr.gouv.culture.vitam.extract.ExtractInfo.java

License:Open Source License

public static void main(String[] args) {
    if (args.length == 0) {
        System.err.println("need file as target");
        return;//from www. j  a  v a2s  .c om
    }
    StaticValues.initialize();
    Document document = DocumentFactory.getInstance().createDocument(StaticValues.CURRENT_OUTPUT_ENCODING);
    Element root = DocumentFactory.getInstance().createElement("extractkeywords");
    document.add(root);
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding(StaticValues.CURRENT_OUTPUT_ENCODING);
    XMLWriter writer = null;
    try {
        writer = new XMLWriter(System.out, format);
    } catch (UnsupportedEncodingException e1) {
        e1.printStackTrace();
        return;
    }
    for (String string : args) {
        File file = new File(string);
        recursive(file, string, root, StaticValues.config, writer);
    }
    if (StaticValues.config.argument.outputModel == VitamOutputModel.OneXML) {
        try {
            writer.write(document);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}