List of usage examples for org.jdom2.input SAXBuilder build
@Override public Document build(final String systemId) throws JDOMException, IOException
This builds a document from the supplied URI.
From source file:net.phyloviz.loadmlst.io.XMLParser.java
License:Open Source License
private void loadXML() { String endPoint = org.openide.util.NbBundle.getMessage(XMLParser.class, "MLSTDBs.endpoint"); try {/*from w w w. j ava 2 s. c om*/ SAXBuilder builder = new SAXBuilder(); document = builder.build(new URL(endPoint)); } catch (IOException e) { Logger.getLogger(XMLParser.class.getName()).log(Level.WARNING, e.getLocalizedMessage()); // No internet connection document = null; } catch (JDOMException e) { Logger.getLogger(XMLParser.class.getName()).log(Level.WARNING, e.getLocalizedMessage()); // JDOM Error Parsing XML document = null; } }
From source file:net.siegmar.japtproxy.misc.Configuration.java
License:Open Source License
/** * Initialize the configuration with the given config file. * * @param configFile the config file.// ww w . ja va 2 s . c o m * @throws InitializationException is thrown if the configuration is erroneous. */ public Configuration(final File configFile) throws InitializationException { try { final SAXBuilder sax = new SAXBuilder(); final Document doc = sax.build(configFile); final Element rootElement = doc.getRootElement(); cacheDir = new File(rootElement.getChildTextTrim("cache-dir")); httpProxy = rootElement.getChildTextTrim("http-proxy"); final String maxVersionsString = rootElement.getChildTextTrim("max-versions"); maxVersions = NumberUtils.createInteger(maxVersionsString); // support remap definitions // Syntax in config file: /* <remaps> <remap from="sourcehost" to="targethost"/> </remaps> */ final List<Element> remapElements = rootElement.getChild("remaps").getChildren(); for (final Element e : remapElements) { final String from = e.getAttributeValue("from"); final String to = e.getAttributeValue("to"); if (from == null || to == null) { LOG.debug("from or to missing from remap config element"); throw new InitializationException( "Error reading configuration. Remap does not contain from and/or to attribute"); } if (remaps.containsKey(from)) { LOG.debug("Mapping for remap {} already present in config file", from); throw new InitializationException( "Error reading configuration. Duplicate remap config for from: " + from); } LOG.debug("Added remap: {} -> {}", from, to); remaps.put(from, to); } /* final List<Element> backends = rootElement.getChild("backends").getChildren(); // disable backend config for (final Element e : backends) { final List<Element> confUrls = e.getChildren(); if (confUrls == null || confUrls.isEmpty()) { continue; } final String name = e.getAttributeValue("name"); final String type = e.getAttributeValue("type"); if (type == null) { throw new InitializationException("type attribute is missing for backend '" + name + "'"); } final String dir = e.getAttributeValue("dir"); final Backend backend = new Backend(BackendType.valueOf(type.toUpperCase(Locale.ENGLISH))); final File backendDirectory = new File(cacheDir, StringUtils.defaultIfEmpty(dir, name)); FileUtils.forceMkdir(backendDirectory); backend.setDirectory(backendDirectory); for (final Element confUrl : confUrls) { backend.addUrl(new URL(confUrl.getTextTrim())); } backendSystems.put(name, backend); } */ } catch (final IOException | JDOMException e) { throw new InitializationException("Error reading configuration", e); } LOG.debug("Initialized configuration: {}", this); }
From source file:nforce.graphs.BipartiteGraph.java
/** * This method reads the graph in xml format. * @param filePath // w ww . j a va 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."); } /* 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;/* w w w .j av a 2 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 .j av a2s .com * @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 w w w. ja va 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); } //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:nl.nn.adapterframework.util.XmlBuilder.java
License:Apache License
private Element buildElement(String value) throws JDOMException, IOException { StringReader stringReader = new StringReader(value); SAXBuilder saxBuilder = new SAXBuilder(); Document document;/* w w w . ja va 2s .co m*/ document = saxBuilder.build(stringReader); Element element = document.getRootElement(); return element.detach(); }
From source file:no.imr.stox.functions.utils.JDOMUtils.java
public static List<Element> readXML(String fileName, String rootLevel) { try {/* w w w . j a v a2 s. co m*/ SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(new File(fileName)); Element docRoot = doc.getRootElement(); if (rootLevel == null) { rootLevel = docRoot.getName(); } return JDOMUtils.getElementsByTagName(docRoot, rootLevel); } catch (JDOMException | IOException ex) { Logger.getLogger(JDOMUtils.class.getName()).log(Level.SEVERE, null, ex); } return null; }
From source file:nz.ac.massey.cs.guery.adapters.jungalt.io.graphml.GraphMLReader.java
License:Apache License
@Override public synchronized DirectedGraph<V, E> readGraph() throws GraphIOException { Map<String, V> vertices = new HashMap<String, V>(); Map<String, E> edges = new HashMap<String, E>(); // parse/*from w ww. ja v a 2s. co m*/ SAXBuilder builder = new SAXBuilder(); try { Namespace NS_GRAPHML = Namespace.getNamespace("http://graphml.graphdrawing.org/xmlns"); Document doc = builder.build(reader); Element root = doc.getRootElement(); assert "graphml".equals(root.getName()); Element eGraph = root.getChild("graph", NS_GRAPHML); for (Object o : eGraph.getChildren("node", NS_GRAPHML)) { if (o instanceof Element) { buildVertex(vertices, (Element) o); } } for (Object o : eGraph.getChildren("edge", NS_GRAPHML)) { if (o instanceof Element) { E e = buildEdge(vertices, (Element) o); if (edges.containsKey(e.getId())) { throw new GraphIOException( "There are two edges with the same id " + e.getId() + " in the graph"); } edges.put(e.getId(), e); } } // during parsing, composite edges can be created which replace simple edges, those simple edges must be removed from the buffer Collection<CompositeEdge<E>> composites = new HashSet<CompositeEdge<E>>(); for (E e : edges.values()) { if (e instanceof CompositeEdge) { composites.add((CompositeEdge) e); } } // now remove for (CompositeEdge<E> ce : composites) { Collection<E> parts = ce.getParts(); for (E part : parts) { edges.remove(part.getId()); } } // TODO: at this stage both the xml doc and the graph are in memory // we could gc the doc before we continue // build graph } catch (Exception e) { throw new GraphIOException(e); } DirectedGraph<V, E> graph = new DirectedSparseGraph<V, E>(); for (V v : vertices.values()) { graph.addVertex(v); } for (E e : edges.values()) { graph.addEdge(e, e.getStart(), e.getEnd()); } return graph; }