Java Code Examples for com.thoughtworks.xstream.core.util.QuickWriter

The following examples show how to use com.thoughtworks.xstream.core.util.QuickWriter. 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: jeewx-boot   Source File: MessageUtil.java    License: Apache License 2.0 6 votes vote down vote up
public HierarchicalStreamWriter createWriter(Writer out) {
    return new PrettyPrintWriter(out) {
        // 对所有xml节点的转换都增加CDATA标记
        boolean cdata = true;

        @SuppressWarnings("unchecked")
        public void startNode(String name, Class clazz) {
            super.startNode(name, clazz);
        }

        protected void writeText(QuickWriter writer, String text) {
            if (cdata) {
                    writer.write("<![CDATA[");
                    writer.write(text);
                    writer.write("]]>");
            } else {
                    writer.write(text);
            }
        }
    };
}
 
Example 2
Source Project: WeChatEnterprise   Source File: WXMessageUtil.java    License: Apache License 2.0 6 votes vote down vote up
public HierarchicalStreamWriter createWriter(Writer out) {
	return new PrettyPrintWriter(out) {
		// 对所有xml节点的转换都增加CDATA标记
		boolean cdata = true;

		public void startNode(String name, Class clazz) {
			super.startNode(name, clazz);
		}

		protected void writeText(QuickWriter writer, String text) {
			if (cdata) {
				writer.write("<![CDATA[");
				writer.write(text);
				writer.write("]]>");
			} else {
				writer.write(text);
			}
		}
	};
}
 
Example 3
public HierarchicalStreamWriter createWriter(Writer out) {
	return new PrettyPrintWriter(out) {
		boolean CDATA = true;
		
		@SuppressWarnings("rawtypes")
		public void startNode(String name, Class clazz) {
			super.startNode(name, clazz);
		}
		protected void writeText(QuickWriter writer, String text) {
			if (CDATA) {
				writer.write("<![CDATA[");
				writer.write(text);
				writer.write("]]>");
			} else {
				writer.write(text);
			}
		}
	};
}
 
Example 4
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 5
Source Project: jeewx   Source File: MessageUtil.java    License: Apache License 2.0 6 votes vote down vote up
public HierarchicalStreamWriter createWriter(Writer out) {
    return new PrettyPrintWriter(out) {
        // 对所有xml节点的转换都增加CDATA标记
        boolean cdata = true;

        @SuppressWarnings("unchecked")
        public void startNode(String name, Class clazz) {
            super.startNode(name, clazz);
        }

        protected void writeText(QuickWriter writer, String text) {
            if (cdata) {
                    writer.write("<![CDATA[");
                    writer.write(text);
                    writer.write("]]>");
            } else {
                    writer.write(text);
            }
        }
    };
}
 
Example 6
Source Project: jeewx   Source File: MessageUtil.java    License: Apache License 2.0 6 votes vote down vote up
public HierarchicalStreamWriter createWriter(Writer out) {
    return new PrettyPrintWriter(out) {
        // 对所有xml节点的转换都增加CDATA标记
        boolean cdata = true;

        @SuppressWarnings("unchecked")
        public void startNode(String name, Class clazz) {
            super.startNode(name, clazz);
        }

        protected void writeText(QuickWriter writer, String text) {
            if (cdata) {
                    writer.write("<![CDATA[");
                    writer.write(text);
                    writer.write("]]>");
            } else {
                    writer.write(text);
            }
        }
    };
}
 
Example 7
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 8
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 9
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 10
Source Project: javalite   Source File: CDATAXppDriver.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public HierarchicalStreamWriter createWriter(Writer out) {
    return new PrettyPrintWriter(out) {
        boolean cdata;
        @Override
        public void startNode(String name, Class clazz) {
            super.startNode(name, clazz);
            cdata = CharSequence.class.isAssignableFrom(clazz);
        }
        @Override
        protected void writeText(QuickWriter writer, String text) {
            if(cdata) {
                writer.write("<![CDATA[");
                writer.write(text);
                writer.write("]]>");
            } else {
                writer.write(text);
            }
        }
    };
}
 
Example 11
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 12
Source Project: lams   Source File: PrettyPrintWriter.java    License: GNU General Public License v2.0 5 votes vote down vote up
private PrettyPrintWriter(
    Writer writer, int mode, char[] lineIndenter, NameCoder nameCoder,
    String newLine) {
    super(nameCoder);
    this.writer = new QuickWriter(writer);
    this.lineIndenter = lineIndenter;
    this.newLine = newLine;
    this.mode = mode;
    if (mode < XML_QUIRKS || mode > XML_1_1) {
        throw new IllegalArgumentException("Not a valid XML mode");
    }
}
 
Example 13
Source Project: weChatRobot   Source File: XmlUtil.java    License: MIT License 5 votes vote down vote up
@Override
public HierarchicalStreamWriter createWriter(Writer out) {
    return new PrettyPrintWriter(out) {
        @Override
        protected void writeText(QuickWriter writer, String text) {
            //对所有xml节点的转换都增加CDATA标记
            writer.write("<![CDATA[");
            writer.write(text);
            writer.write("]]>");
        }
    };
}
 
Example 14
Source Project: gatf   Source File: GatfPrettyPrintWriter.java    License: Apache License 2.0 5 votes vote down vote up
private GatfPrettyPrintWriter(
    Writer writer, int mode, char[] lineIndenter, NameCoder nameCoder,
    String newLine, String[] cdataNodes) {
    super(nameCoder);
    this.writer = new QuickWriter(writer);
    this.lineIndenter = lineIndenter;
    this.newLine = newLine;
    this.mode = mode;
    this.cdataNodes = cdataNodes;
    if (mode < XML_QUIRKS || mode > XML_1_1) {
        throw new IllegalArgumentException("Not a valid XML mode");
    }
}
 
Example 15
Source Project: gatf   Source File: GatfPrettyPrintWriter.java    License: Apache License 2.0 5 votes vote down vote up
protected void writeText(QuickWriter writer, String text) {
  	if(StringUtils.isBlank(text))
  		return;
  	if(cdata) {
	writer.write("<![CDATA[");
	writer.write(text);
	writer.write("]]>");
} else {
	writeText(text, false);
}
  }
 
Example 16
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 17
Source Project: engage-api-client   Source File: XmlApiPrintWriter.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void writeText(QuickWriter writer, String text) {
	if(hasSpecialCharacters(text)) {
		writer.write(String.format("%s%s%s", CDATA_START, text, CDATA_END));
	} else {
		super.writeText(writer, text);
	}
}
 
Example 18
Source Project: lams   Source File: PrettyPrintWriter.java    License: GNU General Public License v2.0 4 votes vote down vote up
protected void writeAttributeValue(QuickWriter writer, String text) {
    writeText(text, true);
}
 
Example 19
Source Project: lams   Source File: PrettyPrintWriter.java    License: GNU General Public License v2.0 4 votes vote down vote up
protected void writeText(QuickWriter writer, String text) {
    writeText(text, false);
}
 
Example 20
Source Project: gatf   Source File: GatfPrettyPrintWriter.java    License: Apache License 2.0 4 votes vote down vote up
protected void writeAttributeValue(QuickWriter writer, String text) {
    writeText(text, true);
}
 
Example 21
Source Project: lams   Source File: JsonWriter.java    License: GNU General Public License v2.0 3 votes vote down vote up
/**
 * Create a JsonWriter.
 * 
 * @param writer the {@link Writer} where the JSON is written to
 * @param mode the JsonWriter mode
 * @param format the JSON format definition
 * @param bufferSize the buffer size of the internally used QuickWriter
 * @see JsonWriter#JsonWriter(Writer, int, Format)
 * @since 1.4
 */
public JsonWriter(Writer writer, int mode, Format format, int bufferSize) {
    super(mode, format.getNameCoder());
    this.writer = new QuickWriter(writer, bufferSize);
    this.format = format;
    depth = (mode & DROP_ROOT_MODE) == 0 ? -1 : 0;
}