<commit_before>/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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.
 */

package org.apache.isis.viewer.html.component.html;

import static org.apache.isis.viewer.html.HtmlViewerConstants.FOOTER;
import static org.apache.isis.viewer.html.HtmlViewerConstants.FOOTER_FILE;
import static org.apache.isis.viewer.html.HtmlViewerConstants.HEADER;
import static org.apache.isis.viewer.html.HtmlViewerConstants.HEADER_FILE;
import static org.apache.isis.viewer.html.HtmlViewerConstants.STYLE_SHEET;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.isis.core.commons.config.IsisConfiguration;
import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
import org.apache.isis.core.metamodel.facets.multiline.MultiLineFacet;
import org.apache.isis.core.metamodel.spec.ActionType;
import org.apache.isis.core.metamodel.spec.feature.ObjectAssociation;
import org.apache.isis.core.progmodel.facets.value.booleans.BooleanValueFacet;
import org.apache.isis.runtimes.dflt.runtime.system.context.IsisContext;
import org.apache.isis.viewer.html.component.Block;
import org.apache.isis.viewer.html.component.Component;
import org.apache.isis.viewer.html.component.ComponentFactory;
import org.apache.isis.viewer.html.component.DebugPane;
import org.apache.isis.viewer.html.component.Form;
import org.apache.isis.viewer.html.component.Page;
import org.apache.isis.viewer.html.component.Table;

public class HtmlComponentFactory implements ComponentFactory {

    private final String footer;
    private final String header;
    private final String styleSheet;

    public HtmlComponentFactory() {
        final IsisConfiguration configuration = IsisContext.getConfiguration();
        styleSheet = configuration.getString(STYLE_SHEET);
        String file = configuration.getString(HEADER_FILE);
        header = file == null ? configuration.getString(HEADER) : loadFile(file);
        file = configuration.getString(FOOTER_FILE);
        footer = file == null ? configuration.getString(FOOTER) : loadFile(file);
    }

    @Override
    public Block createBlock(final String style, final String description) {
        return new Div(style, description);
    }

    @Override
    public Component createBreadCrumbs(final String[] names, final boolean[] isLinked) {
        return new BreadCrumbs(names, isLinked);
    }

    @Override
    public Component createCollectionIcon(final ObjectAssociation field, final ObjectAdapter collection, final String id) {
        return new CollectionLink(field, collection, field.getDescription(), id);
    }

    @Override
    public DebugPane createDebugPane() {
        return new HtmlDebug();
    }

    @Override
    public Component createEditOption(final String id) {
        return new ActionComponent("edit", "Edit Object", "Edit the current object", id, null, null);
    }

    @Override
    public Component createRemoveOption(final String id, final String elementId, final String fieldName) {
        return new ActionComponent("remove", "Remove", "Remove item from collection", id, elementId, fieldName);
    }

    @Override
    public Component createAddOption(final String id, final String fieldName) {
        return new ActionComponent("add", "Add Item", "Add item to collection", id, null, fieldName);
    }

    @Override
    public Component createErrorMessage(final Exception e, final boolean isDebug) {
        return new ErrorMessage(e, isDebug);
    }

    @Override
    public Form createForm(final String id, final String actionName, final int step, final int noOfPages,
        final boolean isEditing) {
        return new HtmlForm(id, actionName, step, noOfPages, isEditing);
    }

    @Override
    public Component createHeading(final String name) {
        return new Heading(name, 4);
    }

    @Override
    public Component createInlineBlock(final String style, final String text, final String description) {
        return new Span(style, text, description);
    }

    @Override
    public Component createCheckboxBlock(final boolean isEditable, final boolean isSet) {
        return new Checkbox(isSet, isEditable);
    }

    @Override
    public Component createSubmenu(final String menuName, final Component[] items) {
        return new Submenu(menuName, items);
    }

    @Override
    public Component createLink(final String link, final String name, final String description) {
        return new Link(link, name, description);
    }

    @Override
    public Component createMenuItem(final String actionId, final String name, final String description,
        final String reasonDisabled, final ActionType type, final boolean hasParameters, final String targetObjectId) {
        return new MenuItem(actionId, name, description, reasonDisabled, type, hasParameters, targetObjectId);
    }

    @Override
    public Component createCollectionIcon(final ObjectAdapter collection, final String collectionId) {
        return new CollectionIcon(collection, collection.getSpecification().getDescription(), collectionId);
    }

    @Override
    public Component createObjectIcon(final ObjectAdapter object, final String objectId, final String style) {
        return new ObjectIcon(object, object.getSpecification().getDescription(), objectId, style);
    }

    @Override
    public Component createObjectIcon(final ObjectAssociation field, final ObjectAdapter object, final String objectId,
        final String style) {
        return new ObjectIcon(object, field.getDescription(), objectId, style);
    }

    @Override
    public Page createPage() {
        return new DynamicHtmlPage(styleSheet, header, footer);
    }

    public LogonFormPage createLogonPage(final String user, final String password) {
        return new LogonFormPage(styleSheet, header, footer, user, password);
    }

    @Override
    public Component createService(final String objectId, final String title, final String iconName) {
        return new ServiceComponent(objectId, title, iconName);
    }

    @Override
    public Table createTable(final int noColumns, final boolean withSelectorColumn) {
        return new HtmlTable(noColumns, withSelectorColumn);
    }

    @Override
    public Component createUserSwap(final String name) {
        return new UserSwapLink(name);

    }

    @Override
    public Component createParseableField(final ObjectAssociation field, final ObjectAdapter value,
        final boolean isEditable) {
        final BooleanValueFacet facet = field.getSpecification().getFacet(BooleanValueFacet.class);
        if (facet != null) {
            return createCheckboxBlock(isEditable, facet.isSet(value));
        } else {
            final String titleString = value != null ? value.titleString() : "";

            final MultiLineFacet multiLineFacet = field.getSpecification().getFacet(MultiLineFacet.class);
            final boolean isWrapped = multiLineFacet != null && !multiLineFacet.preventWrapping();

            if (isWrapped) {
                return createInlineBlock("value", "<pre>" + titleString + "</pre>", null);
            } else {
                return createInlineBlock("value", titleString, null);
            }
        }
    }

    private String loadFile(final String file) {
        final StringBuffer content = new StringBuffer();
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            String line;
            while ((line = reader.readLine()) != null) {
                content.append(line);
                content.append("\n");
            }
        } catch (final FileNotFoundException e) {
            throw new WebViewerException("Failed to find file " + file);
        } catch (final IOException e) {
            throw new WebViewerException("Failed to load file " + file, e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (final IOException ignore) {
                }
            }
        }
        return content.toString();
    }
}
<commit_msg>I needed to change visibility of some variables so that I could extend the HtmlComponentFactory and override default behaviour.<commit_after>/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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.
 */

package org.apache.isis.viewer.html.component.html;

import static org.apache.isis.viewer.html.HtmlViewerConstants.FOOTER;
import static org.apache.isis.viewer.html.HtmlViewerConstants.FOOTER_FILE;
import static org.apache.isis.viewer.html.HtmlViewerConstants.HEADER;
import static org.apache.isis.viewer.html.HtmlViewerConstants.HEADER_FILE;
import static org.apache.isis.viewer.html.HtmlViewerConstants.STYLE_SHEET;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.isis.core.commons.config.IsisConfiguration;
import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
import org.apache.isis.core.metamodel.facets.multiline.MultiLineFacet;
import org.apache.isis.core.metamodel.spec.ActionType;
import org.apache.isis.core.metamodel.spec.feature.ObjectAssociation;
import org.apache.isis.core.progmodel.facets.value.booleans.BooleanValueFacet;
import org.apache.isis.runtimes.dflt.runtime.system.context.IsisContext;
import org.apache.isis.viewer.html.component.Block;
import org.apache.isis.viewer.html.component.Component;
import org.apache.isis.viewer.html.component.ComponentFactory;
import org.apache.isis.viewer.html.component.DebugPane;
import org.apache.isis.viewer.html.component.Form;
import org.apache.isis.viewer.html.component.Page;
import org.apache.isis.viewer.html.component.Table;

public class HtmlComponentFactory implements ComponentFactory {

    protected final String footer;
    protected final String header;
    protected final String styleSheet;

    public HtmlComponentFactory() {
        final IsisConfiguration configuration = IsisContext.getConfiguration();
        styleSheet = configuration.getString(STYLE_SHEET);
        String file = configuration.getString(HEADER_FILE);
        header = file == null ? configuration.getString(HEADER) : loadFile(file);
        file = configuration.getString(FOOTER_FILE);
        footer = file == null ? configuration.getString(FOOTER) : loadFile(file);
    }

    @Override
    public Block createBlock(final String style, final String description) {
        return new Div(style, description);
    }

    @Override
    public Component createBreadCrumbs(final String[] names, final boolean[] isLinked) {
        return new BreadCrumbs(names, isLinked);
    }

    @Override
    public Component createCollectionIcon(final ObjectAssociation field, final ObjectAdapter collection, final String id) {
        return new CollectionLink(field, collection, field.getDescription(), id);
    }

    @Override
    public DebugPane createDebugPane() {
        return new HtmlDebug();
    }

    @Override
    public Component createEditOption(final String id) {
        return new ActionComponent("edit", "Edit Object", "Edit the current object", id, null, null);
    }

    @Override
    public Component createRemoveOption(final String id, final String elementId, final String fieldName) {
        return new ActionComponent("remove", "Remove", "Remove item from collection", id, elementId, fieldName);
    }

    @Override
    public Component createAddOption(final String id, final String fieldName) {
        return new ActionComponent("add", "Add Item", "Add item to collection", id, null, fieldName);
    }

    @Override
    public Component createErrorMessage(final Exception e, final boolean isDebug) {
        return new ErrorMessage(e, isDebug);
    }

    @Override
    public Form createForm(final String id, final String actionName, final int step, final int noOfPages,
        final boolean isEditing) {
        return new HtmlForm(id, actionName, step, noOfPages, isEditing);
    }

    @Override
    public Component createHeading(final String name) {
        return new Heading(name, 4);
    }

    @Override
    public Component createInlineBlock(final String style, final String text, final String description) {
        return new Span(style, text, description);
    }

    @Override
    public Component createCheckboxBlock(final boolean isEditable, final boolean isSet) {
        return new Checkbox(isSet, isEditable);
    }

    @Override
    public Component createSubmenu(final String menuName, final Component[] items) {
        return new Submenu(menuName, items);
    }

    @Override
    public Component createLink(final String link, final String name, final String description) {
        return new Link(link, name, description);
    }

    @Override
    public Component createMenuItem(final String actionId, final String name, final String description,
        final String reasonDisabled, final ActionType type, final boolean hasParameters, final String targetObjectId) {
        return new MenuItem(actionId, name, description, reasonDisabled, type, hasParameters, targetObjectId);
    }

    @Override
    public Component createCollectionIcon(final ObjectAdapter collection, final String collectionId) {
        return new CollectionIcon(collection, collection.getSpecification().getDescription(), collectionId);
    }

    @Override
    public Component createObjectIcon(final ObjectAdapter object, final String objectId, final String style) {
        return new ObjectIcon(object, object.getSpecification().getDescription(), objectId, style);
    }

    @Override
    public Component createObjectIcon(final ObjectAssociation field, final ObjectAdapter object, final String objectId,
        final String style) {
        return new ObjectIcon(object, field.getDescription(), objectId, style);
    }

    @Override
    public Page createPage() {
        return new DynamicHtmlPage(styleSheet, header, footer);
    }

    public LogonFormPage createLogonPage(final String user, final String password) {
        return new LogonFormPage(styleSheet, header, footer, user, password);
    }

    @Override
    public Component createService(final String objectId, final String title, final String iconName) {
        return new ServiceComponent(objectId, title, iconName);
    }

    @Override
    public Table createTable(final int noColumns, final boolean withSelectorColumn) {
        return new HtmlTable(noColumns, withSelectorColumn);
    }

    @Override
    public Component createUserSwap(final String name) {
        return new UserSwapLink(name);

    }

    @Override
    public Component createParseableField(final ObjectAssociation field, final ObjectAdapter value,
        final boolean isEditable) {
        final BooleanValueFacet facet = field.getSpecification().getFacet(BooleanValueFacet.class);
        if (facet != null) {
            return createCheckboxBlock(isEditable, facet.isSet(value));
        } else {
            final String titleString = value != null ? value.titleString() : "";

            final MultiLineFacet multiLineFacet = field.getSpecification().getFacet(MultiLineFacet.class);
            final boolean isWrapped = multiLineFacet != null && !multiLineFacet.preventWrapping();

            if (isWrapped) {
                return createInlineBlock("value", "<pre>" + titleString + "</pre>", null);
            } else {
                return createInlineBlock("value", titleString, null);
            }
        }
    }

    private String loadFile(final String file) {
        final StringBuffer content = new StringBuffer();
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            String line;
            while ((line = reader.readLine()) != null) {
                content.append(line);
                content.append("\n");
            }
        } catch (final FileNotFoundException e) {
            throw new WebViewerException("Failed to find file " + file);
        } catch (final IOException e) {
            throw new WebViewerException("Failed to load file " + file, e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (final IOException ignore) {
                }
            }
        }
        return content.toString();
    }
}

{'fraction_non_alphanumeric': 0.0838598824963973, 'fraction_numerical': 0.0005542622769094336, 'mean_word_length': 3.3540057915057915, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5836641', 'n_tokens_mistral': 4708, 'n_tokens_neox': 4603, 'n_words': 1402}