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

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

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

XDIWriterRegistry介绍

[英]Provides an appropriate XDIWriter for a given type.
[中]为给定类型提供适当的XDIWriter。

代码示例

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

  1. private static void sendText(WebSocketTransportRequest request, WebSocketTransportResponse response, TransportMessagingResponse messagingResponse) throws IOException {
  2. // use default writer
  3. XDIWriter writer = null;
  4. MimeType sendMimeType = null;
  5. writer = sendMimeType != null ? XDIWriterRegistry.forMimeType(sendMimeType) : null;
  6. if (writer == null) writer = XDIWriterRegistry.getDefault();
  7. // send out the message result
  8. if (log.isDebugEnabled()) log.debug("Sending result in " + sendMimeType + " with writer " + writer.getClass().getSimpleName() + ".");
  9. StringWriter buffer = new StringWriter();
  10. writer.write(messagingResponse.getGraph(), buffer);
  11. // TODO figure out if we can use .getAsync() and avoid concurrency problems
  12. if (buffer.getBuffer().length() > 0) {
  13. synchronized (response.getBasic()) {
  14. response.getBasic().sendText(buffer.getBuffer().toString());
  15. }
  16. }
  17. if (log.isDebugEnabled()) log.debug("Output complete.");
  18. }

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

  1. @Override
  2. public String toString(String format, Properties parameters) {
  3. if (format == null) format = XDIWriterRegistry.getDefault().getFormat();
  4. XDIWriter writer = XDIWriterRegistry.forFormat(format, parameters);
  5. if (writer == null) throw new Xdi2RuntimeException("Unknown format for XDI serialization: " + format);
  6. StringWriter buffer = new StringWriter();
  7. try {
  8. writer.write(this, buffer);
  9. } catch (IOException ex) {
  10. return "[Exception: " + ex.getMessage() + "]";
  11. }
  12. return buffer.toString();
  13. }

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

  1. @Override
  2. public String toString(MimeType mimeType) {
  3. if (mimeType == null) throw new NullPointerException();
  4. XDIWriter writer = XDIWriterRegistry.forMimeType(mimeType);
  5. if (writer == null) throw new Xdi2RuntimeException("Unknown MIME type for XDI serialization: " + mimeType);
  6. StringWriter buffer = new StringWriter();
  7. try {
  8. writer.write(this, buffer);
  9. } catch (IOException ex) {
  10. return "[Exception: " + ex.getMessage() + "]";
  11. }
  12. return buffer.toString();
  13. }

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

  1. /**
  2. * Returns an XDIWriter for the specified format, e.g.
  3. * <ul>
  4. * <li>XDI/JSON</li>
  5. * <li>XDI DISPLAY</li>
  6. * </ul>
  7. * @param format The desired format.
  8. * @return An XDIWriter, or null if no appropriate implementation could be found.
  9. */
  10. public static XDIWriter forFormat(String format, Properties parameters) {
  11. if (format == null) return XDIWriterRegistry.getDefault();
  12. Class<? extends XDIWriter> writerClass = writerClassesByFormat.get(format);
  13. if (writerClass == null) return null;
  14. try {
  15. Constructor<? extends XDIWriter> constructor = writerClass.getConstructor(Properties.class);
  16. return constructor.newInstance(parameters);
  17. } catch (Exception ex) {
  18. throw new RuntimeException(ex);
  19. }
  20. }

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

  1. public void testWriters() throws Exception {
  2. String[] formats = new String[] { "XDI/JSON", "XDI DISPLAY", "KEYVALUE" };
  3. String[] fileExtensions = new String[] { "json", "xdi" };
  4. 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") };
  5. for (String format : formats) assertTrue(XDIWriterRegistry.forFormat(format, null).supportsFormat(format));
  6. for (String fileExtension : fileExtensions) assertTrue(XDIWriterRegistry.forFileExtension(fileExtension, null).supportsFileExtension(fileExtension));
  7. for (MimeType mimeType : mimeTypes) assertTrue(XDIWriterRegistry.forMimeType(mimeType).supportsMimeType(mimeType));
  8. }
  9. }

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

  1. private void writeGraph(ExecutionContext executionContext) throws Xdi2MessagingException {
  2. XDIWriter xdiWriter = XDIWriterRegistry.forFormat(this.mimeType, null);
  3. if (xdiWriter == null) throw new Xdi2MessagingException("Cannot write this format: " + this.mimeType, null, executionContext);
  4. Graph graph = this.getGraph();
  5. FileWriter writer = null;
  6. try {
  7. File file = new File(this.path);
  8. file.createNewFile();
  9. writer = new FileWriter(file);
  10. xdiWriter.write(graph, writer);
  11. writer.close();
  12. } catch (Exception ex) {
  13. throw new Xdi2MessagingException("Cannot write file: " + ex.getMessage(), ex, executionContext);
  14. } finally {
  15. if (writer != null) try { writer.close(); } catch (Exception ex) { }
  16. }
  17. graph.close();
  18. }

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

  1. /**
  2. * Returns a MimeType that we can support for this Accept header.
  3. * @param canRead If true, only return MimeTypes we can read.
  4. * @param canWrite If true, only return MimeTypes we can write.
  5. * @return A MimeType, or null if no appropriate implementation could be found.
  6. */
  7. public MimeType bestMimeType(boolean canRead, boolean canWrite) {
  8. List<MimeType> mimeTypes = this.getMimeTypesSortedByQuality();
  9. if ((! canRead) && (! canWrite)) return mimeTypes.get(0).mimeTypeWithoutQuality();
  10. for (MimeType mimeType : mimeTypes) {
  11. MimeType mimeTypeWithoutQuality = mimeType.mimeTypeWithoutQuality();
  12. MimeType mimeTypeWithoutParameters = mimeType.mimeTypeWithoutParameters();
  13. if (canRead && (! XDIReaderRegistry.supportsMimeType(mimeTypeWithoutParameters))) continue;
  14. if (canWrite && (! XDIWriterRegistry.supportsMimeType(mimeTypeWithoutParameters))) continue;
  15. return mimeTypeWithoutQuality;
  16. }
  17. return null;
  18. }

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

  1. @Override
  2. public WrapperStore openWrapper(String identifier) throws IOException {
  3. // initialize graph
  4. XDIReader xdiReader = XDIReaderRegistry.forMimeType(this.mimeType == null ? null : new MimeType(this.mimeType));
  5. XDIWriter xdiWriter = XDIWriterRegistry.forMimeType(this.mimeType == null ? null : new MimeType(this.mimeType));
  6. return new URLWrapperStore(this.url, this.mimeType, xdiReader, xdiWriter);
  7. }

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

  1. /**
  2. * Returns an XDIWriter for the specified file extension, e.g.
  3. * <ul>
  4. * <li>.json</li>
  5. * <li>.xdi</li>
  6. * </ul>
  7. * @param fileExtension The desired file extension.
  8. * @return An XDIWriter, or null if no appropriate implementation could be found.
  9. */
  10. public static XDIWriter forFileExtension(String fileExtension, Properties parameters) {
  11. if (fileExtension == null) return XDIWriterRegistry.getDefault();
  12. Class<? extends XDIWriter> writerClass = writerClassesByFileExtension.get(fileExtension);
  13. if (writerClass == null) return null;
  14. try {
  15. Constructor<? extends XDIWriter> constructor = writerClass.getConstructor(Properties.class);
  16. return constructor.newInstance(parameters);
  17. } catch (Exception ex) {
  18. throw new RuntimeException(ex);
  19. }
  20. }

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

  1. XDIWriter xdiWriter = XDIWriterRegistry.forFormat(format, xdiWriterParameters);
  2. StringWriter xdiStringWriter = new StringWriter();
  3. xdiWriter.write(graph, xdiStringWriter);
  4. XDIWriter xdiWriter = XDIWriterRegistry.forFormat(format, xdiWriterParameters);
  5. StringWriter xdiStringWriter = new StringWriter();
  6. xdiWriter.write(graph, xdiStringWriter);
  7. XDIWriter xdiWriter = XDIWriterRegistry.forFormat(format, xdiWriterParameters);
  8. StringWriter stringWriter = new StringWriter();
  9. xdiWriter.write(resultGraph, stringWriter);

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

  1. private void sendOk(HttpTransportRequest request, HttpTransportResponse response, TransportMessagingResponse messagingResponse) throws IOException {
  2. response.setStatus(HttpTransportResponse.SC_OK);
  3. Map<String, String> headers = new HashMap<String, String> ();
  4. headers.putAll(this.getHeaders());
  5. if (HttpTransportRequest.METHOD_GET.equals(request.getMethod())) headers.putAll(this.getHeadersGet());
  6. if (HttpTransportRequest.METHOD_POST.equals(request.getMethod())) headers.putAll(this.getHeadersPost());
  7. if (HttpTransportRequest.METHOD_PUT.equals(request.getMethod())) headers.putAll(this.getHeadersPut());
  8. if (HttpTransportRequest.METHOD_DELETE.equals(request.getMethod())) headers.putAll(this.getHeadersDelete());
  9. if (HttpTransportRequest.METHOD_OPTIONS.equals(request.getMethod())) headers.putAll(this.getHeadersOptions());
  10. for (Map.Entry<String, String> header : headers.entrySet()) {
  11. response.setHeader(header.getKey(), header.getValue());
  12. }
  13. if (messagingResponse != null) {
  14. // find a suitable writer based on accept headers
  15. if (log.isDebugEnabled()) log.debug("Accept: " + request.getHeader("Accept"));
  16. XDIWriter writer = null;
  17. String acceptHeader = request.getHeader("Accept");
  18. MimeType sendMimeType = acceptHeader != null ? AcceptHeader.parse(acceptHeader).bestMimeType(false, true) : null;
  19. writer = sendMimeType != null ? XDIWriterRegistry.forMimeType(sendMimeType) : null;
  20. if (writer == null) writer = XDIWriterRegistry.getDefault();
  21. // send out the message result
  22. if (log.isDebugEnabled()) log.debug("Sending result in " + sendMimeType + " with writer " + writer.getClass().getSimpleName() + ".");
  23. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  24. writer.write(messagingResponse.getGraph(), buffer);
  25. response.setContentType(writer.getMimeType().toString());
  26. response.setContentLength(buffer.size());
  27. response.writeBody(buffer.toByteArray(), true);
  28. }
  29. if (log.isDebugEnabled()) log.debug("Output complete.");
  30. }

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

  1. @Override
  2. public WrapperStore openWrapper(String identifier) throws IOException {
  3. // initialize graph
  4. XDIReader xdiReader = XDIReaderRegistry.forMimeType(this.mimeType == null ? null : new MimeType(this.mimeType));
  5. XDIWriter xdiWriter = XDIWriterRegistry.forMimeType(this.mimeType == null ? null : new MimeType(this.mimeType));
  6. return new ClasspathWrapperStore(this.classpath, this.mimeType, xdiReader, xdiWriter);
  7. }

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

  1. /**
  2. * Returns an XDIWriter for the specified mime type, e.g.
  3. * <ul>
  4. * <li>application/xdi+json</li>
  5. * <li>text/xdi</li>
  6. * </ul>
  7. * @param mimeType The desired mime type.
  8. * @return An XDIWriter, or null if no appropriate implementation could be found.
  9. */
  10. public static XDIWriter forMimeType(MimeType mimeType) {
  11. if (mimeType == null) return XDIWriterRegistry.getDefault();
  12. Class<? extends XDIWriter> writerClass = writerClassesByMimeType.get(mimeType.mimeTypeWithoutParameters());
  13. if (writerClass == null) return null;
  14. try {
  15. Constructor<? extends XDIWriter> constructor = writerClass.getConstructor(Properties.class);
  16. return constructor.newInstance(mimeType.getParameters());
  17. } catch (Exception ex) {
  18. throw new RuntimeException(ex);
  19. }
  20. }

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

  1. public void testReadWriteFormats() throws Exception {
  2. String[] formats = new String[] { "XDI/JSON", "XDI DISPLAY" };
  3. for (int i=0; i<formats.length; i++) {
  4. File file = new File("xdi.out");
  5. Graph graph4 = this.getGraphFactory().openGraph(this.getClass().getName() + "-graph-4" + "-" + i);
  6. Graph graph5 = this.getGraphFactory().openGraph(this.getClass().getName() + "-graph-5" + "-" + i);
  7. XDIWriter writer = XDIWriterRegistry.forFormat(formats[i], null);
  8. XDIReader reader = XDIReaderRegistry.forFormat(formats[i], null);
  9. FileWriter fileWriter = new FileWriter(file);
  10. FileReader fileReader = new FileReader(file);
  11. makeGraph(graph4);
  12. writer.write(graph4, fileWriter);
  13. reader.read(graph5, fileReader);
  14. fileWriter.close();
  15. fileReader.close();
  16. testGraph(graph5);
  17. testGraphsEqual(graph4, graph5);
  18. graph4.close();
  19. graph5.close();
  20. file.delete();
  21. }
  22. }

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

  1. @Override
  2. public WrapperStore openWrapper(String identifier) throws IOException {
  3. // check identifier
  4. String path = this.getPath();
  5. if (path == null) {
  6. path = FILE_PREFIX + identifier + FILE_SUFFIX;
  7. }
  8. // initialize graph
  9. XDIReader xdiReader = XDIReaderRegistry.forMimeType(this.mimeType == null ? null : new MimeType(this.mimeType));
  10. XDIWriter xdiWriter = XDIWriterRegistry.forMimeType(this.mimeType == null ? null : new MimeType(this.mimeType));
  11. return new FileWrapperStore(path, this.mimeType, xdiReader, xdiWriter);
  12. }

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

  1. @Override
  2. public InterceptorResult before(MessageEnvelope messageEnvelope, ExecutionContext executionContext, ExecutionResult executionResult) throws Xdi2MessagingException {
  3. if (this.isEnableMessageEnvelope()) {
  4. StringWriter stringWriter = new StringWriter();
  5. try {
  6. XDIWriterRegistry.getDefault().write(messageEnvelope.getGraph(), stringWriter);
  7. } catch (IOException ex) {
  8. throw new Xdi2MessagingException("Cannot write graph: " + ex.getMessage(), ex, executionContext);
  9. }
  10. String param = stringWriter.getBuffer().toString();
  11. this.executeCommandLine(param, executionContext);
  12. }
  13. return InterceptorResult.DEFAULT;
  14. }

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

  1. XDIWriterRegistry.forFormat("XDI/JSON", null).write(graph10, buffer1);
  2. XDIWriterRegistry.forFormat("XDI DISPLAY", null).write(graph11, buffer2);
  3. graph10.clear();
  4. graph11.clear();

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

  1. XDIWriter writer = XDIWriterRegistry.forMimeType(sendMimeType);
  2. writer = XDIWriterRegistry.forMimeType(sendMimeType);

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

  1. private void readGraph(ExecutionContext executionContext) throws Xdi2MessagingException {
  2. XDIReader xdiReader = XDIReaderRegistry.forFormat(this.mimeType, null);
  3. if (xdiReader == null) throw new Xdi2MessagingException("Cannot read this format: " + this.mimeType, null, executionContext);
  4. Graph graph = this.getGraph();
  5. graph.clear();
  6. FileReader reader = null;
  7. try {
  8. File file = new File(this.path);
  9. reader = new FileReader(file);
  10. xdiReader.read(graph, reader);
  11. reader.close();
  12. } catch (FileNotFoundException ex) {
  13. } catch (Exception ex) {
  14. throw new Xdi2MessagingException("Cannot read file: " + ex.getMessage(), ex, executionContext);
  15. } finally {
  16. if (reader != null) {
  17. try {
  18. reader.close();
  19. } catch (Exception ex) { }
  20. }
  21. }
  22. if (xdiReader instanceof AutoReader) this.mimeType = ((AutoReader) xdiReader).getLastSuccessfulReader().getFormat();
  23. if (this.mimeType == null) this.mimeType = XDIWriterRegistry.getDefault().getFormat();
  24. }

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

  1. XDIWriter writer = XDIWriterRegistry.forMimeType(sendMimeType);
  2. writer = XDIWriterRegistry.forMimeType(sendMimeType);

相关文章