📄 documentserializableutilities.java
字号:
* @param element The Parant Element * @param tagName The Tag Name of the Child Element that will contain the value * @param defaultValue The return value if there is no Child Element with that Tag Name * @return the value converted from a String **/ public static long getLong(Element element, String tagName, long defaultValue) { Element childElement = getChildElement(element, tagName); if (childElement != null) { return getLong(childElement); } else { return defaultValue; } } /** * Add an Element with the specified tagname and value (converted to a String) * * @param element Parent Element that the new element will be added to * @param tagName TagName to be used for the created Child Element * @param value The value that will be stored in the Element as a String **/ public static void addDouble(Element element, String tagName, double value) { StructuredDocument structuredDocument = element.getRoot(); Element childElement = structuredDocument.createElement(tagName, Double.toString(value)); element.appendChild(childElement); } /** * Get the value of an element converted from a String * * @param element Element that contains the value * @return the value converted from a String **/ public static double getDouble(Element element) { return Double.parseDouble((String) element.getValue()); } /** * Get the value of a Child Element * * @param element The Parant Element * @param tagName The Tag Name of the Child Element that will contain the value * @param defaultValue The return value if there is no Child Element with that Tag Name * @return the value converted from a String **/ public static double getDouble(Element element, String tagName, double defaultValue) { Element childElement = getChildElement(element, tagName); if (childElement != null) { return getDouble(childElement); } else { return defaultValue; } } /** * Add an Element with the specified tagname and value (converted to a String) * * @param element Parent Element that the new element will be added to * @param tagName TagName to be used for the created Child Element * @param value The value that will be stored in the Element as a String **/ public static void addBoolean(Element element, String tagName, boolean value) { StructuredDocument structuredDocument = element.getRoot(); Element childElement = structuredDocument.createElement(tagName, value ? "true" : "false"); element.appendChild(childElement); } /** * Get the value of an element converted from a String ("true" or "false") * * @param element Element that contains the value * @return the value converted from a String **/ public static boolean getBoolean(Element element) { return "true".equals((String) element.getValue()); } /** * Get the value of a Child Element * * @param element The Parant Element * @param tagName The Tag Name of the Child Element that will contain the value * @param defaultValue The return value if there is no Child Element with that Tag Name * @return the value converted from a String **/ public static boolean getBoolean(Element element, String tagName, boolean defaultValue) { Element childElement = getChildElement(element, tagName); if (childElement != null) { return getBoolean(childElement); } else { return defaultValue; } } /** * Add an Element with the specified tagname and value * * @param element Parent Element that the new element will be added to * @param tagName TagName to be used for the created Child Element * @param value The value that will be stored in the Element **/ public static void addString(Element element, String tagName, String value) { StructuredDocument structuredDocument = element.getRoot(); Element childElement = structuredDocument.createElement(tagName, value); element.appendChild(childElement); } /** * Get the value of an element as a String * * @param element Element that contains the value * @return the value converted from a String **/ public static String getString(Element element) { return (String) element.getValue(); } /** * Get the value of a Child Element * * @param element The Parant Element * @param tagName The Tag Name of the Child Element that will contain the value * @param defaultValue The return value if there is no Child Element with that Tag Name * @return The value found in the Element **/ public static String getString(Element element, String tagName, String defaultValue) { Element childElement = getChildElement(element, tagName); if (childElement != null) { return getString(childElement); } else { return defaultValue; } } /** * Convert a DocumentSerializable object to its XML representation as a String * * The Root TagName will be 'documentSerializable' by default * * @param documentSerializable The Object to be converted to an XML Document * @return The String representation of an XML Document * @throws DocumentSerializationException if Unable to serialize object. **/ public static String toXmlString(DocumentSerializable documentSerializable) throws DocumentSerializationException { return toXmlString(documentSerializable, "documentSerializable"); } /** * Convert a DocumentSerializable object to its XML representation as a String * * The Root TagName will be 'documentSerializable' by default * * @param documentSerializable The Object to be converted to an XML Document * @param rootTagName The Root tagName for the XML Document * @return The String representation of an XML Document * @throws DocumentSerializationException if Unable to serialize object. **/ public static String toXmlString(DocumentSerializable documentSerializable, String rootTagName) throws DocumentSerializationException { try { StringWriter bout = new StringWriter(); XMLDocument document = DocumentSerializableUtilities.createStructuredXmlDocument(rootTagName, documentSerializable); document.sendToWriter(bout); bout.close(); return bout.toString(); } catch (IOException e) { throw new DocumentSerializationException("Error converting to String", e); } } /** * Write a DocumentSerializable object as an XML Document to a Stream * * The Root TagName will be 'documentSerializable' by default * * @param out The Stream to write the document to * @param documentSerializable The Object to be converted to an XML Document * @throws DocumentSerializationException if Unable to serialize object. * @throws IOException if I/O error while writing **/ public static void writeAsXmlString(OutputStream out, DocumentSerializable documentSerializable) throws IOException, DocumentSerializationException { writeAsXmlString(out, documentSerializable, "documentSerializable"); } /** * Write a DocumentSerializable object as an XML Document to a Stream * * The Root TagName will be 'documentSerializable' by default * * @param out The Stream to write the document to * @param rootTagName The Root tagName for the XML Document * @param documentSerializable The Object to be converted to an XML Document * @throws DocumentSerializationException if Unable to serialize object. * @throws IOException if I/O error while writing **/ public static void writeAsXmlString(OutputStream out, DocumentSerializable documentSerializable, String rootTagName) throws IOException, DocumentSerializationException { StructuredDocument document = DocumentSerializableUtilities.createStructuredXmlDocument(rootTagName, documentSerializable); document.sendToStream(out); } /** * Write a DocumentSerializable object as an XML Document to StdErr * * The Root TagName will be 'documentSerializable' by default * * @param documentSerializable The DocumentSerializable to be printed. **/ public static void printAsXmlString(DocumentSerializable documentSerializable) { try { if (documentSerializable == null) { System.err.println("<null DocumentSerializable>"); } else { writeAsXmlString(System.err, documentSerializable); } } catch (Exception e) { System.err.println("<Error converting DocumentSerializable to XML doc: " + e); } } /** * Create a DocumentSerializable Object from an XML Document * * @param buf The XML document contained in a String * @param clazz The Class of the resurrected object (must implement DocumentSerializable and have a public no-arg constructor) * @return An object of type 'clazz' * @throws DocumentSerializationException if Unable to parse object. **/ public static DocumentSerializable getDocumentSerializableFromXml(String buf, Class clazz) throws DocumentSerializationException { try { XMLDocument xmlDoc = (XMLDocument) StructuredDocumentFactory.newStructuredDocument(MimeMediaType.XMLUTF8 , new StringReader(buf)); return getDocumentSerializable(xmlDoc.getRoot(), clazz); } catch (IOException readErr) { throw new DocumentSerializationException("Unable to read the document", readErr); } catch (JxtaException e) { throw new DocumentSerializationException("Unable to get the document", e); } } /** * Create a DocumentSerializable Object from an XML Document * * @param buf The XML document contained in a byte buffer * @param clazz The Class of the resurrected object (must implement DocumentSerializable and have a public no-arg constructor) * @return An object of type 'clazz' * @throws DocumentSerializationException if Unable to parse object. **/ public static DocumentSerializable getDocumentSerializableFromXml(byte buf[], Class clazz) throws DocumentSerializationException { return getDocumentSerializableFromXml(new ByteArrayInputStream(buf), clazz); } /** * Create a DocumentSerializable Object from an XML Document * * @param in The Stream containing an XML Document to be read * @param clazz The Class of the resurrected object (must implement DocumentSerializable and have a public no-arg constructor) * @return An object of type 'clazz' * @throws DocumentSerializationException if Unable to parse object. **/ public static DocumentSerializable getDocumentSerializableFromXml(InputStream in, Class clazz) throws DocumentSerializationException { try { XMLDocument xmlDoc = (XMLDocument) StructuredDocumentFactory.newStructuredDocument(MimeMediaType.XMLUTF8, in); return getDocumentSerializable(xmlDoc.getRoot(), clazz); } catch (IOException readErr) { throw new DocumentSerializationException("Unable to read the document", readErr); } catch (JxtaException e) { throw new DocumentSerializationException("Unable to get the document", e); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -