List of usage examples for org.jdom2 Element getContent
@Override public Content getContent(final int index)
From source file:jodtemplate.pptx.postprocessor.StylePostprocessor.java
License:Apache License
private Element getArPrElement(final Element ar) { final List<Element> arPrElements = ar.getContent(Filters.element(PPTXDocument.RPR_ELEMENT, getNamespace())); Element arPr = null;//w w w . java 2 s .c o m if (CollectionUtils.isNotEmpty(arPrElements)) { arPr = arPrElements.get(0).clone(); arPr.removeAttribute("b", getNamespace()); arPr.removeAttribute("i", getNamespace()); arPr.removeAttribute("u", getNamespace()); } return arPr; }
From source file:jodtemplate.pptx.postprocessor.StylePostprocessor.java
License:Apache License
private Element getApPrElement(final Element ap) { final List<Element> apPrElements = ap.getContent(Filters.element(PPTXDocument.PPR_ELEMENT, getNamespace())); if (CollectionUtils.isNotEmpty(apPrElements)) { return apPrElements.get(0).clone(); }/* w ww.j a va 2s . c o m*/ return null; }
From source file:nforce.graphs.BipartiteGraph.java
/** * This method reads the graph in xml format. * @param filePath /*from w ww.j a v a 2 s . c om*/ */ public final void readXmlGraph(String filePath) throws IOException { /* Read the input file. */ SAXBuilder builder = new SAXBuilder(); Document graphInput = null; try { graphInput = builder.build(new File(filePath)); } catch (JDOMException e) { System.out.println( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) Document init error: " + filePath); return; } /* Get the root element. */ Element docRoot, entity; try { docRoot = graphInput.getRootElement(); } catch (IllegalStateException e) { System.out.println( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) The input document does not have a root element."); e.printStackTrace(); return; } /* 1. Anaylze the element "entity", get the name of the nodes.*/ /* Get the child element of "entity", throw an IllegalStateException.*/ entity = docRoot.getChild("entity"); if (entity == null) /* If no such "entity" child element is found, then throw the exception.*/ throw new IllegalStateException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) The input document does not have an \"entity\" child element."); /* Get how many levels are there in this graph.*/ String levelStr = entity.getAttributeValue("levels"); /* Get how many sets are there in the graph. */ /* levelStr is required. */ if (levelStr == null) { /* If no "levels" attribute is defined, then an exception is thrown. */ throw new IllegalArgumentException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) Attribute \"levels\" must be given."); } /* Check if there is an external file for the node names.*/ String hasExternEntityFile = entity.getAttributeValue("externalFile"); /* Get the content in entity. */ String entityContent = entity.getContent(0).getValue().trim(); /* Check entityContent. It cannot be null.*/ if (entityContent == null) throw new IllegalStateException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) The content of the element entity is null."); /* If there is an external file for the nodes' names, then the content of the element should be the path of the external file. Otherwise, it should be the nodes' names themselves. */ if (hasExternEntityFile == null || hasExternEntityFile.equalsIgnoreCase("false")) { XmlInputParser parser = new XmlInputParser(); parser.parseEntityString(entityContent, this); } /* If the node names are stored in an external file.*/ else if (hasExternEntityFile.equalsIgnoreCase("true")) { XmlInputParser parser = new XmlInputParser(); parser.parseEntityFile(entityContent, this); } else throw new IllegalStateException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) Illegal attribute of \"externalFile\": " + hasExternEntityFile); edgeWeights = new float[set0Size][set1Size]; /* Init all matrices. */ /* We have setSizes.length intraEdgeWeightMatrix. */ for (int i = 0; i < edgeWeights.length; i++) for (int j = 0; j < edgeWeights[0].length; j++) edgeWeights[i][j] = Float.NaN; /* Read the edge weights from the xml input file. */ ArrayList<Element> matrixElementList = new ArrayList<>(docRoot.getChildren("matrix")); /* First check the number of elements in matrixElementList, if not equal to 2*setSizes.length-1. */ if (matrixElementList.size() != 1) throw new IllegalArgumentException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) The number of matrices is wrong: " + matrixElementList.size()); /* 2. Assign the edge weights. */ for (Element matrix : matrixElementList) { /* First check where is this matrix. */ String matrixLevel = matrix.getAttributeValue("matrixLevel"); if (matrixLevel == null) /* The matrixLevel attribute is required. */ throw new IllegalArgumentException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) The matrixLevel attribute is required."); String[] matrixLevelSplits = matrixLevel.split("\\s+"); /* If string matrixLevel cannot be splitted into two splits, then it must be wrong. */ if (matrixLevelSplits.length != 2) throw new IllegalArgumentException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) The attribute matrixLevel error: " + matrixLevel); /* Get the two levels. */ int matrixLevel1 = -1, matrixLevel2 = -1; try { matrixLevel1 = Integer.parseInt(String.copyValueOf(matrixLevelSplits[0].toCharArray())); matrixLevel2 = Integer.parseInt(String.copyValueOf(matrixLevelSplits[1].toCharArray())); } catch (NumberFormatException e) { throw new IllegalArgumentException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) The attribute matrixLevel error: " + matrixLevel); } /* Check if the matrix is a intra- or inter edge weight matrix. */ if (matrixLevel1 == matrixLevel2) { /* Intra matrix. */ } else { /* For inter-matrix. */ String matrixContent = matrix.getContent(0).getValue().trim(); /* Check if the matrix is stored in an external file.*/ String hasExternMatrixFile = matrix.getAttributeValue("externalFile"); if (hasExternMatrixFile == null || hasExternMatrixFile.equalsIgnoreCase("false")) { XmlInputParser parser = new XmlInputParser(); parser.parseInterMatrixString(matrixContent, this); } else if (hasExternMatrixFile.equalsIgnoreCase("true")) { XmlInputParser parser = new XmlInputParser(); parser.parseInterMatrixFile(matrixContent, this); } else throw new IllegalStateException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml))Illegal attribute of \"externalFile\": " + hasExternMatrixFile); } } clusters = new ArrayList<>(); actions = new ArrayList<>(); }
From source file:nforce.graphs.GeneralGraph.java
public final void readXmlGraph(String filePath) throws IOException { /* Read the input file. */ SAXBuilder builder = new SAXBuilder(); Document graphInput = null;/*ww w. j a va2 s .c om*/ try { graphInput = builder.build(new File(filePath)); } catch (JDOMException e) { System.out.println( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) Document init error: " + filePath); return; } /* Get the root element. */ Element docRoot, entity; try { docRoot = graphInput.getRootElement(); } catch (IllegalStateException e) { System.out.println( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) The input document does not have a root element."); e.printStackTrace(); return; } /* 1. Anaylze the element "entity", get the name of the nodes.*/ /* Get the child element of "entity", throw an IllegalStateException.*/ entity = docRoot.getChild("entity"); if (entity == null) /* If no such "entity" child element is found, then throw the exception.*/ throw new IllegalStateException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) The input document does not have an \"entity\" child element."); String hasExternEntityFile = entity.getAttributeValue("externalFile"); /* Get the content in entity. */ String entityContent = entity.getContent(0).getValue().trim(); /* Check entityContent. It cannot be null.*/ if (entityContent == null) throw new IllegalStateException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) The content of the element entity is null."); /* If there is an external file for the nodes' names, then the content of the element should be the path of the external file. Otherwise, it should be the nodes' names themselves. */ if (hasExternEntityFile == null || hasExternEntityFile.equalsIgnoreCase("false")) { XmlInputParser parser = new XmlInputParser(); parser.parseEntityString(entityContent, this); } /* If the node names are stored in an external file.*/ else if (hasExternEntityFile.equalsIgnoreCase("true")) { XmlInputParser parser = new XmlInputParser(); parser.parseEntityFile(entityContent, this); } else throw new IllegalStateException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) Illegal attribute of \"externalFile\": " + hasExternEntityFile); //Init the matrix edgeWeights = new float[vertices.size()][vertices.size()]; for (int i = 0; i < edgeWeights.length; i++) for (int j = 0; j < edgeWeights[0].length; j++) edgeWeights[i][j] = Float.NaN; /* Read the edge weights from the xml input file. */ ArrayList<Element> matrixElementList = new ArrayList<>(docRoot.getChildren("matrix")); /* First check the number of elements in matrixElementList, if not equal to 2*setSizes.length-1. */ // There are 2*setSizes.length matrix, half intra-matrices, half inter-matrices. if (matrixElementList.size() != 1) throw new IllegalArgumentException( "(biforce.graphs.MatrixGraph.readGraphInXml) The number of matrices is wrong: " + matrixElementList.size()); Element matrix = matrixElementList.get(0); String matrixContent = matrix.getContent(0).getValue().trim(); String hasExternMatrixFile = matrix.getAttributeValue("externalFile"); if (hasExternMatrixFile == null || hasExternMatrixFile.equalsIgnoreCase("false")) { XmlInputParser parser = new XmlInputParser(); parser.parseMatrixString(matrixContent, this); } /* If the node names are stored in an external file.*/ else if (hasExternMatrixFile.equalsIgnoreCase("true")) { XmlInputParser parser = new XmlInputParser(); parser.parseMatrixFile(matrixContent, this); } else throw new IllegalStateException( "(biforce.graphs.MatrixGraph.readGraphInXml) Illegal attribute of \"externalFile\": " + hasExternMatrixFile); }
From source file:nforce.graphs.HierGraph.java
/** * This method reads the graph from the xml input. * @param filePath // w w w . ja v a 2 s .c o m */ public final void readXmlGraph(String filePath) throws IOException { /* Read the input file. */ SAXBuilder builder = new SAXBuilder(); Document graphInput = null; try { graphInput = builder.build(new File(filePath)); } catch (JDOMException e) { System.out.println( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) Document init error: " + filePath); return; } /* Get the root element. */ Element docRoot, entity; try { docRoot = graphInput.getRootElement(); } catch (IllegalStateException e) { System.out.println( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) The input document does not have a root element."); e.printStackTrace(); return; } /* 1. Anaylze the element "entity", get the name of the nodes.*/ /* Get the child element of "entity", throw an IllegalStateException.*/ entity = docRoot.getChild("entity"); if (entity == null) /* If no such "entity" child element is found, then throw the exception.*/ throw new IllegalStateException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) The input document does not have an \"entity\" child element."); /* Get how many levels are there in this graph.*/ String levelStr = entity.getAttributeValue("levels"); /* Get how many sets are there in the graph. */ /* levelStr is required. */ if (levelStr == null) { /* If no "levels" attribute is defined, then an exception is thrown. */ throw new IllegalArgumentException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) Attribute \"levels\" must be given."); } try { setSizes = new int[Integer.parseInt(levelStr)];/* Init setSizes. */ } catch (NumberFormatException e) { throw new NumberFormatException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) Attribute \"levels\" number format error: " + levelStr); } /* Check if there is an external file for the node names.*/ String hasExternEntityFile = entity.getAttributeValue("externalFile"); /* Get the content in entity. */ String entityContent = entity.getContent(0).getValue().trim(); /* Check entityContent. It cannot be null.*/ if (entityContent == null) throw new IllegalStateException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) The content of the element entity is null."); /* If there is an external file for the nodes' names, then the content of the element should be the path of the external file. Otherwise, it should be the nodes' names themselves. */ if (hasExternEntityFile == null || hasExternEntityFile.equalsIgnoreCase("false")) { XmlInputParser parser = new XmlInputParser(); parser.parseEntityString(entityContent, this); } /* If the node names are stored in an external file.*/ else if (hasExternEntityFile.equalsIgnoreCase("true")) { XmlInputParser parser = new XmlInputParser(); parser.parseEntityFile(entityContent, this); } else throw new IllegalStateException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) Illegal attribute of \"externalFile\": " + hasExternEntityFile); /* Init all matrices. */ /* Init the inter matrices. */ edgeWeights = new ArrayList<>(); for (int i = 0; i < setSizes.length - 1; i++) { float[][] interMatrix = new float[setSizes[i]][setSizes[i + 1]]; /* Give matrix the init values. */ for (int j = 0; j < setSizes[i]; j++) for (int k = 0; k < setSizes[i + 1]; k++) interMatrix[j][k] = Float.NaN; /* Push this interMatrix into interEdgeWeights. */ edgeWeights.add(interMatrix); } /* Read the edge weights from the xml input file. */ ArrayList<Element> matrixElementList = new ArrayList<>(docRoot.getChildren("matrix")); /* First check the number of elements in matrixElementList, if not equal to setSizes.length-1. */ if (matrixElementList.size() != setSizes.length - 1) throw new IllegalArgumentException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) The number of matrices is wrong: " + matrixElementList.size()); /* 2. Assign the edge weights. */ for (Element matrix : matrixElementList) { /* First check where is this matrix. */ String matrixLevel = matrix.getAttributeValue("matrixLevel"); if (matrixLevel == null) /* The matrixLevel attribute is required. */ throw new IllegalArgumentException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) The matrixLevel attribute is required."); String[] matrixLevelSplits = matrixLevel.split("\\s+"); /* If string matrixLevel cannot be splitted into two splits, then it must be wrong. */ if (matrixLevelSplits.length != 2) throw new IllegalArgumentException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) The attribute matrixLevel error: " + matrixLevel); /* Get the two levels. */ int matrixLevel1 = -1, matrixLevel2 = -1; try { matrixLevel1 = Integer.parseInt(String.copyValueOf(matrixLevelSplits[0].toCharArray())); matrixLevel2 = Integer.parseInt(String.copyValueOf(matrixLevelSplits[1].toCharArray())); } catch (NumberFormatException e) { throw new IllegalArgumentException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) The attribute matrixLevel error: " + matrixLevel); } /* Check if the matrix is a intra- or inter edge weight matrix. */ if (matrixLevel1 == matrixLevel2) /* Intra matrix. */ throw new IllegalArgumentException( "(biforce.graphs.MatrixHierNpartiteGraph.readGraphInXml) There is no intra-matrix in a MatrixHierNpartiteGraph."); else { /* For inter-matrix. */ String matrixContent = matrix.getContent(0).getValue().trim(); /* Check if the matrix is stored in an external file.*/ String hasExternMatrixFile = matrix.getAttributeValue("externalFile"); if (hasExternMatrixFile == null || hasExternMatrixFile.equalsIgnoreCase("false")) { XmlInputParser parser = new XmlInputParser(); parser.parseInterMatrixString(matrixContent, matrixLevel1, matrixLevel2, this); } else if (hasExternMatrixFile.equalsIgnoreCase("true")) { XmlInputParser parser = new XmlInputParser(); parser.parseInterMatrixFile(matrixContent, matrixLevel1, matrixLevel2, this); } else throw new IllegalStateException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml))Illegal attribute of \"externalFile\": " + hasExternMatrixFile); } } clusters = new ArrayList<>(); }
From source file:nforce.graphs.HierGraphWIE.java
/** * This method reads graph from the "entity-matrix" style input. * @throws IOException //from w ww. ja v a 2 s . c o m * @param filePath The input file. * untested. */ public final void readXmlGraph(String filePath) throws IOException { /* Read the input file. */ SAXBuilder builder = new SAXBuilder(); Document graphInput = null; try { graphInput = builder.build(new File(filePath)); } catch (JDOMException e) { System.out.println( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) Document init error: " + filePath); return; } /* Get the root element. */ Element docRoot, entity; try { docRoot = graphInput.getRootElement(); } catch (IllegalStateException e) { System.out.println( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) The input document does not have a root element."); e.printStackTrace(); return; } /* 1. Anaylze the element "entity", get the name of the nodes.*/ /* Get the child element of "entity", throw an IllegalStateException.*/ entity = docRoot.getChild("entity"); if (entity == null) /* If no such "entity" child element is found, then throw the exception.*/ throw new IllegalStateException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) The input document does not have an \"entity\" child element."); /* Get how many levels are there in this graph.*/ String levelStr = entity.getAttributeValue("levels"); /* Get how many sets are there in the graph. */ /* levelStr is required. */ if (levelStr == null) { /* If no "levels" attribute is defined, then an exception is thrown. */ throw new IllegalArgumentException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) Attribute \"levels\" must be given."); } try { setSizes = new int[Integer.parseInt(levelStr)];/* Init setSizes. */ } catch (NumberFormatException e) { throw new NumberFormatException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) Attribute \"levels\" number format error: " + levelStr); } /* Check if there is an external file for the node names.*/ String hasExternEntityFile = entity.getAttributeValue("externalFile"); /* Get the content in entity. */ String entityContent = entity.getContent(0).getValue().trim(); /* Check entityContent. It cannot be null.*/ if (entityContent == null) throw new IllegalStateException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) The content of the element entity is null."); /* If there is an external file for the nodes' names, then the content of the element should be the path of the external file. Otherwise, it should be the nodes' names themselves. */ if (hasExternEntityFile == null || hasExternEntityFile.equalsIgnoreCase("false")) { XmlInputParser parser = new XmlInputParser(); parser.parseEntityString(entityContent, this); } /* If the node names are stored in an external file.*/ else if (hasExternEntityFile.equalsIgnoreCase("true")) { XmlInputParser parser = new XmlInputParser(); parser.parseEntityFile(entityContent, this); } else throw new IllegalStateException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) Illegal attribute of \"externalFile\": " + hasExternEntityFile); /* Init all matrices. */ /* We have setSizes.length intraEdgeWeightMatrix. */ intraEdgeWeights = new ArrayList<>(); for (int i = 0; i < setSizes.length; i++) { float[][] intraMatrix = new float[setSizes[i]][setSizes[i]]; /* Give matrix the init values. */ for (int j = 0; j < setSizes[i]; j++) for (int k = 0; k < setSizes[i]; k++) intraMatrix[j][k] = Float.NaN; /* Push this intraMatrix to intraEdgeWeights. */ intraEdgeWeights.add(intraMatrix); } /* Init the inter matrices. */ interEdgeWeights = new ArrayList<>(); for (int i = 0; i < setSizes.length - 1; i++) { float[][] interMatrix = new float[setSizes[i]][setSizes[i + 1]]; /* Give matrix the init values. */ for (int j = 0; j < setSizes[i]; j++) for (int k = 0; k < setSizes[i + 1]; k++) interMatrix[j][k] = Float.NaN; /* Push this interMatrix into interEdgeWeights. */ interEdgeWeights.add(interMatrix); } /* Read the edge weights from the xml input file. */ ArrayList<Element> matrixElementList = new ArrayList<>(docRoot.getChildren("matrix")); /* First check the number of elements in matrixElementList, if not equal to 2*setSizes.length-1. */ if (matrixElementList.size() != 2 * setSizes.length - 1) throw new IllegalArgumentException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) The number of matrices is wrong: " + matrixElementList.size()); /* 2. Assign the edge weights. */ for (Element matrix : matrixElementList) { /* First check where is this matrix. */ String matrixLevel = matrix.getAttributeValue("matrixLevel"); if (matrixLevel == null) /* The matrixLevel attribute is required. */ throw new IllegalArgumentException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) The matrixLevel attribute is required."); String[] matrixLevelSplits = matrixLevel.split("\\s+"); /* If string matrixLevel cannot be splitted into two splits, then it must be wrong. */ if (matrixLevelSplits.length != 2) throw new IllegalArgumentException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) The attribute matrixLevel error: " + matrixLevel); /* Get the two levels. */ int matrixLevel1 = -1, matrixLevel2 = -1; try { matrixLevel1 = Integer.parseInt(String.copyValueOf(matrixLevelSplits[0].toCharArray())); matrixLevel2 = Integer.parseInt(String.copyValueOf(matrixLevelSplits[1].toCharArray())); } catch (NumberFormatException e) { throw new IllegalArgumentException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) The attribute matrixLevel error: " + matrixLevel); } /* Check if the matrix is a intra- or inter edge weight matrix. */ if (matrixLevel1 == matrixLevel2) { /* Intra matrix. */ /* Read the content. */ String matrixContent = matrix.getContent(0).getValue().trim(); /* Check if the matrix is stored in an external file.*/ String hasExternMatrixFile = matrix.getAttributeValue("externalFile"); if (hasExternMatrixFile == null || hasExternMatrixFile.equalsIgnoreCase("false")) { XmlInputParser parser = new XmlInputParser(); parser.parseIntraMatrixString(matrixContent, matrixLevel1, this); } /* If the node names are stored in an external file.*/ else if (hasExternMatrixFile.equalsIgnoreCase("true")) { XmlInputParser parser = new XmlInputParser(); parser.parseIntraMatrixFile(matrixContent, matrixLevel1, this); } else throw new IllegalStateException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) Illegal attribute of \"externalFile\": " + hasExternMatrixFile); } else { /* For inter-matrix. */ String matrixContent = matrix.getContent(0).getValue().trim(); /* Check if the matrix is stored in an external file.*/ String hasExternMatrixFile = matrix.getAttributeValue("externalFile"); if (hasExternMatrixFile == null || hasExternMatrixFile.equalsIgnoreCase("false")) { XmlInputParser parser = new XmlInputParser(); parser.parseInterMatrixString(matrixContent, matrixLevel1, matrixLevel2, this); } else if (hasExternMatrixFile.equalsIgnoreCase("true")) { XmlInputParser parser = new XmlInputParser(); parser.parseInterMatrixFile(matrixContent, matrixLevel1, matrixLevel2, this); } else throw new IllegalStateException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml))Illegal attribute of \"externalFile\": " + hasExternMatrixFile); } } clusters = new ArrayList<>(); }
From source file:nforce.graphs.NpartiteGraph.java
/** * This method reads graph from the "entity-matrix" style input. * @throws IOException //from ww w . j ava2s . co m * @param filePath The input file. * untested. */ public final void readXmlGraph(String filePath) throws IOException { /* Read the input file. */ SAXBuilder builder = new SAXBuilder(); Document graphInput = null; try { graphInput = builder.build(new File(filePath)); } catch (JDOMException e) { System.out.println( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) Document init error: " + filePath); return; } /* Get the root element. */ Element docRoot, entity; try { docRoot = graphInput.getRootElement(); } catch (IllegalStateException e) { System.out.println( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) The input document does not have a root element."); e.printStackTrace(); return; } /* 1. Anaylze the element "entity", get the name of the nodes.*/ /* Get the child element of "entity", throw an IllegalStateException.*/ entity = docRoot.getChild("entity"); if (entity == null) /* If no such "entity" child element is found, then throw the exception.*/ throw new IllegalStateException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) The input document does not have an \"entity\" child element."); /* Get how many levels are there in this graph.*/ String levelStr = entity.getAttributeValue("levels"); /* Get how many sets are there in the graph. */ /* levelStr is required. */ if (levelStr == null) { /* If no "levels" attribute is defined, then an exception is thrown. */ throw new IllegalArgumentException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) Attribute \"levels\" must be given."); } try { setSizes = new int[Integer.parseInt(levelStr)];/* Init setSizes. */ } catch (NumberFormatException e) { throw new NumberFormatException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) Attribute \"levels\" number format error: " + levelStr); } /* Check if there is an external file for the node names.*/ String hasExternEntityFile = entity.getAttributeValue("externalFile"); /* Get the content in entity. */ String entityContent = entity.getContent(0).getValue().trim(); /* Check entityContent. It cannot be null.*/ if (entityContent == null) throw new IllegalStateException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) The content of the element entity is null."); /* If there is an external file for the nodes' names, then the content of the element should be the path of the external file. Otherwise, it should be the nodes' names themselves. */ if (hasExternEntityFile == null || hasExternEntityFile.equalsIgnoreCase("false")) { XmlInputParser parser = new XmlInputParser(); parser.parseEntityString(entityContent, this); } /* If the node names are stored in an external file.*/ else if (hasExternEntityFile.equalsIgnoreCase("true")) { XmlInputParser parser = new XmlInputParser(); parser.parseEntityFile(entityContent, this); } else throw new IllegalStateException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) Illegal attribute of \"externalFile\": " + hasExternEntityFile); /* Init all matrices. */ /* We have setSizes.length intraEdgeWeightMatrix. */ intraEdgeWeights = new ArrayList<>(); for (int i = 0; i < setSizes.length; i++) { float[][] intraMatrix = new float[setSizes[i]][setSizes[i]]; /* Give matrix the init values. */ for (int j = 0; j < setSizes[i]; j++) for (int k = 0; k < setSizes[i]; k++) intraMatrix[j][k] = Float.NaN; /* Push this intraMatrix to intraEdgeWeights. */ intraEdgeWeights.add(intraMatrix); } /* Init the inter matrices. */ interEdgeWeights = new ArrayList<>(); for (int i = 0; i < setSizes.length - 1; i++) { float[][] interMatrix = new float[setSizes[i]][setSizes[i + 1]]; /* Give matrix the init values. */ for (int j = 0; j < setSizes[i]; j++) for (int k = 0; k < setSizes[i + 1]; k++) interMatrix[j][k] = Float.NaN; /* Push this interMatrix into interEdgeWeights. */ interEdgeWeights.add(interMatrix); } //Init the top-rear matrix. float[][] interMatrix = new float[setSizes[0]][setSizes[setSizes.length - 1]]; for (int i = 0; i < interMatrix.length; i++) for (int j = 0; j < interMatrix[0].length; j++) interMatrix[i][j] = Float.NaN; interEdgeWeights.add(interMatrix); /* Read the edge weights from the xml input file. */ ArrayList<Element> matrixElementList = new ArrayList<>(docRoot.getChildren("matrix")); /* First check the number of elements in matrixElementList, if not equal to 2*setSizes.length-1. */ // There are 2*setSizes.length matrix, half intra-matrices, half inter-matrices. // No check of the number of the matrices anymore 2015.06.01 //if(matrixElementList.size() != 2*setSizes.length) // throw new IllegalArgumentException("(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) The number of matrices is wrong: "+matrixElementList.size()); /* 2. Assign the edge weights. */ for (Element matrix : matrixElementList) { /* First check where is this matrix. */ String matrixLevel = matrix.getAttributeValue("matrixLevel"); if (matrixLevel == null) /* The matrixLevel attribute is required. */ throw new IllegalArgumentException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) The matrixLevel attribute is required."); String[] matrixLevelSplits = matrixLevel.split("\\s+"); /* If string matrixLevel cannot be splitted into two splits, then it must be wrong. */ if (matrixLevelSplits.length != 2) throw new IllegalArgumentException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) The attribute matrixLevel error: " + matrixLevel); /* Get the two levels. */ int matrixLevel1 = -1, matrixLevel2 = -1; try { matrixLevel1 = Integer.parseInt(String.copyValueOf(matrixLevelSplits[0].toCharArray())); matrixLevel2 = Integer.parseInt(String.copyValueOf(matrixLevelSplits[1].toCharArray())); } catch (NumberFormatException e) { throw new IllegalArgumentException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) The attribute matrixLevel error: " + matrixLevel); } /* Check if the matrix is a intra- or inter edge weight matrix. */ if (matrixLevel1 == matrixLevel2) { /* Intra matrix. */ /* Read the content. */ String matrixContent = matrix.getContent(0).getValue().trim(); /* Check if the matrix is stored in an external file.*/ String hasExternMatrixFile = matrix.getAttributeValue("externalFile"); if (hasExternMatrixFile == null || hasExternMatrixFile.equalsIgnoreCase("false")) { XmlInputParser parser = new XmlInputParser(); parser.parseIntraMatrixString(matrixContent, matrixLevel1, this); } /* If the node names are stored in an external file.*/ else if (hasExternMatrixFile.equalsIgnoreCase("true")) { XmlInputParser parser = new XmlInputParser(); parser.parseIntraMatrixFile(matrixContent, matrixLevel1, this); } else throw new IllegalStateException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml) Illegal attribute of \"externalFile\": " + hasExternMatrixFile); } else { /* For inter-matrix. */ String matrixContent = matrix.getContent(0).getValue().trim(); /* Check if the matrix is stored in an external file.*/ String hasExternMatrixFile = matrix.getAttributeValue("externalFile"); if (hasExternMatrixFile == null || hasExternMatrixFile.equalsIgnoreCase("false")) { XmlInputParser parser = new XmlInputParser(); parser.parseInterMatrixString(matrixContent, matrixLevel1, matrixLevel2, this); } else if (hasExternMatrixFile.equalsIgnoreCase("true")) { XmlInputParser parser = new XmlInputParser(); parser.parseInterMatrixFile(matrixContent, matrixLevel1, matrixLevel2, this); } else throw new IllegalStateException( "(biforce.graphs.MatrixHierGeneralGraph.readGraphInXml))Illegal attribute of \"externalFile\": " + hasExternMatrixFile); } } clusters = new ArrayList<>(); }
From source file:org.apache.maven.io.util.WriterUtils.java
License:Apache License
/** * Method updateElement.//from w w w . j a v a2 s .c o m * * @param counter * @param shouldExist * @param name * @param parent * @return Element */ public static Element updateElement(final IndentationCounter counter, final Element parent, final String name, final boolean shouldExist) { Element element = parent.getChild(name, parent.getNamespace()); if (shouldExist) { if (element == null) { element = factory.element(name, parent.getNamespace()); insertAtPreferredLocation(parent, element, counter); } counter.increaseCount(); } else if (element != null) { final int index = parent.indexOf(element); if (index > 0) { final Content previous = parent.getContent(index - 1); if (previous instanceof Text) { final Text txt = (Text) previous; if (txt.getTextTrim().length() == 0) { parent.removeContent(txt); } } } parent.removeContent(element); } return element; }
From source file:org.codice.ddf.opensearch.source.OpenSearchSource.java
License:Open Source License
private SourceResponseImpl processResponse(InputStream is, QueryRequest queryRequest) throws UnsupportedQueryException { List<Result> resultQueue = new ArrayList<>(); SyndFeedInput syndFeedInput = new SyndFeedInput(); SyndFeed syndFeed = null;/*from w ww . j a v a2s.c o m*/ try { syndFeed = syndFeedInput.build(new InputStreamReader(is, StandardCharsets.UTF_8)); } catch (FeedException e) { LOGGER.debug("Unable to read RSS/Atom feed.", e); } List<SyndEntry> entries; long totalResults = 0; List<Element> foreignMarkup = null; if (syndFeed != null) { entries = syndFeed.getEntries(); for (SyndEntry entry : entries) { resultQueue.addAll(createResponseFromEntry(entry)); } totalResults = entries.size(); foreignMarkup = syndFeed.getForeignMarkup(); for (Element element : foreignMarkup) { if (element.getName().equals("totalResults")) { try { totalResults = Long.parseLong(element.getContent(0).getValue()); } catch (NumberFormatException | IndexOutOfBoundsException e) { // totalResults is already initialized to the correct value, so don't change it here. LOGGER.debug("Received invalid number of results.", e); } } } } SourceResponseImpl response = new SourceResponseImpl(queryRequest, resultQueue); response.setHits(totalResults); if (foreignMarkup != null) { this.foreignMarkupBiConsumer.accept(Collections.unmodifiableList(foreignMarkup), response); } return response; }
From source file:org.codice.ddf.opensearch.source.OpenSearchSource.java
License:Open Source License
/** * Creates a single response from input parameters. Performs XPath operations on the document to * retrieve data not passed in./*w w w. j ava 2 s . com*/ * * @param entry a single Atom entry * @return single response */ private List<Result> createResponseFromEntry(SyndEntry entry) throws UnsupportedQueryException { String id = entry.getUri(); if (StringUtils.isNotEmpty(id)) { id = id.substring(id.lastIndexOf(':') + 1); } List<SyndContent> contents = entry.getContents(); List<SyndCategory> categories = entry.getCategories(); List<Metacard> metacards = new ArrayList<>(); List<Element> foreignMarkup = entry.getForeignMarkup(); String relevance = ""; for (Element element : foreignMarkup) { if (element.getName().equals("score")) { relevance = element.getContent(0).getValue(); } metacards.addAll(processAdditionalForeignMarkups(element, id)); } // we currently do not support downloading content via an RSS enclosure, this support can be // added at a later date if we decide to include it for (SyndContent content : contents) { Metacard metacard = parseContent(content.getValue(), id); if (metacard != null) { metacard.setSourceId(this.shortname); String title = metacard.getTitle(); if (StringUtils.isEmpty(title)) { metacard.setAttribute(new AttributeImpl(Core.TITLE, entry.getTitle())); } metacards.add(metacard); } } for (int i = 0; i < categories.size() && i < metacards.size(); i++) { SyndCategory category = categories.get(i); Metacard metacard = metacards.get(i); if (StringUtils.isBlank(metacard.getContentTypeName())) { metacard.setAttribute(new AttributeImpl(Metacard.CONTENT_TYPE, category.getName())); } } List<Result> results = new ArrayList<>(); for (Metacard metacard : metacards) { ResultImpl result = new ResultImpl(metacard); if (StringUtils.isEmpty(relevance)) { LOGGER.debug("Couldn't find valid relevance. Setting relevance to 0"); relevance = "0"; } result.setRelevanceScore(new Double(relevance)); results.add(result); } return results; }