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

The following examples show how to use com.thoughtworks.xstream.io.xml.XppDriver. These examples are extracted from open source projects. 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 Project: wechat-core   Source File: MessageUtils.java    License: 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 2
Source Project: openmeetings   Source File: LibraryChartLoader.java    License: 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 3
Source Project: openmeetings   Source File: WbConverter.java    License: 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 4
Source Project: mamute   Source File: QuestionRssEntryFactory.java    License: 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 5
Source Project: olat   Source File: GlossaryItemManager.java    License: 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 6
Source Project: olat   Source File: GlossaryItemManager.java    License: 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 7
Source Project: weixin-java-tools   Source File: XStreamInitializer.java    License: 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 8
Source Project: cuba   Source File: CubaXStream.java    License: 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 9
Source Project: weixin-java-tools   Source File: XStreamInitializer.java    License: 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 10
Source Project: engage-api-client   Source File: XStreamFactory.java    License: 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 11
Source Project: spring-analysis-note   Source File: XStreamMarshaller.java    License: MIT License 4 votes vote down vote up
private HierarchicalStreamDriver getDefaultDriver() {
	if (this.defaultDriver == null) {
		this.defaultDriver = new XppDriver();
	}
	return this.defaultDriver;
}
 
Example 12
Source Project: java-technology-stack   Source File: XStreamMarshaller.java    License: 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 Project: spring4-understanding   Source File: XStreamMarshaller.java    License: 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 14
Source Project: aaden-pay   Source File: XmlBeanJsonConverUtil.java    License: 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 Project: aaden-pay   Source File: XmlBeanJsonConverUtil.java    License: 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);
}
 
Example 16
Source Project: lams   Source File: XStream.java    License: 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 17
Source Project: lams   Source File: XStream.java    License: 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 Project: lams   Source File: XStreamer.java    License: 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 19
Source Project: lams   Source File: XStreamer.java    License: 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);
}