All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sourceforge.tink.model.impl.TinkUtilImpl Maven / Gradle / Ivy

/**
 * Copyright 2008,2009 Ivan SZKIBA
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * under the License.
 */

package net.sourceforge.tink.model.impl;

import net.sourceforge.tink.model.TinkContext;
import net.sourceforge.tink.model.TinkException;
import net.sourceforge.tink.model.TinkUtil;

import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;

import java.io.StringWriter;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

public class TinkUtilImpl extends AbstractTinkComponent implements TinkUtil
{
    private Transformer transformer;

    @Override public String formatAttributes(Element element)
    {
        StringBuilder buff = new StringBuilder();
        NamedNodeMap attrs = element.getAttributes();

        for (int i = 0; i < attrs.getLength(); i++)
        {
            if (i != 0)
            {
                buff.append(' ');
            }

            buff.append(attrs.item(i).getNodeName());
            buff.append('=');
            buff.append('"');
            buff.append(attrs.item(i).getNodeValue());
            buff.append('"');
        }

        return buff.toString();
    }

    @Override public String formatContent(Element element) throws TinkException
    {
        StringWriter buff = new StringWriter();
        StreamResult result = new StreamResult(buff);
        NodeList nodes = element.getChildNodes();

        for (int i = 0; i < nodes.getLength(); i++)
        {
            DOMSource source = new DOMSource(nodes.item(i));

            try
            {
                transformer.transform(source, result);
            }
            catch (TransformerException x)
            {
                throw new TinkException(x);
            }
        }

        return buff.toString();
    }

    @Override public void init(TinkContext context) throws TinkException
    {
        super.init(context);
        try
        {
            transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty(OutputKeys.METHOD, "xml");
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        }
        catch (TransformerConfigurationException e)
        {
            throw new TinkException("Transformer configuration problem", e);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy