/* * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template */ package com.megatim.reporting.adhoc.process; import com.megatim.reporting.adhoc.pojo.Constantes; import com.megatim.reporting.adhoc.pojo.layouts.Report; import com.megatim.reporting.adhoc.pojo.xml.ReportTag; import com.megatim.reporting.adhoc.pojo.components.Component; import com.megatim.reporting.adhoc.pojo.components.Image; import com.megatim.reporting.adhoc.pojo.components.Label; import com.megatim.reporting.adhoc.pojo.components.Line; import com.megatim.reporting.adhoc.pojo.components.TextComponent; import com.megatim.reporting.adhoc.pojo.components.TextField; import com.megatim.reporting.adhoc.pojo.components.ValueElement; import com.megatim.reporting.adhoc.pojo.enums.ReportOrientation; import com.megatim.reporting.adhoc.pojo.enums.ValueElementType; import com.megatim.reporting.adhoc.pojo.layouts.ColumnData; import com.megatim.reporting.adhoc.pojo.layouts.ColumnHeader; import com.megatim.reporting.adhoc.pojo.layouts.Layout; import com.megatim.reporting.adhoc.pojo.layouts.MainReport; import com.megatim.reporting.adhoc.pojo.layouts.PageFooter; import com.megatim.reporting.adhoc.pojo.layouts.PageHeader; import com.megatim.reporting.adhoc.pojo.layouts.SubReport; import com.megatim.reporting.adhoc.pojo.layouts.Title; import java.io.File; import java.util.ArrayList; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; /** * * @author ASUS */ public class ParseXMLTemplate { private ReportTag reportTag; /** * Méthode qui parse le fichier de xml de description de l'état * * @param file * @return * @throws Exception */ public Report parse(File file) throws Exception { reportTag = new ReportTag(); Report report = new Report(); Document doc = intialize(file); //Liste des noeud NodeList reportList = doc.getElementsByTagName(Constantes.TAG_REPORT); if (reportList.getLength() > 0) { Node reportNode = reportList.item(0); //Attributs de NamedNodeMap reportAttributes = reportNode.getAttributes(); setReportProperties(reportAttributes, report); //Liste des balises enfant du 1er Noeud NodeList reportChildren = reportNode.getChildNodes(); getFieldsAndParameters(reportChildren); report.setParameters(reportTag.getParameters()); report.setFields(reportTag.getFields()); if (reportTag.getTitle() != null) { Title title = getTitle(reportTag.getTitle()); report.setTitle(title); } if (reportTag.getPageHeader() != null) { PageHeader pHeader = getPageHeader(reportTag.getPageHeader()); report.setPageHeader(pHeader); } if (reportTag.getColumnHeader() != null) { ColumnHeader cHeader = getColumnHeader(reportTag.getColumnHeader()); report.setColumnHeader(cHeader); } if (reportTag.getColumnData() != null) { ColumnData cData = getColumnData(reportTag.getColumnData()); report.setColumnData(cData); } if (reportTag.getPageFooter() != null) { PageFooter pageFooter = getPageFooter(reportTag.getPageFooter()); report.setPageFooter(pageFooter); } NodeList subReportList = doc.getElementsByTagName(Constantes.TAG_SUB_REPORT); if (subReportList.getLength() > 0) { List subReports = getAllSubReports(subReportList); report.getSubReports().addAll(subReports); // for (SubReport sub : subReports) { // ValueElement valueElt = new ValueElement(sub.getDataSource(), java.util.List.class, ValueElementType.FIELD); // report.getFields().putIfAbsent(sub.getDataSource(), valueElt); // } } } return report; } /** * Méthode qui construit le composant 'Titre' du rapport à partir d'un noeud * * @param node : Noeud representant la balise 'title' extrait du fichier xml * @return * @throws ClassNotFoundException */ private Title getTitle(Node node) throws ClassNotFoundException { NamedNodeMap attributes = node.getAttributes(); Title title = new Title(); //Parcours des attributs de la balise for (int i = 0; i < attributes.getLength(); i++) { String nodeName = attributes.item(i).getNodeName(); String nodeValue = attributes.item(i).getNodeValue(); if (nodeName.equalsIgnoreCase(Constantes.ATTRIBUTE_STYLE)) { title = (Title) getLayoutStyle(nodeValue, title); } } List<Component> components = getComponents(node.getChildNodes()); title.setComponents(components); return title; } /** * Méthode qui construit le composant 'PageHeader' du rapport à partir d'un * noeud * * @param node : Noeud representant la balise 'pageHeader' extrait du * fichier xml * @return * @throws ClassNotFoundException */ private PageHeader getPageHeader(Node node) throws Exception { NamedNodeMap attributes = node.getAttributes(); PageHeader pageHeader = new PageHeader(); //Prcours des attributs de la balise <Title> for (int i = 0; i < attributes.getLength(); i++) { String nodeName = attributes.item(i).getNodeName(); String nodeValue = attributes.item(i).getNodeValue(); if (nodeName.equalsIgnoreCase(Constantes.ATTRIBUTE_STYLE)) { pageHeader = (PageHeader) getLayoutStyle(nodeValue, pageHeader); } } List<Component> components = getComponents(node.getChildNodes()); pageHeader.setComponents(components); return pageHeader; } /** * Méthode qui crée le composant 'ColumnHeader' du rapport à partir d'un * noeud * * @param node : Noeud representant la balise 'columnHeader' extrait du * fichier xml * @return * @throws ClassNotFoundException */ private ColumnHeader getColumnHeader(Node node) throws Exception { NamedNodeMap attributes = node.getAttributes(); ColumnHeader columnHeader = new ColumnHeader(); //Parcours des attributs de la balise <Title> for (int i = 0; i < attributes.getLength(); i++) { String nodeName = attributes.item(i).getNodeName(); String nodeValue = attributes.item(i).getNodeValue(); if (nodeName.equalsIgnoreCase(Constantes.ATTRIBUTE_STYLE)) { columnHeader = (ColumnHeader) getLayoutStyle(nodeValue, columnHeader); } else if (nodeName.equalsIgnoreCase(Constantes.ATTRIBUTE_COLUMN_STYLE)) { columnHeader.setColumnStyle(nodeValue); } } List<Component> components = getComponents(node.getChildNodes()); columnHeader.setComponents(components); return columnHeader; } /** * Méthode qui crée le composant 'ColumnData'(Detail) du rapport à partir * d'un noeud * * @param node : Noeud representant la balise 'columnData' extrait du * fichier xml * @return * @throws ClassNotFoundException */ private ColumnData getColumnData(Node node) throws ClassNotFoundException { ColumnData columnData = new ColumnData(); NamedNodeMap attributes = node.getAttributes(); //Parcours des attributs de la balise <ColumnData> for (int i = 0; i < attributes.getLength(); i++) { String nodeName = attributes.item(i).getNodeName(); String nodeValue = attributes.item(i).getNodeValue(); if (nodeName.equalsIgnoreCase(Constantes.ATTRIBUTE_STYLE)) { columnData = (ColumnData) getLayoutStyle(nodeValue, columnData); } else if (nodeName.equalsIgnoreCase(Constantes.ATTRIBUTE_COLUMN_STYLE)) { columnData.setColumnStyle(nodeValue); } } List<Component> components = getComponents(node.getChildNodes()); columnData.setComponents(components); return columnData; } /** * Méthode qui crée le composant 'PageFooter' du rapport à partir d'un noeud * * @param node : Noeud representant la balise 'pageFooter' extrait du * fichier xml * @return * @throws ClassNotFoundException */ private PageFooter getPageFooter(Node node) throws ClassNotFoundException { PageFooter pageFooter = new PageFooter(); NamedNodeMap attributes = node.getAttributes(); //Parcours des attributs de la balise <ColumnData> for (int i = 0; i < attributes.getLength(); i++) { String nodeName = attributes.item(i).getNodeName(); String nodeValue = attributes.item(i).getNodeValue(); if (nodeName.equalsIgnoreCase(Constantes.ATTRIBUTE_STYLE)) { pageFooter = (PageFooter) getLayoutStyle(nodeValue, pageFooter); } } List<Component> components = getComponents(node.getChildNodes()); pageFooter.setComponents(components); return pageFooter; } /** * * @param attributeValue * @param layout * @return */ private Layout getLayoutStyle(String attributeValue, Layout layout) { String[] styles = attributeValue.trim().split(";"); for (String style : styles) { String[] tab = style.split(":"); if (tab.length == 2) { if (tab[0].equals(Constantes.ATTRIBUTE_HEIGHT)) { layout.setHeight(Integer.parseInt(tab[1])); } else if (tab[0].equals(Constantes.ATTRIBUTE_SPLIT_TYPE)) { layout.setSplitType(tab[1]); } } } return layout; } /** * Méthode qui recupère tous les enfants d'une balise qui représente une * section(Title, PageHeader, ColumnHeader,Detail, PageFooter) d'un état et * construis * * @param childNodes : Balises enfant * @return * @throws ClassNotFoundException */ private List<Component> getComponents(NodeList childNodes) throws ClassNotFoundException { List<Component> components = new ArrayList<>(); for (int i = 0; i < childNodes.getLength(); i++) { Node child = childNodes.item(i); if (child.getNodeName().equalsIgnoreCase(Constantes.TAG_IMAGE)) { Image image = getImageTag(child); components.add(image); } else if (child.getNodeName().equalsIgnoreCase(Constantes.TAG_TEXT_FIELD)) { TextField textField = getTextFieldTag(child); components.add(textField); } else if (child.getNodeName().equalsIgnoreCase(Constantes.TAG_LABEL)) { Label label = getLabelTag(child); components.add(label); } else if (child.getNodeName().equalsIgnoreCase(Constantes.TAG_LINE)) { Line line = getLineTag(child); components.add(line); } else if (child.getNodeName().equalsIgnoreCase(Constantes.TAG_LINE_NUMBER)) { TextField textField = getTextFieldTag(child); textField.setLineNumber(true); components.add(textField); } else if (child.getNodeName().equalsIgnoreCase(Constantes.TAG_PAGE_NUMBER)) { TextField textField = getTextFieldTag(child); textField.setPageNumber(true); components.add(textField); } } return components; } // private void get /** * Méthode qui extrait les attributs communs aux composants de type * Text(Label,TextField) * * @param attributeValue : contenu de l'attribut 'style' de la balise parent * @param textComponent : Objet auquel sera affecté les attributs extraits */ public static void getTextComponentAttributes(String attributeValue, TextComponent textComponent) { String[] styles = attributeValue.trim().split(";"); for (String style : styles) { String[] tab = style.trim().split(":"); if (tab.length == 2 && tab[1] != null && !tab[1].isEmpty()) { if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_FONT_NAME)) { textComponent.setFontName(tab[1]); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_BOLD)) { textComponent.setBold(Boolean.parseBoolean(tab[1])); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_UNDERLINE)) { textComponent.setUnderLine(Boolean.parseBoolean(tab[1])); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_TEXT_ADJUST)) { textComponent.setTextAdjust(tab[1]); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_IS_BLANK_WHEN_NULL)) { textComponent.setBlankWhenNull(Boolean.parseBoolean(tab[1])); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_IS_PRINT_IN_FIRST_WHOLE_BAND)) { textComponent.setPrintInFirtWholeBand(Boolean.parseBoolean(tab[1])); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_IS_PRINT_WHEN_DETAIL_OVER_FLOWS)) { textComponent.setPrintWhenDetailOverflows(Boolean.parseBoolean(tab[1])); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_SIZE)) { textComponent.setSize(Integer.parseInt(tab[1])); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_FORE_COLOR)) { textComponent.setForeColor(tab[1]); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_RIGHT_INDENT)) { textComponent.getParagraph().setRightIndent(Integer.parseInt(tab[1])); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_LEFT_INDENT)) { textComponent.getParagraph().setLeftIndent(Integer.parseInt(tab[1])); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_SPACING_AFTER)) { textComponent.getParagraph().setSpacingAfter(Integer.parseInt(tab[1])); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_SPACING_BEFORE)) { textComponent.getParagraph().setSpacingBefore(Integer.parseInt(tab[1])); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_FIRST_LINE_INDENT)) { textComponent.getParagraph().setFirstLineIndent(Integer.parseInt(tab[1])); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_LINE_SPACING_SIZE)) { textComponent.getParagraph().setLineSpacingSize(Double.parseDouble(tab[1])); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_LINE_SPACING)) { textComponent.getParagraph().setLineSpacing(tab[1]); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_REMOVE_LINE_WHEN_BLANK)) { textComponent.setRemoveLineWhenBlank(Boolean.parseBoolean(tab[1])); } } } } /** * Méthode qui extrait les attributs communs à tous les composants * * @param attributeValue : contenu de l'attribut 'style' de la balise parent * @param component : Objet auquel sera affecté les attributs extraits */ public static void getComponentAttributes(String attributeValue, Component component) { String[] styles = attributeValue.trim().split(";"); for (String style : styles) { String[] tab = style.trim().split(":"); if (tab.length == 2 && tab[1] != null && !tab[1].isEmpty()) { if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_HEIGHT)) { component.setHeight(Integer.parseInt(tab[1])); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_X)) { component.setX(Integer.parseInt(tab[1])); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_Y)) { component.setY(Integer.parseInt(tab[1])); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_WIDTH)) { component.setWidth(Integer.parseInt(tab[1])); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_H_ALIGN)) { component.sethAlign(tab[1]); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_V_ALIGN)) { component.setvAlign(tab[1]); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_LEFT_BORDER)) { component.getBorder().setLeftBorder(Float.parseFloat(tab[1])); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_TOP_BORDER)) { component.getBorder().setTopBorder(Float.parseFloat(tab[1])); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_BOTTOM_BORDER)) { component.getBorder().setBottomBorder(Float.parseFloat(tab[1])); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_RIGHT_BORDER)) { component.getBorder().setRightBorder(Float.parseFloat(tab[1])); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_RIGHT_PADDING)) { component.getBorder().setRightPadding(Integer.parseInt(tab[1])); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_LEFT_PADDING)) { component.getBorder().setLeftPadding(Integer.parseInt(tab[1])); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_TOP_PADDING)) { component.getBorder().setTopPadding(Integer.parseInt(tab[1])); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_BOTTOM_PADDING)) { component.getBorder().setBottomPadding(Integer.parseInt(tab[1])); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_LINE_STYLE)) { component.getBorder().setLineStyle(tab[1]); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_LINE_COLOR)) { component.getBorder().setLineColor(tab[1]); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_OPERAND_TYPE)) { component.getDisplayCondition().setOperandType(tab[1]); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_LEFT_OPERAND)) { component.getDisplayCondition().setLeftOperand(tab[1]); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_RIGHT_OPERAND)) { component.getDisplayCondition().setRightOperand(tab[1]); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_OPERATOR)) { component.getDisplayCondition().setOperator(tab[1]); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_STRETCH_TYPE)) { component.setStretchType(tab[1]); } } } } /** * Méthode qui parse une balise <image> et construis l'objet Image * correspondant * * @param node * @return */ private Image getImageTag(Node node) throws ClassNotFoundException { Image image = new Image(); NamedNodeMap attributes = node.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { String attributeName = attributes.item(i).getNodeName(); String attributeValue = attributes.item(i).getNodeValue(); if (attributeName.equalsIgnoreCase(Constantes.ATTRIBUTE_STYLE) || attributeName.equalsIgnoreCase(Constantes.ATTRIBUTE_DISPLAY_CONDITION)) { //Récupérer les attributs communs à tous les 'Component' getComponentAttributes(attributeValue, image); } else if (attributeName.equalsIgnoreCase(Constantes.ATTRIBUTE_SCALE_IMAGE)) { image.setScaleImage(attributeValue); } } NodeList imageChildren = node.getChildNodes(); ValueElement valueElt = getValueElement(imageChildren); image.setValueElement(valueElt); return image; } /** * Méthode qui parse une balise <textField> et construis l'objet textField * correspondant * * @param node * @return */ private TextField getTextFieldTag(Node node) throws ClassNotFoundException { NamedNodeMap attributes = node.getAttributes(); TextField textField = new TextField(); //Parcours des attributs de la balise <textField> for (int i = 0; i < attributes.getLength(); i++) { String attributeName = attributes.item(i).getNodeName(); String attributeValue = attributes.item(i).getNodeValue(); if (attributeName.equalsIgnoreCase(Constantes.ATTRIBUTE_STYLE)) { //Recuperer la valeur des attributs communs à tous les 'Component' getComponentAttributes(attributeValue, textField); //Recuperer la valeur des attributs communs à tous 'TextComponent' getTextComponentAttributes(attributeValue, textField); //Recupérer les attibuts spécifiques à 'TextField' } else if (attributeName.equalsIgnoreCase(Constantes.ATTRIBUTE_PATTERN) && attributeValue != null && !attributeValue.isEmpty()) { textField.setPattern(attributeValue.trim()); } else if (attributeName.equalsIgnoreCase(Constantes.ATTRIBUTE_DISPLAY_CONDITION)) { getComponentAttributes(attributeValue, textField); } } NodeList textFieldChildren = node.getChildNodes(); ValueElement valueElt = getValueElement(textFieldChildren); textField.setValueElement(valueElt); return textField; } /** * * @param node * @throws Exception */ private List<SubReport> getAllSubReports(NodeList subReportList) throws Exception { List<SubReport> subReports = new ArrayList<>(); for (int i = 0; i < subReportList.getLength(); i++) { Node subReportTag = subReportList.item(i); subReports.add(getSubReportTag(subReportTag)); } return subReports; } private SubReport getSubReportTag(Node subReportTag) throws Exception { NamedNodeMap attributes = subReportTag.getAttributes(); SubReport subReport = new SubReport(); //Parcours des attributs de la balise <subReport> for (int j = 0; j < attributes.getLength(); j++) { String attributeName = attributes.item(j).getNodeName(); String attributeValue = attributes.item(j).getNodeValue(); if (attributeName.equalsIgnoreCase(Constantes.ATTRIBUTE_STYLE)) { setReportStyle(attributeValue, subReport); } else if (attributeName.equalsIgnoreCase(Constantes.ATTRIBUTE_NAME)) { subReport.setName(attributeValue); } else if (attributeName.equalsIgnoreCase(Constantes.ATTRIBUTE_DATASOURCE)) { subReport.setDataSource(attributeValue); } else if (attributeName.equalsIgnoreCase(Constantes.ATTRIBUTE_BAND_STYLE)) { String[] styles = attributeValue.split(";"); for (String style : styles) { String[] tab = style.trim().split(":"); if (tab.length == 2 && tab[1] != null && !tab[1].isEmpty()) { if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_IS_PRINT_IN_FIRST_WHOLE_BAND)) { subReport.setPrintInFirtWholeBand(Boolean.parseBoolean(tab[1])); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_IS_PRINT_WHEN_DETAIL_OVER_FLOWS)) { subReport.setPrintWhenDetailOverflows(Boolean.parseBoolean(tab[1])); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_HEIGHT)) { subReport.setHeight(Integer.parseInt(tab[1])); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_X)) { subReport.setX(Integer.parseInt(tab[1])); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_Y)) { subReport.setY(Integer.parseInt(tab[1])); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_WIDTH)) { subReport.setWidth(Integer.parseInt(tab[1])); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_SPLIT_TYPE)) { subReport.setSplitType(tab[1]); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_STRETCH_TYPE)) { subReport.setStretchType(tab[1]); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_RUN_TO_BOTTOM)) { subReport.setRunToBottom(Boolean.parseBoolean(tab[1])); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_USING_CACHE)) { subReport.setUsingCache(Boolean.parseBoolean(tab[1])); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_OVERFLOW_TYPE)) { subReport.setOverflowType(tab[1]); } } } } else if (attributeName.equalsIgnoreCase(Constantes.ATTRIBUTE_PAGE_TITLE_STYLE)) { subReport.setPageTitleStyle(attributeValue); } } reportTag = new ReportTag(); getFieldsAndParameters(subReportTag.getChildNodes()); subReport.setParameters(reportTag.getParameters()); subReport.setFields(reportTag.getFields()); if (reportTag.getTitle() != null) { Title title = getTitle(reportTag.getTitle()); subReport.setTitle(title); } if (reportTag.getPageHeader() != null) { PageHeader pHeader = getPageHeader(reportTag.getPageHeader()); subReport.setPageHeader(pHeader); } if (reportTag.getColumnHeader() != null) { ColumnHeader cHeader = getColumnHeader(reportTag.getColumnHeader()); subReport.setColumnHeader(cHeader); } if (reportTag.getColumnData() != null) { ColumnData cData = getColumnData(reportTag.getColumnData()); subReport.setColumnData(cData); } if (reportTag.getPageFooter() != null) { PageFooter pageFooter = getPageFooter(reportTag.getPageFooter()); subReport.setPageFooter(pageFooter); } return subReport; } private ValueElement getValueElement(NodeList children) throws ClassNotFoundException { ValueElement valueElt = null; for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (child.getNodeName().equalsIgnoreCase(Constantes.TAG_PARAMETER) || child.getNodeName().equalsIgnoreCase(Constantes.TAG_FIELD)) { NamedNodeMap textFieldChildAttributes = child.getAttributes(); String classe = "java.lang.String"; String name = null; for (int j = 0; j < textFieldChildAttributes.getLength(); j++) { Node attNode = textFieldChildAttributes.item(j); String attributeValue = attNode.getNodeValue(); String attributeName = attNode.getNodeName(); if (attributeName.equalsIgnoreCase(Constantes.ATTRIBUTE_CLASSE)) { classe = attributeValue; } else if (attributeName.equalsIgnoreCase(Constantes.ATTRIBUTE_NAME)) { name = attributeValue; } } Class<?> clazz = Class.forName(classe); if (child.getNodeName().equalsIgnoreCase(Constantes.TAG_PARAMETER)) { valueElt = new ValueElement(name, clazz, ValueElementType.PARAMETER); } else if (child.getNodeName().equalsIgnoreCase(Constantes.TAG_FIELD)) { valueElt = new ValueElement(name, clazz, ValueElementType.FIELD); } } else if (child.getNodeName().equalsIgnoreCase(Constantes.TAG_TEXT)) { NamedNodeMap textFieldChildAttributes = child.getAttributes(); valueElt = new ValueElement(ValueElementType.TEXT); for (int j = 0; j < textFieldChildAttributes.getLength(); j++) { Node attNode = textFieldChildAttributes.item(j); String attributeValue = attNode.getNodeValue(); String attributeName = attNode.getNodeName(); if (attributeName.equalsIgnoreCase(Constantes.ATTRIBUTE_VALUE)) { valueElt.setValue(attributeValue); } } } else if (child.getNodeName().equalsIgnoreCase(Constantes.TAG_GENERATE)) { NamedNodeMap textFieldChildAttributes = child.getAttributes(); valueElt = new ValueElement(ValueElementType.GENERATE); for (int j = 0; j < textFieldChildAttributes.getLength(); j++) { Node attNode = textFieldChildAttributes.item(j); String attributeValue = attNode.getNodeValue(); String attributeName = attNode.getNodeName(); if (attributeName.equalsIgnoreCase(Constantes.ATTRIBUTE_PATTERN)) { valueElt.setPattern(attributeValue); } else if (attributeName.equalsIgnoreCase(Constantes.ATTRIBUTE_CLASSE)) { valueElt.setClasse(Class.forName(attributeValue)); } } } } return valueElt; } /** * Méthode qui parse une balise <label> et construis l'objet label * correspondant * * @param node * @return */ private Label getLabelTag(Node node) { NamedNodeMap attributes = node.getAttributes(); Label label = new Label(); //Parcours des attributs de la balise <label> for (int i = 0; i < attributes.getLength(); i++) { String attributeName = attributes.item(i).getNodeName(); String attributeValue = attributes.item(i).getNodeValue(); if (attributeName.equalsIgnoreCase(Constantes.ATTRIBUTE_STYLE)) { //Récuperer la valeur des attributs communs à tous les 'Component' getComponentAttributes(attributeValue, label); //Récuperer la valeur des attributs communs à tous les 'TextComponent' getTextComponentAttributes(attributeValue, label); } else if (attributeName.equalsIgnoreCase(Constantes.ATTRIBUTE_VALUE)) { //Recupérer les attibuts spécifiques à 'Label' label.setValue(attributeValue); } else if (attributeName.equalsIgnoreCase(Constantes.ATTRIBUTE_DISPLAY_CONDITION)) { getComponentAttributes(attributeValue, label); } } return label; } /** * Méthode qui parse une balise <line> et construis l'objet line * correspondant * * @param node * @return */ private Line getLineTag(Node node) { NamedNodeMap attributes = node.getAttributes(); Line line = new Line(); //Parcours des attributs de la balise <line> for (int i = 0; i < attributes.getLength(); i++) { String attributeName = attributes.item(i).getNodeName(); String attributeValue = attributes.item(i).getNodeValue(); if (attributeName.equals(Constantes.ATTRIBUTE_STYLE)) { //Recupérer la valeur des attributs commun à 'Component' getComponentAttributes(attributeValue, line); //Recupérer les attibuts spécifiques à 'Line' String[] styles = attributeValue.trim().split(";"); for (String style : styles) { String[] tab = style.trim().split(":"); if (tab.length == 2 && tab[1] != null && !tab[1].isEmpty()) { if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_LINE_STYLE)) { line.setLineStyle(tab[1]); } else if (tab[0].equalsIgnoreCase(Constantes.ATTRIBUTE_LINE_WIDTH)) { line.setLineWidth(Float.parseFloat(tab[1])); } } } } else if (attributeName.equals(Constantes.ATTRIBUTE_DISPLAY_CONDITION)) { getComponentAttributes(attributeValue, line); } } return line; } /** * Méthode qui recupère tous les champs et paramètres trouvés dans le * fichier xml de template * * @param reportChildren * @throws ClassNotFoundException */ private void getFieldsAndParameters(NodeList reportChildren) throws ClassNotFoundException { for (int i = 0; i < reportChildren.getLength(); i++) { Node child = reportChildren.item(i); if (child.getNodeName().equalsIgnoreCase(Constantes.TAG_TITLE)) { reportTag.setTitle(child); } else if (child.getNodeName().equalsIgnoreCase(Constantes.TAG_PAGE_HEADER)) { reportTag.setPageHeader(child); } else if (child.getNodeName().equalsIgnoreCase(Constantes.TAG_COLUMN_HEADER)) { reportTag.setColumnHeader(child); } else if (child.getNodeName().equalsIgnoreCase(Constantes.TAG_COLUMN_DATA)) { reportTag.setColumnData(child); } else if (child.getNodeName().equalsIgnoreCase(Constantes.TAG_PAGE_FOOTER)) { reportTag.setPageFooter(child); } if (child.getNodeName().equals(Constantes.TAG_PARAMETER) || child.getNodeName().equals(Constantes.TAG_FIELD)) { NamedNodeMap valueElementeterAttributes = child.getAttributes(); String classe = "java.lang.String"; String name = null; String defaultValue = null; for (int j = 0; j < valueElementeterAttributes.getLength(); j++) { String nodeName = valueElementeterAttributes.item(j).getNodeName(); String nodeValue = valueElementeterAttributes.item(j).getNodeValue(); if (nodeName.equalsIgnoreCase(Constantes.ATTRIBUTE_CLASSE)) { classe = nodeValue; } else if (nodeName.equalsIgnoreCase(Constantes.ATTRIBUTE_NAME)) { name = nodeValue; } else if (nodeName.equalsIgnoreCase(Constantes.ATTRIBUTE_DEFAULT_VALUE)) { defaultValue = nodeValue; } } ValueElement valueElement = null; final Class<?> clazz = Class.forName(classe); if (child.getNodeName().equals(Constantes.TAG_PARAMETER)) { valueElement = new ValueElement(name, clazz, ValueElementType.PARAMETER); if (defaultValue != null) { valueElement.setValue(defaultValue); } reportTag.getParameters().merge(valueElement.getName(), valueElement, (v1, v2) -> v1.getValue() != null ? v2 : v1); } else if (child.getNodeName().equals(Constantes.TAG_FIELD)) { valueElement = new ValueElement(name, clazz, ValueElementType.FIELD); reportTag.getFields().putIfAbsent(valueElement.getName(), valueElement); } } else { if (child.getChildNodes().getLength() > 0 && !child.getNodeName().equalsIgnoreCase(Constantes.TAG_SUB_REPORT)) { getFieldsAndParameters(child.getChildNodes()); } } } } /** * Méthode qui recherche les attributs de la balise <report> * * @param reportAttributes * @return * @throws Exception */ private Report setReportProperties(NamedNodeMap reportAttributes, Report report) throws Exception { for (int i = 0; i < reportAttributes.getLength(); i++) { String nodeName = reportAttributes.item(i).getNodeName(); String nodeValue = reportAttributes.item(i).getNodeValue(); if (nodeName.equalsIgnoreCase(Constantes.ATTRIBUTE_NAME)) { report.setName(nodeValue); } else if (nodeName.equalsIgnoreCase(Constantes.ATTRIBUTE_STYLE)) { setReportStyle(nodeValue, report); } } return report; } private MainReport setReportStyle(String styles, MainReport report) { if (styles != null && !styles.isEmpty()) { String[] tab = styles.split(";"); for (String str : tab) { String[] style = str.split(":"); if (style.length == 2 && style[1] != null && !style[1].isEmpty()) { if (style[0].equalsIgnoreCase(Constantes.ATTRIBUTE_BOTTOM_MARGIN)) { report.setBottomMargin(Integer.parseInt(style[1])); } else if (style[0].equalsIgnoreCase(Constantes.ATTRIBUTE_TOP_MARGIN)) { report.setTopMargin(Integer.parseInt(style[1])); } else if (style[0].equalsIgnoreCase(Constantes.ATTRIBUTE_LEFT_MARGIN)) { report.setLeftMargin(Integer.parseInt(style[1])); } else if (style[0].equalsIgnoreCase(Constantes.ATTRIBUTE_RIGHT_MARGIN)) { report.setRightMargin(Integer.parseInt(style[1])); } else if (style[0].equalsIgnoreCase(Constantes.ATTRIBUTE_ORIENTATION)) { report.setOrientation(ReportOrientation.reportOrientation(style[1])); } else if (style[0].equalsIgnoreCase(Constantes.ATTRIBUTE_PAGE_HEIGHT)) { report.setPageHeight(Integer.parseInt(style[1])); } else if (style[0].equalsIgnoreCase(Constantes.ATTRIBUTE_PAGE_WIDTH)) { report.setPageWidth(Integer.parseInt(style[1])); } else if (style[0].equalsIgnoreCase(Constantes.ATTRIBUTE_COLUMN_WIDTH)) { report.setColumnWidth(Integer.parseInt(style[1])); } } } } return report; } /** * Méthode utilitaire pour extraire les données dans un fichier xml * * @valueElement array * @return * @throws Exception */ private Document intialize(File file) throws Exception { /** * Défini un factory qui aide à obtenir un parseur qui produit un arbre * d'objets DOM à partir d'un docuent XML. */ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); /** * création d'un objet du builder pour parser le fichier XML. */ DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(file); doc.getDocumentElement().normalize(); return doc; } }