Ejemplo n.º 1
0
 public static SAXParser getSaxParser() throws ParserConfigurationException, SAXException {
   SAXParserFactory parserFactory = SAXParserFactory.newInstance();
   parserFactory.setNamespaceAware(false);
   parserFactory.setValidating(true);
   parserFactory.setXIncludeAware(false);
   return parserFactory.newSAXParser();
 }
Ejemplo n.º 2
0
  /**
   * @see MessageBodyReader#readFrom(Class, Type, MediaType, Annotation[], MultivaluedMap,
   *     InputStream)
   */
  @Override
  public Object readFrom(
      Class<Object> type,
      Type genericType,
      Annotation[] annotations,
      MediaType mediaType,
      MultivaluedMap<String, String> httpHeaders,
      InputStream entityStream)
      throws IOException {
    try {
      SAXParserFactory spf = SAXParserFactory.newInstance();
      spf.setXIncludeAware(isXIncludeAware());
      spf.setNamespaceAware(true);
      spf.setValidating(isValidatingDtd());
      spf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, isSecureProcessing());
      spf.setFeature(
          "http://xml.org/sax/features/external-general-entities", isExpandingEntityRefs());
      spf.setFeature(
          "http://xml.org/sax/features/external-parameter-entities", isExpandingEntityRefs());

      XMLReader reader = spf.newSAXParser().getXMLReader();
      JAXBContext jaxbContext = getJaxbContext(type);
      Unmarshaller um = jaxbContext.createUnmarshaller();
      return um.unmarshal(new SAXSource(reader, new InputSource(entityStream)));
    } catch (Exception e) {
      throw new IOException("Could not unmarshal to " + type.getName());
    }
  }
 /**
  * Return the registered SAX parser factory or null if one does not exist. * @return
  *
  * @return returns a SAXParserFactory
  */
 public SAXParserFactory getFactory() {
   if (saxParserFactory == null) {
     saxParserFactory = new SAXParserFactoryImpl();
   }
   SAXParserFactory theFactory = saxParserFactory;
   if (theFactory != null) {
     theFactory.setNamespaceAware(true);
     theFactory.setXIncludeAware(false);
     theFactory.setValidating(false);
   }
   return theFactory;
 }
Ejemplo n.º 4
0
  @Override
  public final void load(File model) throws Exception {
    long start = System.currentTimeMillis();
    if (trained) {
      throw new VerifyError("Model can not be overloaded! Create new emtpy Instance of markov!");
    }

    logger.info("reading " + model.getAbsolutePath() + " (" + (model.length() / 1024) + " kb)");

    GraphXmlHandler graphXmlHandler = new GraphXmlHandler();
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setValidating(true);
    factory.setXIncludeAware(true);

    SAXParser parser = factory.newSAXParser();
    parser.parse(model, graphXmlHandler);

    logger.info(
        "adding "
            + graphXmlHandler.getListVertex().size()
            + " vertices and "
            + graphXmlHandler.getListEdge().size()
            + " edges");
    for (String edgeConfig : graphXmlHandler.getListEdge()) {
      String[] parts = edgeConfig.split(";"); // source;target;weight

      // source
      String[] src = parts[0].split(":"); // source=aa:sse:hp
      String aa = src[0].intern();
      String sse = src[1].intern();
      double hp = Double.parseDouble(src[2].intern());
      Vertex source = new Vertex(aa, sse, hp);
      mapVertex.put(source.toString(), source);

      // target
      String[] trg = parts[1].split(":"); // target=aa:sse:hp
      aa = trg[0].intern();
      sse = trg[1].intern();
      hp = Double.parseDouble(trg[2].intern());
      Vertex target = new Vertex(aa, sse, hp);
      mapVertex.put(target.toString(), target);

      // add vertices, edge
      if (!wintermute.containsVertex(source)) {
        wintermute.addVertex(source);
      }
      if (!wintermute.containsVertex(target)) {
        wintermute.addVertex(target);
      }

      Edge edge = wintermute.addEdge(source, target);

      // edge
      String[] edgeId = parts[2].split(":"); // id=weight:weightTmh:weightNonTmh:windowPos
      double weightComplete = Double.parseDouble(edgeId[0]);
      double weightTmh = Double.parseDouble(edgeId[1]);
      double weightNonTmh = Double.parseDouble(edgeId[2]);
      int windowPos = Integer.parseInt(edgeId[3]);

      {
        edge.setWeightComplete(weightComplete);
        edge.setWeightTmh(weightTmh);
        edge.setWeightNonTmh(weightNonTmh);
        edge.setWindowPos(windowPos);
        // verify weight
        if ((weightComplete - (weightTmh + weightNonTmh)) >= 0.001d) {
          throw new VerifyError(
              "Edge is corrupted and can not be set! Export new model!" + "\nedge: " + edge);
        }
      }
    }

    // missing vertices, which have no edges
    for (String vertex : graphXmlHandler.getListVertex()) {
      String[] parts = vertex.split(":");
      String aa = parts[0].intern();
      String sse = parts[1].intern();
      Double hp = Double.valueOf(parts[2].intern());
      Vertex tmp = new Vertex(aa, sse, hp);
      if (!wintermute.containsVertex(tmp)) {
        wintermute.addVertex(tmp);
      }
    }

    verify(model);

    trained = true;
    long end = System.currentTimeMillis();
    logger.info("-> in " + (end - start) + " ms");
  }