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

org.neo4j.server.rest.domain.HtmlHelper Maven / Gradle / Ivy

There is a newer version: 5.26.1
Show newest version
/*
 * Copyright (c) 2002-2016 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */
package org.neo4j.server.rest.domain;

import java.util.Collection;
import java.util.Map;

/**
 * This is just a simple test of how a HTML renderer could be like
 */
public class HtmlHelper
{
    private static final String STYLE_LOCATION = "http://resthtml.neo4j.org/style/";

    public static String from( final Object object, final ObjectType objectType )
    {
        StringBuilder builder = start( objectType, null );
        append( builder, object, objectType );
        return end( builder );
    }

    public static StringBuilder start( final ObjectType objectType, final String additionalCodeInHead )
    {
        return start( objectType.getCaption(), additionalCodeInHead );
    }

    public static StringBuilder start( final String title, final String additionalCodeInHead )
    {
        StringBuilder builder = new StringBuilder();
        builder.append( "\n" );
        builder.append( "" + title + "" );
        if ( additionalCodeInHead != null )
        {
            builder.append( additionalCodeInHead );
        }
        builder.append( "\n" + "\n"
                        + "\n\n" + "
" + "" + "\n
\n" ); return builder; } public static String end( final StringBuilder builder ) { builder.append( "
 
" + "
" ); return builder.toString(); } public static void appendMessage( final StringBuilder builder, final String message ) { builder.append( "

" + message + "

" ); } public static void append( final StringBuilder builder, final Object object, final ObjectType objectType ) { if ( object instanceof Collection ) { builder.append( "
    \n" ); for ( Object item : (Collection) object ) { builder.append( "
  • " ); append( builder, item, objectType ); builder.append( "
  • \n" ); } builder.append( "
\n" ); } else if ( object instanceof Map ) { Map map = (Map) object; String htmlClass = objectType.getHtmlClass(); String caption = objectType.getCaption(); if ( !map.isEmpty() ) { boolean isNodeOrRelationship = ObjectType.NODE.equals( objectType ) || ObjectType.RELATIONSHIP.equals( objectType ); if ( isNodeOrRelationship ) { builder.append( "

" + caption + "

\n" ); append( builder, map.get( "data" ), ObjectType.PROPERTIES ); htmlClass = "meta"; caption += " info"; } if ( ObjectType.NODE.equals( objectType ) && map.size() == 1 ) { // there's only properties, so we're finished here return; } builder.append( "\n" ); boolean odd = true; for ( Map.Entry entry : map.entrySet() ) { if ( isNodeOrRelationship && "data".equals( entry.getKey() ) ) { continue; } builder.append( "" ); odd = !odd; builder.append( "\n" ); } builder.append( "
" ); builder.append( caption ); builder.append( "
" + entry.getKey() + "" ); // TODO We always assume that an inner map is for // properties, correct? append( builder, entry.getValue(), ObjectType.PROPERTIES ); builder.append( "
\n" ); } else { builder.append( "" ); builder.append( "" ); builder.append( "
" ); builder.append( caption ); builder.append( "
" ); } } else { builder.append( object != null ? embedInLinkIfClickable( object.toString() ) : "" ); } } private static String embedInLinkIfClickable( String string ) { // TODO Hardcode "http://" string? if ( string.startsWith( "http://" ) || string.startsWith( "https://" ) ) { String anchoredString = ""; string = anchoredString; } else { string = escapeHtml( string ); } return string; } private static String escapeHtml( final String string ) { if ( string == null ) { return null; } String res = string.replace( "&", "&" ); res = res.replace( "\"", """ ); res = res.replace( "<", "<" ); res = res.replace( ">", ">" ); return res; } public static enum ObjectType { NODE, RELATIONSHIP, PROPERTIES, ROOT, INDEX_ROOT, ; String getCaption() { return name().substring( 0, 1 ) .toUpperCase() + name().substring( 1 ) .toLowerCase(); } String getHtmlClass() { return getCaption().toLowerCase(); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy