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

The following examples show how to use com.thoughtworks.xstream.core.util.QuickWriter. 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: MessageUtil.java    From jeewx with 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 File: CDATAXppDriver.java    From javalite with 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 #3
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 #4
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 #5
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 #6
Source File: MessageUtil.java    From jeewx-boot with 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 File: MessageUtil.java    From jeewx with 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 #8
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 #9
Source File: MsgXmlUtil.java    From AlipayWechatPlatform with GNU General Public License v3.0 6 votes vote down vote up
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 #10
Source File: WXMessageUtil.java    From WeChatEnterprise with 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 #11
Source File: PrettyPrintWriter.java    From lams with 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 #12
Source File: XmlUtil.java    From weChatRobot with 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 #13
Source File: GatfPrettyPrintWriter.java    From gatf with 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 #14
Source File: GatfPrettyPrintWriter.java    From gatf with 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 #15
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 #16
Source File: XmlApiPrintWriter.java    From engage-api-client with 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 #17
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 #18
Source File: PrettyPrintWriter.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
protected void writeText(QuickWriter writer, String text) {
    writeText(text, false);
}
 
Example #19
Source File: PrettyPrintWriter.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
protected void writeAttributeValue(QuickWriter writer, String text) {
    writeText(text, true);
}
 
Example #20
Source File: GatfPrettyPrintWriter.java    From gatf with Apache License 2.0 4 votes vote down vote up
protected void writeAttributeValue(QuickWriter writer, String text) {
    writeText(text, true);
}
 
Example #21
Source File: JsonWriter.java    From lams with 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;
}