com.thoughtworks.xstream.io.xml.XppDriver Java Examples

The following examples show how to use com.thoughtworks.xstream.io.xml.XppDriver. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GlossaryItemManager.java    From olat with Apache License 2.0 6 votes vote down vote up
/**
 * writes glossary to xml-file prepend doc-book dtd: <!DOCTYPE glossary PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
 * "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd">
 * 
 * @param glossaryFile
 * @param glossaryItemArr
 */
private void saveToFile(VFSLeaf glossaryFile, ArrayList<GlossaryItem> glossaryItemArr) {
    // cdata-tags should be used instead of strings, overwrite writer.
    XStream xstream = new XStream(new XppDriver() {
        @Override
        public HierarchicalStreamWriter createWriter(Writer out) {
            return new PrettyPrintWriter(out) {
                @Override
                protected void writeText(QuickWriter writer, String text) {
                    if (text.contains("<") || text.contains(">") || text.contains("&")) {
                        writer.write("<![CDATA[");
                        writer.write(text);
                        writer.write("]]>");
                    } else {
                        writer.write(text);
                    }
                }
            };
        }
    });

    xstream.alias(XML_GLOSSARY_ITEM_NAME, GlossaryItem.class);
    glossaryItemArr = removeEmptyGlossaryItems(glossaryItemArr);
    XStreamHelper.writeObject(xstream, glossaryFile, glossaryItemArr);
}
 
Example #2
Source File: GlossaryItemManager.java    From olat with Apache License 2.0 6 votes vote down vote up
/**
 * writes glossary to xml-file prepend doc-book dtd: <!DOCTYPE glossary PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
 * "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd">
 * 
 * @param glossaryFile
 * @param glossaryItemArr
 */
private void saveToFile(VFSLeaf glossaryFile, ArrayList<GlossaryItem> glossaryItemArr) {
    // cdata-tags should be used instead of strings, overwrite writer.
    XStream xstream = new XStream(new XppDriver() {
        @Override
        public HierarchicalStreamWriter createWriter(Writer out) {
            return new PrettyPrintWriter(out) {
                @Override
                protected void writeText(QuickWriter writer, String text) {
                    if (text.contains("<") || text.contains(">") || text.contains("&")) {
                        writer.write("<![CDATA[");
                        writer.write(text);
                        writer.write("]]>");
                    } else {
                        writer.write(text);
                    }
                }
            };
        }
    });

    xstream.alias(XML_GLOSSARY_ITEM_NAME, GlossaryItem.class);
    glossaryItemArr = removeEmptyGlossaryItems(glossaryItemArr);
    XStreamHelper.writeObject(xstream, glossaryFile, glossaryItemArr);
}
 
Example #3
Source File: QuestionRssEntryFactory.java    From mamute with Apache License 2.0 6 votes vote down vote up
private XStream buildXstream() {
	return new XStream(new XppDriver() {
		public HierarchicalStreamWriter createWriter(Writer out) {
			return new PrettyPrintWriter(out) {
				List<String> cdataFields = asList("title", "author");
				boolean cdata = false;
				
				public void startNode(String name, Class clazz) {
					super.startNode(name, clazz);
					cdata = cdataFields.contains(name);
				}
				protected void writeText(QuickWriter writer, String text) {
					if (cdata) {
						writer.write("<![CDATA[");
						writer.write(text);
						writer.write("]]>");
					} else {
						writer.write(text);
					}
				}
			};
		}
	});
}
 
Example #4
Source File: WbConverter.java    From openmeetings with Apache License 2.0 6 votes vote down vote up
public static List<?> loadWmlFile(String hash) {
	String name = OmFileHelper.getName(hash, EXTENSION_WML);
	File file = new File(OmFileHelper.getUploadWmlDir(), name);
	log.debug("filepathComplete: {}", file);

	XStream xstream = new XStream(new XppDriver());
	xstream.setMode(XStream.NO_REFERENCES);
	xstream.addPermission(NoTypePermission.NONE);
	xstream.addPermission(NullPermission.NULL);
	xstream.addPermission(PrimitiveTypePermission.PRIMITIVES);
	xstream.allowTypeHierarchy(List.class);
	xstream.allowTypeHierarchy(String.class);
	xstream.ignoreUnknownElements();
	try (InputStream is = new FileInputStream(file); BufferedReader reader = new BufferedReader(new InputStreamReader(is, UTF_8))) {
		return (List<?>) xstream.fromXML(reader);
	} catch (Exception err) {
		log.error("loadWmlFile", err);
	}
	return new ArrayList<>();
}
 
Example #5
Source File: LibraryChartLoader.java    From openmeetings with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("rawtypes")
public static List loadChart(File dir, String fileName) {
	try {
		File file = new File(dir, fileName + CHART_EXT);

		log.error("filepathComplete: {}", file);

		XStream xStream = new XStream(new XppDriver());
		xStream.setMode(XStream.NO_REFERENCES);

		try (InputStream is = new FileInputStream(file);
				BufferedReader reader = new BufferedReader(new InputStreamReader(is, UTF_8)))
		{
			return (List) xStream.fromXML(reader);
		}
	} catch (Exception err) {
		log.error("Unexpected error while loading chart", err);
	}
	return new ArrayList<>();
}
 
Example #6
Source File: MessageUtils.java    From wechat-core with Apache License 2.0 6 votes vote down vote up
/**
 * 扩展xstream,使其支持CDATA块
 */
private static XStream newXStreamInstance() {
    return new XStream(new XppDriver() {
        @Override
        public HierarchicalStreamWriter createWriter(Writer out) {
            return new PrettyPrintWriter(out) {
                // 对所有xml节点的转换都增加CDATA标记
                boolean cdata = true;

                @Override
                protected void writeText(QuickWriter writer, String text) {
                    if (this.cdata) {
                        writer.write("<![CDATA[");
                        writer.write(text);
                        writer.write("]]>");
                    } else {
                        writer.write(text);
                    }
                }
            };
        }
    });
}
 
Example #7
Source File: XStreamFactory.java    From engage-api-client with Apache License 2.0 5 votes vote down vote up
public XStream createXStream() {
	return new XStream(
		new XppDriver() {
			@Override
			public HierarchicalStreamWriter createWriter(Writer out) {
                   return new XmlApiPrintWriter(out, new XmlFriendlyNameCoder("__", "_"));
			}
		});
}
 
Example #8
Source File: XStreamInitializer.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
public static XStream getInstance() {
  XStream xstream = new XStream(new XppDriver() {

    @Override
    public HierarchicalStreamWriter createWriter(Writer out) {
      return new PrettyPrintWriter(out, getNameCoder()) {
        protected String PREFIX_CDATA = "<![CDATA[";
        protected String SUFFIX_CDATA = "]]>";
        protected String PREFIX_MEDIA_ID = "<MediaId>";
        protected String SUFFIX_MEDIA_ID = "</MediaId>";
        @Override
        protected void writeText(QuickWriter writer, String text) {
          if (text.startsWith(PREFIX_CDATA) && text.endsWith(SUFFIX_CDATA)) {
            writer.write(text);
          } else if (text.startsWith(PREFIX_MEDIA_ID) && text.endsWith(SUFFIX_MEDIA_ID)) {
            writer.write(text);
          } else {
            super.writeText(writer, text);
          }

        }
      };
    }
  });
  xstream.ignoreUnknownElements();
  xstream.setMode(XStream.NO_REFERENCES);
  xstream.addPermission(NullPermission.NULL);
  xstream.addPermission(PrimitiveTypePermission.PRIMITIVES);
  return xstream;
}
 
Example #9
Source File: CubaXStream.java    From cuba with Apache License 2.0 5 votes vote down vote up
public CubaXStream(List<Class> excluded) {
    this(null,
            new XppDriver(),
            new ClassLoaderReference(Thread.currentThread().getContextClassLoader()),
            null,
            new CubaXStreamConverterLookup(excluded));
}
 
Example #10
Source File: XStreamInitializer.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
public static XStream getInstance() {
  XStream xstream = new XStream(new PureJavaReflectionProvider(), new XppDriver() {

    @Override
    public HierarchicalStreamWriter createWriter(Writer out) {
      return new PrettyPrintWriter(out, getNameCoder()) {
        protected String PREFIX_CDATA = "<![CDATA[";
        protected String SUFFIX_CDATA = "]]>";
        protected String PREFIX_MEDIA_ID = "<MediaId>";
        protected String SUFFIX_MEDIA_ID = "</MediaId>";

        @Override
        protected void writeText(QuickWriter writer, String text) {
          if (text.startsWith(this.PREFIX_CDATA) && text.endsWith(this.SUFFIX_CDATA)) {
            writer.write(text);
          } else if (text.startsWith(this.PREFIX_MEDIA_ID) && text.endsWith(this.SUFFIX_MEDIA_ID)) {
            writer.write(text);
          } else {
            super.writeText(writer, text);
          }

        }

        @Override
        public String encodeNode(String name) {
          //防止将_转换成__
          return name;
        }
      };
    }
  });

  xstream.ignoreUnknownElements();
  xstream.setMode(XStream.NO_REFERENCES);
  xstream.addPermission(NullPermission.NULL);
  xstream.addPermission(PrimitiveTypePermission.PRIMITIVES);
  xstream.setClassLoader(Thread.currentThread().getContextClassLoader());
  return xstream;
}
 
Example #11
Source File: XStreamMarshaller.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private HierarchicalStreamDriver getDefaultDriver() {
	if (this.defaultDriver == null) {
		this.defaultDriver = new XppDriver();
	}
	return this.defaultDriver;
}
 
Example #12
Source File: XStreamMarshaller.java    From java-technology-stack with MIT License 4 votes vote down vote up
private HierarchicalStreamDriver getDefaultDriver() {
	if (this.defaultDriver == null) {
		this.defaultDriver = new XppDriver();
	}
	return this.defaultDriver;
}
 
Example #13
Source File: XStreamMarshaller.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private HierarchicalStreamDriver getDefaultDriver() {
	if (this.defaultDriver == null) {
		this.defaultDriver = new XppDriver();
	}
	return this.defaultDriver;
}
 
Example #14
Source File: XmlBeanJsonConverUtil.java    From aaden-pay with Apache License 2.0 3 votes vote down vote up
/**
 * 把对象转成XML
 *
 * @param <T>
 * @param obj
 *            要转换的对象
 * @param clss
 *            是转换对象的所有对象的的LIST列表如aa.class
 * @return
 */
public static <T> String beanToXML(Object obj, List<Class<T>> clss) {
	XStream xstream = new XStream(new XppDriver(new NoNameCoder()));
	for (Class<T> cls : clss) {
		xstream.processAnnotations(cls);
	}
	return xmlHeader + xstream.toXML(obj);
}
 
Example #15
Source File: XStreamer.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Deserialize a self-contained XStream with object from an XML Reader. The method will use
 * internally an XppDriver to load the contained XStream instance.
 * 
 * @param xml the {@link Reader} providing the XML data
 * @param permissions the permissions to use (ensure that they include the defaults)
 * @throws IOException if an error occurs reading from the Reader.
 * @throws ClassNotFoundException if a class in the XML stream cannot be found
 * @throws com.thoughtworks.xstream.XStreamException if the object cannot be deserialized
 * @since 1.4.7
 * @see #toXML(XStream, Object, Writer)
 */
public Object fromXML(final Reader xml, final TypePermission[] permissions)
        throws IOException, ClassNotFoundException {
    return fromXML(new XppDriver(), xml, permissions);
}
 
Example #16
Source File: XStreamer.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Deserialize a self-contained XStream with object from an XML Reader. The method will use
 * internally an XppDriver to load the contained XStream instance with default permissions.
 * 
 * @param xml the {@link Reader} providing the XML data
 * @throws IOException if an error occurs reading from the Reader.
 * @throws ClassNotFoundException if a class in the XML stream cannot be found
 * @throws com.thoughtworks.xstream.XStreamException if the object cannot be deserialized
 * @since 1.2
 * @see #toXML(XStream, Object, Writer)
 */
public Object fromXML(final Reader xml)
        throws IOException, ClassNotFoundException {
    return fromXML(new XppDriver(), xml);
}
 
Example #17
Source File: XStream.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Constructs an XStream with a special {@link ReflectionProvider}.
 * <p>
 * The instance will use the {@link XppDriver} as default.
 * </p>
 * 
 * @param reflectionProvider the reflection provider to use or <em>null</em> for best
 *            matching reflection provider
 * @throws InitializationException in case of an initialization problem
 */
public XStream(ReflectionProvider reflectionProvider) {
    this(reflectionProvider, (Mapper)null, new XppDriver());
}
 
Example #18
Source File: XStream.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Constructs a default XStream.
 * <p>
 * The instance will use the {@link XppDriver} as default and tries to determine the best
 * match for the {@link ReflectionProvider} on its own.
 * </p>
 *
 * @throws InitializationException in case of an initialization problem
 */
public XStream() {
    this(null, (Mapper)null, new XppDriver());
}
 
Example #19
Source File: XmlBeanJsonConverUtil.java    From aaden-pay with Apache License 2.0 2 votes vote down vote up
/**
 * 把对象转成XML
 *
 * @param obj
 *            要转换的对象
 * @param rootAlias
 *            要转换的对象的根节点别名,如果为空,默认是类的名字
 * @return
 */
public static String beanToXML(Object obj, String rootAlias) {
	XStream xstream = new XStream(new XppDriver(new NoNameCoder()));
	xstream.processAnnotations(obj.getClass());
	return xmlHeader + xstream.toXML(obj);
}