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

org.restlet.ext.emf.internal.EmfHtmlWriter Maven / Gradle / Ivy

The newest version!
/**
 * Copyright 2005-2020 Talend
 * 
 * The contents of this file are subject to the terms of one of the following
 * open source licenses: Apache 2.0 or or EPL 1.0 (the "Licenses"). You can
 * select the license that you prefer but you may not use this file except in
 * compliance with one of these Licenses.
 * 
 * You can obtain a copy of the Apache 2.0 license at
 * http://www.opensource.org/licenses/apache-2.0
 * 
 * You can obtain a copy of the EPL 1.0 license at
 * http://www.opensource.org/licenses/eclipse-1.0
 * 
 * See the Licenses for the specific language governing permissions and
 * limitations under the Licenses.
 * 
 * Alternatively, you can obtain a royalty free commercial license with less
 * limitations, transferable or non-transferable, directly at
 * https://restlet.talend.com/
 * 
 * Restlet is a registered trademark of Talend S.A.
 */

package org.restlet.ext.emf.internal;

import java.io.IOException;
import java.io.Writer;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;

/**
 * Offers a generic HTML representation of an EMF object. It lists all its
 * properties and can even generate HTML links when the proper EMF eAnnotation
 * is detected.
 * 
 * This is useful to be able to automatically navigate a Web API whose resource
 * representations are defined using EMF.
 * 
 * @author Jerome Louvel
 * @deprecated Will be removed in next minor release.
 */
@Deprecated
public class EmfHtmlWriter {

    public static final String ANNOTATION_URI = "http://restlet.org/schemas/2011/emf/html";

    private final EObject object;

    /**
     * Constructor.
     * 
     * @param object
     */
    public EmfHtmlWriter(EObject object) {
        this.object = object;
    }

    /**
     * Returns the EMF object to write.
     * 
     * @return The EMF object to write.
     */
    public EObject getObject() {
        return object;
    }

    /**
     * Writes the wrapped EMF object as an HTML document.
     * 
     * @param writer
     *            The writer to use.
     * @throws IOException
     */
    @SuppressWarnings("unchecked")
    public void write(Writer writer) throws IOException {
        String title = null;
        EClass eClass = getObject().eClass();
        EAnnotation annotation = eClass.getEAnnotation(ANNOTATION_URI);

        if (annotation != null) {
            title = (String) annotation.getDetails().get("label");
        }

        title = (title == null) ? eClass.getName() : title;

        // Write the header
        writer.write("\n");
        writer.write("\n");
        writer.write("

" + title + "

\n"); writer.write("\n"); writer.write("\n"); writer.write(""); writer.write("\n"); writer.write("\n"); writer.write("\n"); writer.write("\n"); writer.write("\n"); // Write the object properties for (EObject content : eClass.eContents()) { if (content instanceof EStructuralFeature) { EStructuralFeature sf = (EStructuralFeature) content; String label = null; boolean hyperlink = false; annotation = sf.getEAnnotation(ANNOTATION_URI); if (annotation != null) { label = (String) annotation.getDetails().get("label"); hyperlink = Boolean.parseBoolean(annotation.getDetails() .get("linked")); } label = (label == null) ? sf.getName() : label; Object value = getObject().eGet(sf); if (value instanceof EList) { EList items = (EList) value; for (Object item : items) { writeRow(writer, label, item.toString(), hyperlink); } } else { writeRow(writer, label, (value == null) ? "null" : value.toString(), hyperlink); } } } writer.write("\n"); writer.write("
PropertyValue
\n"); writer.write("\n"); writer.write("\n"); writer.flush(); } private void writeRow(Writer writer, String name, String value, boolean hyperlink) throws IOException { writer.write(""); // Write the property name writer.write(""); writer.write(name); writer.write("\n"); // Write the property value writer.write(""); if (hyperlink) { writer.write(""); } writer.write(value); if (hyperlink) { writer.write(""); } writer.write("\n"); writer.write("\n"); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy