private Collection<ConnectableDefinition> loadConnectableDefs(File connectableDefs) {
    Collection<ConnectableDefinition> defs = new HashSet<ConnectableDefinition>();
    try {
      Builder parser = new Builder();
      Document doc = parser.build(connectableDefs);
      Element root = doc.getRootElement();

      Elements definitions = root.getChildElements();
      for (int i = 0; i < definitions.size(); i++) {
        Element definition = definitions.get(i);
        String id = definition.getAttributeValue("id");
        String pinCount = definition.getAttributeValue("pins");
        String type = definition.getAttributeValue("type");

        ConnectableDefinition d = new ConnectableDefinition(id, type, Integer.parseInt(pinCount));

        Elements pins = definition.getChildElements();
        for (int j = 0; j < pins.size(); j++) {
          Element pinElement = pins.get(j);
          String pinNumber = pinElement.getAttributeValue("number");
          String pinName = pinElement.getAttributeValue("name");
          d.addPin(Integer.parseInt(pinNumber), pinName);
        }

        defs.add(d);
      }
    } catch (ParsingException ex) {
      System.err.println("malformed XML file : " + ex.getMessage());
    } catch (IOException ex) {
      System.err.println("io error : " + ex.getMessage());
    }
    return defs;
  }
예제 #2
0
    public void map(LongWritable key, Text value, Context context)
        throws IOException, InterruptedException {

      String strId = "";
      String strBody = "";

      // Parse the xml and read data (page id and article body)
      // Using XOM library
      Builder builder = new Builder();

      try {
        Document doc = builder.build(value.toString(), null);

        Nodes nodeId = doc.query("//eecs485_article_id");
        strId = nodeId.get(0).getChild(0).getValue();

        Nodes nodeBody = doc.query("//eecs485_article_body");
        strBody = nodeBody.get(0).getChild(0).getValue();
      } catch (ParsingException ex) {
        System.out.println("Not well-formed.");
        System.out.println(ex.getMessage());
      } catch (IOException ex) {
        System.out.println("io exception");
      }

      // Tokenize document body
      Pattern pattern = Pattern.compile("\\w+");
      Matcher matcher = pattern.matcher(strBody);

      while (matcher.find()) {
        // Write the parsed token
        // key = term, docid   value = 1
        context.write(new Text(matcher.group() + "," + strId), one);
      }
    }
예제 #3
0
 private void carregarFitxer(String rutaFitxer) throws ParcAtraccionsExcepcio {
   Builder builder = new Builder();
   try {
     doc = builder.build(rutaFitxer);
   } catch (Exception e) {
     throw new ParcAtraccionsExcepcio("GestorXML.carregar");
   }
 }
  /**
   * Read urn.
   *
   * @param file the file
   * @return The URN specified in the METS file or null if the METS file doesn't specify an URN
   * @throws IOException Signals that an I/O exception has occurred.
   * @throws ParseException the parse exception
   * @author Thomas Kleinke
   */
  public String readURN(File file) throws IOException, ParseException {

    FileInputStream fileInputStream = new FileInputStream(file);
    BOMInputStream bomInputStream = new BOMInputStream(fileInputStream);

    XMLReader xmlReader = null;
    SAXParserFactory spf = SAXParserFactory.newInstance();
    try {
      xmlReader = spf.newSAXParser().getXMLReader();
    } catch (Exception e) {
      fileInputStream.close();
      bomInputStream.close();
      throw new IOException("Error creating SAX parser", e);
    }
    xmlReader.setErrorHandler(err);
    NodeFactory nodeFactory = new PremisXmlReaderNodeFactory();
    Builder parser = new Builder(xmlReader, false, nodeFactory);
    logger.trace("Successfully built builder and XML reader");

    try {
      String urn = null;

      Document doc = parser.build(bomInputStream);
      Element root = doc.getRootElement();

      Element dmdSecEl = root.getFirstChildElement("dmdSec", METS_NS);
      if (dmdSecEl == null) return null;

      Element mdWrapEl = dmdSecEl.getFirstChildElement("mdWrap", METS_NS);
      if (mdWrapEl == null) return null;

      Element xmlDataEl = mdWrapEl.getFirstChildElement("xmlData", METS_NS);
      if (xmlDataEl == null) return null;

      Element modsEl = xmlDataEl.getFirstChildElement("mods", MODS_NS);
      if (modsEl == null) return null;

      Elements identifierEls = modsEl.getChildElements("identifier", MODS_NS);
      for (int i = 0; i < identifierEls.size(); i++) {
        Element element = identifierEls.get(i);
        Attribute attribute = element.getAttribute("type");
        if (attribute.getValue().toLowerCase().equals("urn")) urn = element.getValue();
      }

      if (urn != null && urn.equals("")) urn = null;

      return urn;
    } catch (ValidityException ve) {
      throw new IOException(ve);
    } catch (ParsingException pe) {
      throw new IOException(pe);
    } catch (IOException ie) {
      throw new IOException(ie);
    } finally {
      fileInputStream.close();
      bomInputStream.close();
    }
  }
  protected ComponentSet loadSet(File componentsDef, File connectionsDef) {
    ComponentSet set = null;

    // for collecting all connectables
    Map<String, Connectable> connectables = new HashMap<String, Connectable>();

    try {
      // open the components.xml file
      Builder builder = new Builder();
      Document doc = builder.build(componentsDef);
      Element componentsRoot = doc.getRootElement();

      int rows = Integer.parseInt(componentsRoot.getAttributeValue("rows"));
      int cols = Integer.parseInt(componentsRoot.getAttributeValue("cols"));
      boolean autoLocate = Boolean.parseBoolean(componentsRoot.getAttributeValue("autoLocate"));

      // add connectables (components need to be located in the set,
      // endpoints are not so they are just added to the connectables map
      Elements componentElements = componentsRoot.getChildElements();
      if (autoLocate) {
        set = addComponentsWithAutoLocate(connectables, rows, cols, componentElements);
      } else {
        set = addComponents(connectables, rows, cols, componentElements);
      }

      doc = builder.build(connectionsDef);
      Element connectionsRoot = doc.getRootElement();

      // add connections
      Elements connections = connectionsRoot.getChildElements();
      for (int i = 0; i < connections.size(); i++) {
        Element c = connections.get(i);
        String from = c.getAttributeValue("from");
        String to = c.getAttributeValue("to");
        String fromPinLabel = c.getAttributeValue("fromPin");
        String toPinLabel = c.getAttributeValue("toPin");

        Connectable fromComp = connectables.get(from);
        Connectable toComp = connectables.get(to);

        Collection<Pin> fromPins = fromComp.getPins(fromPinLabel);
        Collection<Pin> toPins = toComp.getPins(toPinLabel);

        Connection con = new Connection(fromComp, fromPins, toComp, toPins);
        set.addConnection(con);
      }

    } catch (ParsingException ex) {
      System.err.println("malformed XML file : " + ex.getMessage());
    } catch (IOException ex) {
      System.err.println("io error : " + ex.getMessage());
    }
    return set;
  }
예제 #6
0
  @Override
  Document processResponse(String url, HttpMethod method, InputStream input)
      throws FogBugzException, IOException, OperationCanceledException {
    Builder builder = new Builder();
    try {
      return builder.build(input);
    } catch (ParsingException e) {
      // abort method, because full response might have not been read
      method.abort();

      throw parsingError(e, url);
    }
  }
  private Element getResultElement(HttpResponse res) throws ICTomorrowApiException {
    try {
      Builder builder = new Builder();
      Document document = builder.build(res.body(), null);
      Element rootElement = document.getRootElement();

      String statusCode = rootElement.getAttributeValue("status_code");
      String statusMessage = rootElement.getAttributeValue("status_message");
      if (statusCode.equals("200")) {
        return rootElement;
      } else {
        throw new ICTomorrowApiException(
            "Request returned an error response: " + res.body(), statusCode, statusMessage);
      }
    } catch (ParsingException e) {
      throw new ICTomorrowApiException("Exception while parsing response: " + res.body(), e);
    } catch (IOException e) {
      throw new ICTomorrowApiException("Exception while parsing response: " + res.body(), e);
    }
  }
예제 #8
0
  public static void main(String[] args) {

    if (args.length <= 0) {
      System.out.println("Usage: java nu.xom.samples.PureValidator URL");
      return;
    }

    try {
      Builder parser = new Builder(true, new MinimalNodeFactory());
      parser.build(args[0]);
      System.out.println(args[0] + " is valid.");
    } catch (ValidityException ex) {
      System.out.println(args[0] + " is not valid.");
      System.out.println(ex.getMessage());
      System.out.println(" at line " + ex.getLineNumber() + ", column " + ex.getColumnNumber());
    } catch (ParsingException ex) {
      System.out.println(args[0] + " is not well-formed.");
      System.out.println(ex.getMessage());
      System.out.println(" at line " + ex.getLineNumber() + ", column " + ex.getColumnNumber());
    } catch (IOException ex) {
      System.out.println("Due to an IOException, the parser could not check " + args[0]);
    }
  }
예제 #9
0
  public static void doStatisticsForXMLFile(String filePath) {
    try {
      Builder builder = new Builder(false);
      Document doc = builder.build(filePath);
      Element corpus = doc.getRootElement();
      Elements lexeltElements = corpus.getChildElements("lexelt");

      Map<String, Integer> posMap = new HashMap<String, Integer>();

      for (int k = 0; k < lexeltElements.size(); k++) {
        Element lexeltElement = lexeltElements.get(k);
        Elements instanceElements = lexeltElement.getChildElements("instance");
        for (int i = 0; i < instanceElements.size(); i++) {
          Element instanceElement = instanceElements.get(i);
          Element postaggingElement = instanceElement.getFirstChildElement("postagging");
          Elements wordElements = postaggingElement.getChildElements("word");
          for (int j = 0; j < wordElements.size(); j++) {
            Element wordElement = wordElements.get(j);
            Elements subwordElements = wordElement.getChildElements("subword");
            if (subwordElements.size() == 0) {
              String pos = wordElement.getAttributeValue("pos");
              doIncrement(posMap, pos);
            } else {
              for (int m = 0; m < subwordElements.size(); m++) {
                Element subwordElement = subwordElements.get(m);
                String pos = subwordElement.getAttributeValue("pos");
                doIncrement(posMap, pos);
              }
            }
          }
        }
      }
      doStatistics(filePath, posMap);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
예제 #10
0
  public GlosslistHandler(String inDir, String outDir) throws Exception {
    Directory in = new Directory(inDir);
    if (!in.exists()) throw new IOException(inDir);

    Collection<File> files = in.getFiles();

    con = new XPathContext();
    con.addNamespace("db", dbns);

    for (File file : files) {
      System.out.println("processing glossentries in " + file.getName());
      if (file.getCanonicalPath().endsWith(".xml")) {
        Builder builder = new Builder();
        Document doc = builder.build(file);
        try {
          doc = process(doc);
        } catch (Exception e) {
          System.err.println("Error processing " + file.getName());
          throw e;
        }
        serialize(doc, new File(outDir, file.getName()));
      }
    }
  }
예제 #11
0
  public static SaxonXQueryExpression.Result evaluateXQuery(
      final SaxonXQueryExpression xquery,
      Object context,
      Map<String, Object> parameterValues,
      final RowProcessor processor,
      CommandContext commandContext)
      throws TeiidProcessingException, TeiidComponentException {
    DynamicQueryContext dynamicContext = new DynamicQueryContext(xquery.config);

    SaxonXQueryExpression.Result result = new SaxonXQueryExpression.Result();
    try {
      try {
        for (Map.Entry<String, Object> entry : parameterValues.entrySet()) {
          Object value = entry.getValue();
          if (value instanceof SQLXML) {
            value = XMLSystemFunctions.convertToSource(value);
            result.sources.add((Source) value);
            value = wrapStax((Source) value, xquery.getConfig());
          } else if (value instanceof java.util.Date) {
            value = XMLSystemFunctions.convertToAtomicValue(value);
          } else if (value instanceof BinaryType) {
            value = new HexBinaryValue(((BinaryType) value).getBytesDirect());
          }
          dynamicContext.setParameter(entry.getKey(), value);
        }
      } catch (TransformerException e) {
        throw new TeiidProcessingException(QueryPlugin.Event.TEIID30148, e);
      }
      if (context != null) {
        Source source = XMLSystemFunctions.convertToSource(context);
        result.sources.add(source);
        source = wrapStax(source, xquery.getConfig());
        if (xquery.contextRoot != null) {
          // create our own filter as this logic is not provided in the free saxon
          ProxyReceiver filter = new PathMapFilter(xquery.contextRoot);
          AugmentedSource sourceInput = AugmentedSource.makeAugmentedSource(source);
          sourceInput.addFilter(filter);
          source = sourceInput;

          // use streamable processing instead
          if (xquery.streamingPath != null && processor != null) {
            if (LogManager.isMessageToBeRecorded(LogConstants.CTX_DQP, MessageLevel.DETAIL)) {
              LogManager.logDetail(
                  LogConstants.CTX_DQP,
                  "Using stream processing for evaluation of",
                  xquery.xQueryString); // $NON-NLS-1$
            }
            // set to non-blocking in case default expression evaluation blocks
            boolean isNonBlocking = commandContext.isNonBlocking();
            commandContext.setNonBlocking(true);

            final StreamingTransform myTransform =
                new StreamingTransform() {
                  public Nodes transform(Element elem) {
                    processor.processRow(XQueryEvaluator.wrap(elem, xquery.config));
                    return NONE;
                  }
                };

            Builder builder =
                new Builder(
                    new SaxonReader(xquery.config, sourceInput),
                    false,
                    new StreamingPathFilter(xquery.streamingPath, xquery.namespaceMap)
                        .createNodeFactory(null, myTransform));
            try {
              // the builder is hard wired to parse the source, but the api will throw an exception
              // if the stream is null
              builder.build(FAKE_IS);
              return result;
            } catch (ParsingException e) {
              if (e.getCause() instanceof TeiidRuntimeException) {
                RelationalNode.unwrapException((TeiidRuntimeException) e.getCause());
              }
              throw new TeiidProcessingException(
                  QueryPlugin.Event.TEIID30151,
                  e,
                  QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30151));
            } catch (IOException e) {
              throw new TeiidProcessingException(
                  QueryPlugin.Event.TEIID30151,
                  e,
                  QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30151));
            } finally {
              if (!isNonBlocking) {
                commandContext.setNonBlocking(false);
              }
            }
          }
        }
        DocumentInfo doc;
        try {
          doc = xquery.config.buildDocument(source);
        } catch (XPathException e) {
          throw new TeiidProcessingException(
              QueryPlugin.Event.TEIID30151, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30151));
        }
        dynamicContext.setContextItem(doc);
      }
      try {
        result.iter = xquery.xQuery.iterator(dynamicContext);
        return result;
      } catch (TransformerException e) {
        throw new TeiidProcessingException(
            QueryPlugin.Event.TEIID30152, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30152));
      }
    } finally {
      if (result.iter == null) {
        result.close();
      }
    }
  }
예제 #12
0
  @Test
  public void testGeneration() throws Exception {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    // new XmlInstanceGenerator(SampleA.class).generateXmlSchema(System.out, false);
    new XmlInstanceGenerator(SampleA.class).generateXmlSchema(bos, false);

    Builder domBuilder = new Builder();
    Document doc = domBuilder.build(new ByteArrayInputStream(bos.toByteArray()));

    String namespace = XmlInstanceGenerator.getNamespace(SampleA.class);

    Element config = doc.getRootElement();
    Assert.assertEquals("config", config.getLocalName());

    Elements i = config.getChildElements("i", namespace);
    Assert.assertEquals(1, i.size());
    Assert.assertEquals("0", i.get(0).getValue());

    Elements s = config.getChildElements("s", namespace);
    Assert.assertEquals(1, s.size());
    Assert.assertEquals("abc", s.get(0).getValue());

    Elements r = config.getChildElements("r1", namespace);
    Assert.assertEquals(1, r.size());
    Assert.assertEquals("TODO", r.get(0).getValue());

    r = config.getChildElements("r2", namespace);
    Assert.assertEquals(1, r.size());
    Assert.assertEquals("TODO", r.get(0).getValue());

    r = config.getChildElements("r3", namespace);
    Assert.assertEquals(1, r.size());
    Assert.assertEquals("SampleC", r.get(0).getValue());

    Elements l = config.getChildElements("l", namespace);
    Assert.assertEquals(1, l.size());
    Elements values = l.get(0).getChildElements("value", namespace);
    Assert.assertEquals(2, values.size());
    Assert.assertEquals("abc", values.get(0).getValue());
    Assert.assertEquals("xyz", values.get(1).getValue());

    Elements m = config.getChildElements("m", namespace);
    Assert.assertEquals(1, m.size());
    Elements entries = m.get(0).getChildElements("entry", namespace);
    Assert.assertEquals(2, entries.size());
    Assert.assertEquals("ABC", entries.get(0).getValue());
    Assert.assertEquals("1", entries.get(0).getAttribute("key").getValue());
    Assert.assertEquals("XYZ", entries.get(1).getValue());
    Assert.assertEquals("2", entries.get(1).getAttribute("key").getValue());

    Elements lr = config.getChildElements("lr", namespace);
    Assert.assertEquals(1, lr.size());
    values = lr.get(0).getChildElements("value", namespace);
    Assert.assertEquals(1, values.size());
    Assert.assertEquals("", values.get(0).getValue());

    Elements mr = config.getChildElements("mr", namespace);
    Assert.assertEquals(1, mr.size());
    entries = mr.get(0).getChildElements("entry", namespace);
    Assert.assertEquals(1, entries.size());
    Assert.assertEquals("", entries.get(0).getValue());
    Assert.assertEquals("", entries.get(0).getAttribute("key").getValue());
  }
예제 #13
0
  /**
   * Removes a model. Removes extension and any files.
   *
   * @param model
   */
  public void removeModel(IDSTest model, String pluginID) {

    // Remove from DSBusinessModel
    dsBusinessModel.getTests().remove(model);
    for (Endpoint ep : dsBusinessModel.getEndpoints()) {
      if (ep.getTests() != null) {
        ep.getTests().remove(model);
        logger.debug("Removed model " + model.getName() + " from EP " + ep.getName());
      }
    }

    // Remove extensions in models.container plugin
    // =============================================
    File pluginXMLfile;
    try {
      pluginXMLfile = new File(FileUtil.getFilePath("plugin.xml", pluginID));

      Builder parser = new Builder();
      Document doc = parser.build(pluginXMLfile);
      Element root = doc.getRootElement();

      Element extension = null;

      // Find extension in plugin.xml, if exists
      Elements existingExtensions = root.getChildElements("extension");
      if (existingExtensions != null && existingExtensions.size() > 0) {
        for (int i = 0; i < existingExtensions.size(); i++) {
          extension = existingExtensions.get(i);

          // If exists a model with same name, remove it
          Elements existingTests = extension.getChildElements("test");
          if (existingTests != null && existingTests.size() > 0) {
            for (int j = 0; j < existingTests.size(); j++) {
              Element test = existingTests.get(j);
              String testName = test.getAttribute("name").getValue();

              // Remove spaces included by XML serialization
              while (testName.contains("  ")) testName = testName.replace("  ", " ");

              if (model.getName().equals(testName)) {
                test.getParent().removeChild(test);
                logger.debug("Removing existing model extension: " + model.getName());

                // Also remove the files
                Elements resources = test.getChildElements("resource");
                for (int rescnt = 0; rescnt < resources.size(); rescnt++) {
                  Element resource = resources.get(rescnt);
                  String path = resource.getAttribute("path").getValue();
                  try {
                    File reFile = new File(FileUtil.getFilePath(path, pluginID));
                    if (reFile.exists()) {
                      logger.debug("Removing file: " + reFile);
                      reFile.delete();
                    } else logger.debug("Unable to locate file to remove: " + path);
                  } catch (Exception e) {
                    logger.error("Problems removing file: " + path);
                  }
                }
              }
            }
          }
        }
      }

      // Serialize the updated plugin.xml to file
      Serializer serializer = new Serializer(new FileOutputStream(pluginXMLfile));
      serializer.setIndent(4);
      serializer.setMaxLength(64);
      serializer.write(doc);

    } catch (IllegalArgumentException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (ValidityException e) {
      e.printStackTrace();
    } catch (ParsingException e) {
      e.printStackTrace();
    }
  }