Search in sources :

Example 76 with Document

use of org.dom4j.Document in project tdi-studio-se by Talend.

the class HTMLDocGenerator method parseXml2HtmlPdf.

private List<URL> parseXml2HtmlPdf(String tempFolderPath, String jobName, String xslFilePath) throws Exception {
    // clear the cache, maybe need improve it latter.
    HTMLHandler.clearExternalNodeFileCache();
    String htmlFilePath = tempFolderPath + File.separatorChar + jobName + IHTMLDocConstants.HTML_FILE_SUFFIX;
    String xmlFilePath = tempFolderPath + File.separatorChar + jobName + IHTMLDocConstants.XML_FILE_SUFFIX;
    HTMLHandler.generateHTMLFile(tempFolderPath, xslFilePath, xmlFilePath, htmlFilePath, this.externalNodeHTMLMap);
    // for pdf
    File originalXmlFile = new File(xmlFilePath);
    if (originalXmlFile.exists()) {
        //$NON-NLS-1$
        String pdfXmlPath = tempFolderPath + File.separatorChar + "pdf_" + jobName + IHTMLDocConstants.XML_FILE_SUFFIX;
        File pdfXmlFile = new File(pdfXmlPath);
        if (pdfXmlFile.exists()) {
            pdfXmlFile.delete();
        }
        FilesUtils.copyFile(originalXmlFile, pdfXmlFile);
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(pdfXmlPath);
        //$NON-NLS-1$
        Attribute attri = (Attribute) document.selectNodes("/project/job/preview/@picture").get(0);
        //$NON-NLS-1$
        attri.setValue(IHTMLDocConstants.PICTUREFOLDERPATH + "pdf_" + jobName + IHTMLDocConstants.JOB_PREVIEW_PIC_SUFFIX);
        //$NON-NLS-1$
        List attributeList = document.selectNodes("/project/job/externalNodeComponents/component/@preview");
        for (int i = 0; i < attributeList.size(); i++) {
            Attribute at = (Attribute) attributeList.get(i);
            //$NON-NLS-1$
            String externalValue = at.getValue().substring(at.getValue().indexOf("/") + 1);
            //$NON-NLS-1$
            String value = IHTMLDocConstants.PICTUREFOLDERPATH + "pdf_" + externalValue;
            at.setValue(value);
        }
        XMLHandler.generateXMLFile(tempFolderPath, pdfXmlPath, document);
        HTMLHandler.clearExternalNodeFileCache();
        //$NON-NLS-1$
        String htmlPdfPath = tempFolderPath + File.separatorChar + "pdf_" + jobName + IHTMLDocConstants.HTML_FILE_SUFFIX;
        HTMLHandler.generateHTMLFile(tempFolderPath, xslFilePath, pdfXmlPath, htmlPdfPath, this.externalNodeHTMLMap);
    }
    return getParsedUrl(tempFolderPath);
}
Also used : Attribute(org.dom4j.Attribute) SAXReader(org.dom4j.io.SAXReader) CSSRuleList(org.w3c.dom.css.CSSRuleList) ArrayList(java.util.ArrayList) List(java.util.List) NodeList(org.w3c.dom.NodeList) EList(org.eclipse.emf.common.util.EList) Document(org.dom4j.Document) IFile(org.eclipse.core.resources.IFile) File(java.io.File) Point(org.eclipse.draw2d.geometry.Point)

Example 77 with Document

use of org.dom4j.Document in project tesb-studio-se by Talend.

the class AddSpringConfigurationPropertyMigrationTask method addIgnoreExchangeEventProperty.

private void addIgnoreExchangeEventProperty(CamelProcessItem item) throws PersistenceException, DocumentException {
    String springContent = item.getSpringContent();
    if (null != springContent && !springContent.isEmpty()) {
        Document document = DocumentHelper.parseText(springContent);
        QName qname = QName.get("bean", SPRING_BEANS_NAMESPACE);
        List<Element> beans = document.getRootElement().elements(qname);
        for (Element bean : beans) {
            if ("jmxEventNotifier".equals(bean.attributeValue("id")) && "org.apache.camel.management.JmxNotificationEventNotifier".equals(bean.attributeValue("class"))) {
                List<Element> properties = bean.elements(QName.get("property", SPRING_BEANS_NAMESPACE));
                boolean hasIgnore = false;
                for (Element property : properties) {
                    List<Attribute> propertyAttributes = property.attributes();
                    for (Attribute propertyAttribute : propertyAttributes) {
                        if (propertyAttribute.getValue().equals(IGNORE_EXCHANGE_EVENTS)) {
                            hasIgnore = true;
                            break;
                        }
                    }
                }
                if (!hasIgnore) {
                    DefaultElement ignoreExchangeElement = new DefaultElement("property", bean.getNamespace());
                    ignoreExchangeElement.add(DocumentHelper.createAttribute(ignoreExchangeElement, "name", IGNORE_EXCHANGE_EVENTS));
                    ignoreExchangeElement.add(DocumentHelper.createAttribute(ignoreExchangeElement, "value", "true"));
                    bean.add(ignoreExchangeElement);
                    item.setSpringContent(document.asXML());
                    saveItem(item);
                }
                break;
            }
        }
    }
}
Also used : Attribute(org.dom4j.Attribute) DefaultElement(org.dom4j.tree.DefaultElement) QName(org.dom4j.QName) DefaultElement(org.dom4j.tree.DefaultElement) Element(org.dom4j.Element) Document(org.dom4j.Document)

Example 78 with Document

use of org.dom4j.Document in project cpsolver by UniTime.

the class GetInfo method getInfo.

public static void getInfo(File folder, List<Info> infos, String prefix) {
    File infoFile = new File(folder, "info.xml");
    if (infoFile.exists()) {
        System.out.println("Reading " + infoFile + " ...");
        try {
            Document document = (new SAXReader()).read(infoFile);
            HashMap<String, String> info = getInfo(document.getRootElement());
            if (info != null && !info.isEmpty()) {
                infos.add(new Info(prefix, info));
                return;
            }
        } catch (Exception e) {
            System.err.println("Error reading file " + infoFile + ", message: " + e.getMessage());
        }
    }
    File solutionFile = new File(folder, "solution.xml");
    if (solutionFile.exists()) {
        /*
             * File newOutputFile = new File(folder, "new-output.csv"); if
             * (newOutputFile.exists()) {
             * System.out.println("Reading "+newOutputFile+" ..."); try {
             * HashMap info = getInfo(newOutputFile); if (info!=null &&
             * !info.isEmpty()) { infos.addElement(new Object[]{prefix,info});
             * return; } } catch (Exception e) {
             * System.err.println("Error reading file "
             * +infoFile+", message: "+e.getMessage()); } }
             */
        System.out.println("Reading " + solutionFile + " ...");
        try {
            HashMap<String, String> info = getInfoOfASolution(solutionFile);
            if (info != null && !info.isEmpty()) {
                infos.add(new Info(prefix, info));
                return;
            }
        } catch (Exception e) {
            System.err.println("Error reading file " + infoFile + ", message: " + e.getMessage());
        }
    }
    File outputFile = new File(folder, "output.csv");
    if (outputFile.exists()) {
        System.out.println("Reading " + outputFile + " ...");
        try {
            HashMap<String, String> info = getInfo(outputFile);
            if (info != null && !info.isEmpty()) {
                infos.add(new Info(prefix, info));
                return;
            }
        } catch (Exception e) {
            System.err.println("Error reading file " + infoFile + ", message: " + e.getMessage());
        }
    }
}
Also used : SAXReader(org.dom4j.io.SAXReader) Document(org.dom4j.Document) File(java.io.File) CSVFile(org.cpsolver.ifs.util.CSVFile)

Example 79 with Document

use of org.dom4j.Document in project cpsolver by UniTime.

the class Test method loadLastLikeCourseDemandsXml.

/**
     * Load last-like students from an XML file (the one that is used to load
     * last like course demands table in the timetabling application)
     * @param model problem model
     * @param xml an XML file
     */
public static void loadLastLikeCourseDemandsXml(StudentSectioningModel model, File xml) {
    try {
        Document document = (new SAXReader()).read(xml);
        Element root = document.getRootElement();
        HashMap<Course, List<Request>> requests = new HashMap<Course, List<Request>>();
        long reqId = 0;
        for (Iterator<?> i = root.elementIterator("student"); i.hasNext(); ) {
            Element studentEl = (Element) i.next();
            Student student = new Student(Long.parseLong(studentEl.attributeValue("externalId")));
            student.setDummy(true);
            int priority = 0;
            HashSet<Course> reqCourses = new HashSet<Course>();
            for (Iterator<?> j = studentEl.elementIterator("studentCourse"); j.hasNext(); ) {
                Element courseEl = (Element) j.next();
                String subjectArea = courseEl.attributeValue("subject");
                String courseNbr = courseEl.attributeValue("courseNumber");
                Course course = null;
                offerings: for (Offering offering : model.getOfferings()) {
                    for (Course c : offering.getCourses()) {
                        if (c.getSubjectArea().equals(subjectArea) && c.getCourseNumber().equals(courseNbr)) {
                            course = c;
                            break offerings;
                        }
                    }
                }
                if (course == null && courseNbr.charAt(courseNbr.length() - 1) >= 'A' && courseNbr.charAt(courseNbr.length() - 1) <= 'Z') {
                    String courseNbrNoSfx = courseNbr.substring(0, courseNbr.length() - 1);
                    offerings: for (Offering offering : model.getOfferings()) {
                        for (Course c : offering.getCourses()) {
                            if (c.getSubjectArea().equals(subjectArea) && c.getCourseNumber().equals(courseNbrNoSfx)) {
                                course = c;
                                break offerings;
                            }
                        }
                    }
                }
                if (course == null) {
                    sLog.warn("Course " + subjectArea + " " + courseNbr + " not found.");
                } else {
                    if (!reqCourses.add(course)) {
                        sLog.warn("Course " + subjectArea + " " + courseNbr + " already requested.");
                    } else {
                        List<Course> courses = new ArrayList<Course>(1);
                        courses.add(course);
                        CourseRequest request = new CourseRequest(reqId++, priority++, false, student, courses, false, null);
                        List<Request> requestsThisCourse = requests.get(course);
                        if (requestsThisCourse == null) {
                            requestsThisCourse = new ArrayList<Request>();
                            requests.put(course, requestsThisCourse);
                        }
                        requestsThisCourse.add(request);
                    }
                }
            }
            if (!student.getRequests().isEmpty())
                model.addStudent(student);
        }
        for (Map.Entry<Course, List<Request>> entry : requests.entrySet()) {
            Course course = entry.getKey();
            List<Request> requestsThisCourse = entry.getValue();
            double weight = getLastLikeStudentWeight(course, 0, requestsThisCourse.size());
            for (Request request : requestsThisCourse) {
                request.setWeight(weight);
            }
        }
    } catch (Exception e) {
        sLog.error(e.getMessage(), e);
    }
}
Also used : HashMap(java.util.HashMap) SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) Document(org.dom4j.Document) List(java.util.List) ArrayList(java.util.ArrayList) Course(org.cpsolver.studentsct.model.Course) HashSet(java.util.HashSet) Request(org.cpsolver.studentsct.model.Request) CourseRequest(org.cpsolver.studentsct.model.CourseRequest) Student(org.cpsolver.studentsct.model.Student) Offering(org.cpsolver.studentsct.model.Offering) IOException(java.io.IOException) CourseRequest(org.cpsolver.studentsct.model.CourseRequest) Map(java.util.Map) HashMap(java.util.HashMap)

Example 80 with Document

use of org.dom4j.Document in project cpsolver by UniTime.

the class Test method loadStudentInfoXml.

/** Load student infos from a given XML file. 
     * @param model problem model
     * @param xml an XML file
     **/
public static void loadStudentInfoXml(StudentSectioningModel model, File xml) {
    try {
        sLog.info("Loading student infos from " + xml);
        Document document = (new SAXReader()).read(xml);
        Element root = document.getRootElement();
        HashMap<Long, Student> studentTable = new HashMap<Long, Student>();
        for (Student student : model.getStudents()) {
            studentTable.put(new Long(student.getId()), student);
        }
        for (Iterator<?> i = root.elementIterator("student"); i.hasNext(); ) {
            Element studentEl = (Element) i.next();
            Student student = studentTable.get(Long.valueOf(studentEl.attributeValue("externalId")));
            if (student == null) {
                sLog.debug(" -- student " + studentEl.attributeValue("externalId") + " not found");
                continue;
            }
            sLog.debug(" -- loading info for student " + student);
            student.getAcademicAreaClasiffications().clear();
            if (studentEl.element("studentAcadAreaClass") != null)
                for (Iterator<?> j = studentEl.element("studentAcadAreaClass").elementIterator("acadAreaClass"); j.hasNext(); ) {
                    Element studentAcadAreaClassElement = (Element) j.next();
                    student.getAcademicAreaClasiffications().add(new AcademicAreaCode(studentAcadAreaClassElement.attributeValue("academicArea"), studentAcadAreaClassElement.attributeValue("academicClass")));
                }
            sLog.debug("   -- acad areas classifs " + student.getAcademicAreaClasiffications());
            student.getMajors().clear();
            if (studentEl.element("studentMajors") != null)
                for (Iterator<?> j = studentEl.element("studentMajors").elementIterator("major"); j.hasNext(); ) {
                    Element studentMajorElement = (Element) j.next();
                    student.getMajors().add(new AcademicAreaCode(studentMajorElement.attributeValue("academicArea"), studentMajorElement.attributeValue("code")));
                }
            sLog.debug("   -- majors " + student.getMajors());
            student.getMinors().clear();
            if (studentEl.element("studentMinors") != null)
                for (Iterator<?> j = studentEl.element("studentMinors").elementIterator("minor"); j.hasNext(); ) {
                    Element studentMinorElement = (Element) j.next();
                    student.getMinors().add(new AcademicAreaCode(studentMinorElement.attributeValue("academicArea", ""), studentMinorElement.attributeValue("code", "")));
                }
            sLog.debug("   -- minors " + student.getMinors());
        }
    } catch (Exception e) {
        sLog.error(e.getMessage(), e);
    }
}
Also used : HashMap(java.util.HashMap) SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) Iterator(java.util.Iterator) AcademicAreaCode(org.cpsolver.studentsct.model.AcademicAreaCode) Document(org.dom4j.Document) Student(org.cpsolver.studentsct.model.Student) IOException(java.io.IOException)

Aggregations

Document (org.dom4j.Document)288 Element (org.dom4j.Element)192 SAXReader (org.dom4j.io.SAXReader)143 Test (org.junit.Test)102 StringReader (java.io.StringReader)88 File (java.io.File)57 ArrayList (java.util.ArrayList)47 List (java.util.List)40 IOException (java.io.IOException)27 DocumentException (org.dom4j.DocumentException)25 XMLWriter (org.dom4j.io.XMLWriter)22 HashMap (java.util.HashMap)19 Map (java.util.Map)18 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)17 LinkedList (java.util.LinkedList)16 Attribute (org.dom4j.Attribute)15 Node (org.dom4j.Node)14 FileOutputStream (java.io.FileOutputStream)11 DavException (com.zimbra.cs.dav.DavException)10 OutputFormat (org.dom4j.io.OutputFormat)9