xdi2.core.io.XDIReader类的使用及代码示例

x33g5p2x  于2022-02-03 转载在 其他  
字(11.0k)|赞(0)|评价(0)|浏览(136)

本文整理了Java中xdi2.core.io.XDIReader类的一些代码示例,展示了XDIReader类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。XDIReader类的具体详情如下:
包路径:xdi2.core.io.XDIReader
类名称:XDIReader

XDIReader介绍

[英]Provides methods for reading an XDI graph.
[中]提供读取XDI图的方法。

代码示例

代码示例来源:origin: projectdanube/xdi2

@Override
public void load(MemoryGraph memoryGraph) {
  memoryGraph.clear();
  try {
    File file = new File(this.path);
    if (! file.exists()) {
      if (log.isDebugEnabled()) log.debug("File " + file.getAbsolutePath() + " does not exist. Not loading.");
      return;
    }
    if (log.isDebugEnabled()) log.debug("Loading file " + file.getAbsolutePath());
    Reader reader = new FileReader(file);
    this.xdiReader.read(memoryGraph, reader);
    reader.close();
  } catch (Exception ex) {
    throw new Xdi2RuntimeException("Cannot load file at " + this.path, ex);
  }
}

代码示例来源:origin: projectdanube/xdi2

public static void addReader(Class<? extends XDIReader> readerClass) {
  XDIReader reader;
  try {
    Constructor<? extends XDIReader> constructor = readerClass.getConstructor(Properties.class);
    reader = constructor.newInstance((Properties) null);
  } catch (Exception ex) {
    throw new RuntimeException(ex);
  }
  String format = reader.getFormat();
  String fileExtension = reader.getFileExtension();
  MimeType mimeType = reader.getMimeType();
  if (format != null) readerClassesByFormat.put(format, readerClass);
  if (fileExtension != null) readerClassesByFileExtension.put(fileExtension, readerClass);
  if (mimeType != null) readerClassesByMimeType.put(mimeType, readerClass);
}

代码示例来源:origin: projectdanube/xdi2

private void readGraph(ExecutionContext executionContext) throws Xdi2MessagingException {
  XDIReader xdiReader = XDIReaderRegistry.forFormat(this.mimeType, null);
  if (xdiReader == null) throw new Xdi2MessagingException("Cannot read this format: " + this.mimeType, null, executionContext);
  Graph graph = this.getGraph();
  graph.clear();
  FileReader reader = null;
  try {
    File file = new File(this.path);
    reader = new FileReader(file);
    xdiReader.read(graph, reader);
    reader.close();
  } catch (FileNotFoundException ex) {
  } catch (Exception ex) {
    throw new Xdi2MessagingException("Cannot read file: " + ex.getMessage(), ex, executionContext);
  } finally {
    if (reader != null) {
      try {
        reader.close();
      } catch (Exception ex) { }
    }
  }
  if (xdiReader instanceof AutoReader) this.mimeType = ((AutoReader) xdiReader).getLastSuccessfulReader().getFormat();
  if (this.mimeType == null) this.mimeType = XDIWriterRegistry.getDefault().getFormat();
}

代码示例来源:origin: projectdanube/xdi2

public void testReaders() throws Exception {
  String[] formats = new String[] { "XDI/JSON", "XDI DISPLAY" };
  String[] fileExtensions = new String[] { "json", "xdi" };
  MimeType[] mimeTypes = new MimeType[] { new MimeType("application/xdi+json"), new MimeType("application/xdi+json;contexts=0"), new MimeType("application/xdi+json;contexts=1"), new MimeType("text/xdi"), new MimeType("text/xdi;contexts=0"), new MimeType("text/xdi;contexts=1") };
  for (String format : formats) assertTrue(XDIReaderRegistry.forFormat(format, null).supportsFormat(format));
  for (String fileExtension : fileExtensions) assertTrue(XDIReaderRegistry.forFileExtension(fileExtension, null).supportsFileExtension(fileExtension));
  for (MimeType mimeType : mimeTypes) assertTrue(XDIReaderRegistry.forMimeType(mimeType).supportsMimeType(mimeType));
}

代码示例来源:origin: projectdanube/xdi2

public String getLastSuccessfulFormat() {

    if (this.lastSuccessfulReader == null) return null;

    return this.lastSuccessfulReader.getFormat();
  }
}

代码示例来源:origin: projectdanube/xdi2

@Override
  public Graph parseGraph(String string, MimeType mimeType) throws IOException, Xdi2ParseException {

    Graph graph = this.openGraph();
    XDIReaderRegistry.forMimeType(mimeType).read(graph, new StringReader(string));

    return graph;
  }
}

代码示例来源:origin: projectdanube/xdi2

@Override
public Graph parseGraph(String string, String format, Properties parameters) throws IOException, Xdi2ParseException {
  Graph graph = this.openGraph();
  XDIReaderRegistry.forFormat(format, parameters).read(graph, new StringReader(string));
  return graph;
}

代码示例来源:origin: projectdanube/xdi2

private synchronized void read(Graph graph, byte[] byteArray) throws Xdi2ParseException {
  for (XDIReader xdiReader : readers) {
    if (xdiReader instanceof AutoReader) continue;
    try {
      graph.clear();
      xdiReader.read(graph, new ByteArrayInputStream(byteArray));
      this.lastSuccessfulReader = xdiReader;
      return;
    } catch(Exception ex) {
      continue;
    }
  }
  this.lastSuccessfulReader = null;
  throw new Xdi2ParseException("Unknown serialization format.");
}

代码示例来源:origin: projectdanube/xdi2

private synchronized void read(Graph graph, String string) throws Xdi2ParseException {
  for (XDIReader xdiReader : readers) {
    if (xdiReader instanceof AutoReader) continue;
    try {
      graph.clear();
      xdiReader.read(graph, new StringReader(string));
      this.lastSuccessfulReader = xdiReader;
      return;
    } catch(Exception ex) {
      continue;
    }
  }
  this.lastSuccessfulReader = null;
  throw new Xdi2ParseException("Unknown serialization format.");
}

代码示例来源:origin: projectdanube/xdi2

@Override
public void load(MemoryGraph memoryGraph) {
  memoryGraph.clear();
  String classpath = this.getClasspath();
  if (classpath.startsWith("/")) classpath = classpath.substring(1);
  try {
    if (log.isDebugEnabled()) log.debug("Loading classpath " + classpath);
    InputStream stream = this.getClassLoader().getResourceAsStream(classpath);
    this.xdiReader.read(memoryGraph, stream);
    stream.close();
  } catch (Exception ex) {
    throw new Xdi2RuntimeException("Cannot load classpath at " + classpath + " with classloader " + this.getClassLoader().getClass().getCanonicalName(), ex);
  }
}

代码示例来源:origin: projectdanube/xdi2

@Override
public void load(MemoryGraph memoryGraph) {
  memoryGraph.clear();
  try {
    if (log.isDebugEnabled()) log.debug("Loading URL " + this.getUrl());
    InputStream stream = this.getUrl().openStream();
    this.xdiReader.read(memoryGraph, stream);
    stream.close();
  } catch (Exception ex) {
    throw new Xdi2RuntimeException("Cannot load URL at " + this.getUrl(), ex);
  }
}

代码示例来源:origin: projectdanube/xdi2

xdiReader.read(graph, reader);
} catch (IOException ex) {

代码示例来源:origin: projectdanube/xdi2

private static MessageEnvelope readFromBody(HttpTransportRequest request, HttpTransportResponse response) throws IOException {
  InputStream inputStream = request.getBodyInputStream();
  // try to find an appropriate reader for the provided mime type
  XDIReader xdiReader = null;
  String contentType = request.getContentType();
  MimeType recvMimeType = contentType != null ? new MimeType(contentType) : null;
  xdiReader = recvMimeType != null ? XDIReaderRegistry.forMimeType(recvMimeType) : null;
  if (xdiReader == null) xdiReader = XDIReaderRegistry.getDefault();
  // read everything into an in-memory XDI graph (a message envelope)
  if (log.isDebugEnabled()) log.debug("Reading message in " + recvMimeType + " with reader " + xdiReader.getClass().getSimpleName() + ".");
  MessageEnvelope messageEnvelope;
  try {
    Graph graph = MemoryGraphFactory.getInstance().openGraph();
    xdiReader.read(graph, inputStream);
    messageEnvelope = MessageEnvelope.fromGraph(graph);
  } catch (IOException ex) {
    throw ex;
  } catch (Exception ex) {
    log.error("Cannot parse XDI graph: " + ex.getMessage(), ex);
    throw new IOException("Cannot parse XDI graph: " + ex.getMessage(), ex);
  } finally {
    inputStream.close();
  }
  if (log.isDebugEnabled()) log.debug("Message envelope received (" + messageEnvelope.getMessageCount() + " messages). Executing...");
  // done
  return messageEnvelope;
}

代码示例来源:origin: projectdanube/xdi2

private static MessageEnvelope read(WebSocketTransportRequest request, WebSocketTransportResponse response) throws IOException {
  Reader reader = request.getReader();
  // try to find an appropriate reader for the provided mime type
  XDIReader xdiReader = null;
  String contentType = request.getNegotiatedSubprotocol();
  MimeType recvMimeType = contentType != null ? new MimeType(contentType) : null;
  xdiReader = recvMimeType != null ? XDIReaderRegistry.forMimeType(recvMimeType) : null;
  if (xdiReader == null) xdiReader = XDIReaderRegistry.getDefault();
  // read everything into an in-memory XDI graph (a message envelope)
  if (log.isDebugEnabled()) log.debug("Reading message in " + recvMimeType + " with reader " + xdiReader.getClass().getSimpleName() + ".");
  MessageEnvelope messageEnvelope;
  try {
    Graph graph = MemoryGraphFactory.getInstance().openGraph();
    xdiReader.read(graph, reader);
    messageEnvelope = MessageEnvelope.fromGraph(graph);
  } catch (IOException ex) {
    throw ex;
  } catch (Exception ex) {
    log.error("Cannot parse XDI graph: " + ex.getMessage(), ex);
    throw new IOException("Cannot parse XDI graph: " + ex.getMessage(), ex);
  } finally {
    reader.close();
  }
  if (log.isDebugEnabled()) log.debug("Message envelope received (" + messageEnvelope.getMessageCount() + " messages). Executing...");
  // done
  return messageEnvelope;
}

代码示例来源:origin: projectdanube/xdi2

/**
 * Returns the graph from a normalized string.
 */
public static Graph deserialize(String string) throws Xdi2ParseException, IOException {
  Graph tempGraph = MemoryGraphFactory.getInstance().openGraph();
  try {
    XDIREADER.read(tempGraph, new StringReader(string));
  } catch (Xdi2ParseException ex) {
    tempGraph.close();
    throw ex;
  } catch (IOException ex) {
    tempGraph.close();
    throw ex;
  }
  return tempGraph;
}

代码示例来源:origin: projectdanube/xdi2

reader.read(messagingResponseGraph, inputStream);
  inputStream.close();
} catch (Exception ex) {

代码示例来源:origin: projectdanube/xdi2

xdiReader.read(graph, new StringReader(graphstring));
} catch (Xdi2ParseException ex) {
  xdiReader.read(messageEnvelope.getGraph(), new StringReader(graphstring));

代码示例来源:origin: projectdanube/xdi2

public void testReadJson() throws Exception {
  Graph graph3 = this.getGraphFactory().openGraph(this.getClass().getName() + "-graph-3");
  XDIReader reader = XDIReaderRegistry.forFormat("XDI/JSON", null);
  reader.read(graph3, AbstractGraphTest.class.getResourceAsStream("test.json")).close();
  testGraph(graph3);
  graph3.getRootContextNode().clear();
  assertTrue(graph3.isEmpty());
  graph3.close();
}

代码示例来源:origin: projectdanube/xdi2

public void testReadWriteFormats() throws Exception {
  String[] formats = new String[] { "XDI/JSON", "XDI DISPLAY" };
  for (int i=0; i<formats.length; i++) {
    File file = new File("xdi.out");
    Graph graph4 = this.getGraphFactory().openGraph(this.getClass().getName() + "-graph-4" + "-" + i);
    Graph graph5 = this.getGraphFactory().openGraph(this.getClass().getName() + "-graph-5" + "-" + i);
    XDIWriter writer = XDIWriterRegistry.forFormat(formats[i], null);
    XDIReader reader = XDIReaderRegistry.forFormat(formats[i], null);
    FileWriter fileWriter = new FileWriter(file);
    FileReader fileReader = new FileReader(file);
    makeGraph(graph4);
    writer.write(graph4, fileWriter);
    reader.read(graph5, fileReader);
    fileWriter.close();
    fileReader.close();
    testGraph(graph5);
    testGraphsEqual(graph4, graph5);
    graph4.close();
    graph5.close();
    file.delete();
  }
}

代码示例来源:origin: projectdanube/xdi2

autoReader.read(graph, this.getClass().getResourceAsStream("graph" + i + ".xdi")).close();
    autoReader.read(message, this.getClass().getResourceAsStream("message" + i + "." + ii + ".xdi")).close();
    autoReader.read(positive, this.getClass().getResourceAsStream("positive" + i + "." + ii + ".xdi")).close();
    autoReader.read(negative, this.getClass().getResourceAsStream("negative" + i + "." + ii + ".xdi")).close();
    autoReader.read(exception, this.getClass().getResourceAsStream("exception" + i + "." + ii + ".xdi")).close();

相关文章

XDIReader类方法