Java Code Examples for org.apache.xml.serializer.OutputPropertiesFactory

Following code examples demonstrate how to use org.apache.xml.serializer.OutputPropertiesFactoryfrom android. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.apache.xml.serializer.OutputPropertiesFactoryand various code implementation of this class.

    }

    public static String Serialize(Node html)
        throws IOException
    {
        Properties properties = OutputPropertiesFactory.getDefaultMethodProperties(Method.HTML);
        Serializer serializer = SerializerFactory.getSerializer(properties);
        StringWriter writer = new StringWriter();
        serializer.setWriter(writer);
        serializer.asDOMSerializer().serialize(html);
        return writer.toString();
    } 


	public void outputtoXML(OutputStream out) throws ParserConfigurationException, TransformerException {
		
		StreamResult result = new StreamResult(out);

		CopyContentResultCreator creator = new CopyContentResultCreator(results);
		Document xml = creator.create();
		DOMSource source = new DOMSource(xml);

		Transformer transformer = TransformerFactory.newInstance().newTransformer();
		transformer.setOutputProperty(OutputKeys.INDENT, "yes");
		transformer.setOutputProperty(OutputPropertiesFactory.S_KEY_INDENT_AMOUNT, "4");

		transformer.transform(source, result);		
	} 

	public static String outputXmlFile(Document xmlDocument, String fileName) throws XmlCreateAppException {
		Transformer transformer  = null;
		try {
			transformer = transFactory.newTransformer();
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
			transformer.setOutputProperty(OutputKeys.METHOD, "xml");
			transformer.setOutputProperty(OutputPropertiesFactory.S_KEY_INDENT_AMOUNT, "2");
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
			System.err.println("�o�͐ݒ�Ɏ��s���܂����B");
			throw new XmlCreateAppException();
		}

		DOMSource source = new DOMSource(xmlDocument);
		File xml = new File(directoryPath + fileName);
		try {
			FileOutputStream fos = new FileOutputStream(xml);
			StreamResult result = new StreamResult(fos);
			transformer.transform(source, result);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.err.println("�t�@�C���̍쐬�Ɏ��s���܂����B");
			throw new XmlCreateAppException();
		} catch (TransformerException e) {
			e.printStackTrace();
			System.err.println("XML�̏o�͂Ɏ��s���܂����B");
			throw new XmlCreateAppException();
		}

		return xml.getPath();
	} 

    public static void printSmartXML(OMElement elem) {
        try {
            Document document = DocumentBuilderFactory.newInstance()
                    .newDocumentBuilder().parse(
                            new InputSource(new StringReader(elem.toString())));

            Transformer transformer = TransformerFactory.newInstance()
                    .newTransformer();

            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty(OutputKeys.METHOD, "xml");
            transformer.setOutputProperty(
                    OutputPropertiesFactory.S_KEY_INDENT_AMOUNT, ""
                            + DEFAULT_INDENT_NUM);

            Writer writer = new PrintWriter(System.out);
            transformer.transform(new DOMSource(document), new StreamResult(writer));
        } catch (Throwable e) {
            e.printStackTrace();
        }
    } 

    private static Properties loadPropertiesFile(String resourceName, Properties defaults) throws IOException {
        IOException ioe;
        SecurityException se;
        Throwable th;
        Properties props = new Properties(defaults);
        InputStream inputStream = null;
        BufferedInputStream bufferedInputStream = null;
        try {
            if (ACCESS_CONTROLLER_CLASS != null) {
                inputStream = (InputStream) AccessController.doPrivileged(new AnonymousClass1(resourceName));
            } else {
                inputStream = OutputPropertiesFactory.class.getResourceAsStream(resourceName);
            }
            BufferedInputStream bis = new BufferedInputStream(inputStream);
            try {
                props.load(bis);
                if (bis != null) {
                    bis.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
                Enumeration keys = ((Properties) props.clone()).keys();
                while (keys.hasMoreElements()) {
                    String key = (String) keys.nextElement();
                    String str = null;
                    try {
                        str = System.getProperty(key);
                    } catch (SecurityException e) {
                    }
                    if (str == null) {
                        str = (String) props.get(key);
                    }
                    String newKey = fixupPropertyString(key, true);
                    String newValue = null;
                    try {
                        newValue = System.getProperty(newKey);
                    } catch (SecurityException e2) {
                    }
                    if (newValue == null) {
                        newValue = fixupPropertyString(str, false);
                    } else {
                        newValue = fixupPropertyString(newValue, false);
                    }
                    if (key != newKey || str != newValue) {
                        props.remove(key);
                        props.put(newKey, newValue);
                    }
                }
                return props;
            } catch (IOException e3) {
                ioe = e3;
                bufferedInputStream = bis;
                if (defaults != null) {
                    throw new WrappedRuntimeException(Utils.messages.createMessage(MsgKey.ER_COULD_NOT_LOAD_RESOURCE, new Object[]{resourceName}), ioe);
                }
                throw ioe;
            } catch (SecurityException e4) {
                se = e4;
                bufferedInputStream = bis;
                if (defaults != null) {
                    throw new WrappedRuntimeException(Utils.messages.createMessage(MsgKey.ER_COULD_NOT_LOAD_RESOURCE, new Object[]{resourceName}), se);
                }
                throw se;
            } catch (Throwable th2) {
                th = th2;
                bufferedInputStream = bis;
                if (bufferedInputStream != null) {
                    bufferedInputStream.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
                throw th;
            }
        } catch (IOException e5) {
            ioe = e5;
            if (defaults != null) {
                throw ioe;
            }
            throw new WrappedRuntimeException(Utils.messages.createMessage(MsgKey.ER_COULD_NOT_LOAD_RESOURCE, new Object[]{resourceName}), ioe);
        } catch (SecurityException e6) {
            se = e6;
            if (defaults != null) {
                throw se;
            } else {
                throw new WrappedRuntimeException(Utils.messages.createMessage(MsgKey.ER_COULD_NOT_LOAD_RESOURCE, new Object[]{resourceName}), se);
            }
        } catch (Throwable th3) {
            th = th3;
        }
    } 

    public static void main(String[] args) {
        if(args.length != 2){
            System.out.println("Usage CSV2XML <input> <output>");
            return;
        }
        try {

            File outputFile = new File(args[1]);
            StreamResult result = new StreamResult(new FileOutputStream(outputFile));
            result.setSystemId(outputFile.getAbsolutePath());

            Properties props = OutputPropertiesFactory.getDefaultMethodProperties(Method.XML);
            props.setProperty(OutputKeys.INDENT,"yes");
            Serializer serializer = SerializerFactory.getSerializer(props);
            serializer.setOutputStream(result.getOutputStream());

            CSVBasicXMLReader xmlReader = new CSVBasicXMLReader();
            xmlReader.setContentHandler(serializer.asContentHandler());
            
            FileInputStream input = new FileInputStream(args[0]);
            InputSource source = new InputSource(input);
            xmlReader.parse(source);
            
            
            
        } catch (FileNotFoundException e) {
            System.out.println("File not found: " + e.getMessage());
        } catch (IOException e) {
            System.out.println("IO Exception: " + e.getMessage());
        } catch (SAXException e) {
            System.out.println("SAX Exception: " + e.getMessage());
        }
    } 

	public void outputtoXML(OutputStream out) throws ParserConfigurationException, TransformerException {

		StreamResult result = new StreamResult(out);

		BlogResultCreator creator = new BlogResultCreator(blogResults);
		Document xml = creator.create();
		DOMSource source = new DOMSource(xml);

		Transformer transformer = TransformerFactory.newInstance().newTransformer();
		transformer.setOutputProperty(OutputKeys.INDENT, "yes");
		transformer.setOutputProperty(OutputPropertiesFactory.S_KEY_INDENT_AMOUNT, "4");

		transformer.transform(source, result);
	} 

	public void outputtoXML(OutputStream out) throws ParserConfigurationException, TransformerException {

		StreamResult result = new StreamResult(out);

		AuthorResultCreator creator = new AuthorResultCreator(authorResults, showDetail);
		Document xml = creator.create();
		DOMSource source = new DOMSource(xml);

		Transformer transformer = TransformerFactory.newInstance().newTransformer();
		transformer.setOutputProperty(OutputKeys.INDENT, "yes");
		transformer.setOutputProperty(OutputPropertiesFactory.S_KEY_INDENT_AMOUNT, "4");

		transformer.transform(source, result);
	} 

    public static XMLFilter makePipeline(OutputStream out, URIResolver urlResolver, String... filters) throws Exception {
        if (filters == null || filters.length == 0) {
            return null;
        }
        XMLFilter lastFilter = null;
        TransformerFactory tf = new TransformerFactoryImpl();
        tf.setURIResolver(urlResolver);
        if (tf.getFeature(SAXSource.FEATURE) && tf.getFeature(SAXResult.FEATURE)) {
            XMLReader reader = XMLReaderFactory.createXMLReader();
            Properties xmlProps = OutputPropertiesFactory.getDefaultMethodProperties("xml");
            xmlProps.setProperty("indent", "no");
            xmlProps.setProperty("standalone", "no");
            Serializer serializer = SerializerFactory.getSerializer(xmlProps);
            serializer.setOutputStream(out);
            SAXTransformerFactory saxTF = (SAXTransformerFactory) tf;
            List<XMLFilter> filterList = new ArrayList<XMLFilter>();
            for (int i = 0; i < filters.length; i++) {
                Filter f = (Filter) saxTF.newXMLFilter(new StreamSource(XsltUtils.class.getResourceAsStream(filters[i])));
                if (i == 0) {
                    f.setParent(reader);
                } else {
                    f.setParent(filterList.get(i - 1));
                }
                if (i == filters.length - 1) {
                    f.setContentHandler(serializer.asContentHandler());
                    lastFilter = f;
                }
                filterList.add(f);
            }
        }
        return lastFilter;
    } 

Advertisement
Javadoc
This class is a factory to generate a set of default properties of key/value pairs that are used to

create a serializer through the factory SerializerFactory SerilizerFactory. The properties generated by this factory may be modified to non-default values before the SerializerFactory is used to create a Serializer.

The given output types supported are "xml", "text", and "html". These type strings can be obtained from the Method Method class in this package.

Other constants defined in this class are the non-standard property keys that can be used to set non-standard property values on a java.util.Properties object that is used to create or configure a serializer. Here are the non-standard keys: S_KEY_INDENT_AMOUNT - The non-standard property key to use to set the indentation amount. The "indent" key needs to have a value of "yes", and this properties value is a the number of whitespaces to indent by per indentation level. S_KEY_CONTENT_HANDLER - This non-standard property key is used to set the name of the fully qualified Java class that implements the ContentHandler interface. The output of the serializer will be SAX events sent to this an object of this class. S_KEY_ENTITIES - This non-standard property key is used to specify the name of the property file that specifies character to entity reference mappings. A line in such a file is has the name of the entity and the numeric (base 10) value of the corresponding character, like this one:
quot=34
S_USE_URL_ESCAPING - This non-standard property key is used to set a value of "yes" if the href values for HTML serialization should use %xx escaping. S_OMIT_META_TAG - This non-standard property key is used to set a value of "yes" if the META tag should be omitted where it would otherwise be supplied. @see SerializerFactory @see Method @see Serializer

Read More
Advertisement