Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2018-2022 Pavel Ponec, https://github.com/pponec
* https://github.com/pponec/ujorm/blob/master/samples/servlet/src/main/java/org/ujorm/ujoservlet/tools/Html.java
*
* 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.
*/
package org.ujorm.tools.web;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Base64;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.StringJoiner;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.servlet.http.HttpServletRequest;
import javax.sql.rowset.spi.XmlWriter;
import org.ujorm.tools.Assert;
import org.ujorm.tools.Check;
import org.ujorm.tools.web.ao.Column;
import org.ujorm.tools.web.ao.WebUtils;
import org.ujorm.tools.xml.ApiElement;
import org.ujorm.tools.xml.builder.XmlBuilder;
import org.ujorm.tools.xml.model.XmlModel;
import org.ujorm.tools.web.ao.HttpParameter;
import org.ujorm.tools.web.ao.Injector;
/**
* A HTML Element implements some methods for frequently used elements and attributes
*
*
Usage
*
*
* MockServletResponse response = new MockServletResponse();
* try (HtmlElement html = HtmlElement.of(response)) {
* try (Element body = html.getBody()) {
* body.addHeading("Hello!");
* }
* }
* assertTrue(response.toString().contains("<h1>Hello!</h1>"));
*
*
* @see HtmlElement#of(org.ujorm.tools.xml.config.HtmlConfig)
*/
public final class Element implements ApiElement, Html {
/** An original XML element */
final ApiElement internalElement;
/** New element for an API element
* @see #of(org.ujorm.tools.xml.ApiElement)
*/
Element(@NotNull final ApiElement original) {
this.internalElement = original;
}
@NotNull
@Override
public String getName() {
return internalElement.getName();
}
/**
* Set an attribute
* @param name Required element name
* @param value The {@code null} value is silently ignored. Formatting is performed by the
* {@link XmlWriter#writeValue(java.lang.Object, org.ujorm.tools.model.XmlModel, java.lang.String, java.io.Writer) }
* method, where the default implementation calls a {@code toString()} only.
* @return The original element
*/
@NotNull
@Override
public Element setAttribute(@NotNull final String name, @Nullable final Object value) {
internalElement.setAttribute(name, value);
return this;
}
/**
* Set an attribute
* @param name Required element name
* @param value The {@code null} value is silently ignored. Formatting is performed by the
* {@link XmlWriter#writeValue(java.lang.Object, org.ujorm.tools.model.XmlModel, java.lang.String, java.io.Writer) }
* method, where the default implementation calls a {@code toString()} only.
* @return The original element
*/
@NotNull
public Element setAttributes(
@NotNull final String name,
@NotNull final CharSequence separator,
@NotNull final Object... value) {
final String val = Stream.of(value)
.filter(Objects::nonNull)
.map(v -> v.toString())
.collect(Collectors.joining(separator));
internalElement.setAttribute(name, val);
return this;
}
/**
* Set an attribute with no value
* @param name Required element name
* @return The original element
*/
@NotNull
public Element setAttribute(@NotNull final String name) {
return setAttribute(name, "");
}
/**
* An deprecated shortcut for the method {@link #setAttribute(java.lang.String, java.lang.Object) }.
* @param name Required element name
* @param value The {@code null} value is silently ignored. Formatting is performed by the
* {@link XmlWriter#writeValue(java.lang.Object, org.ujorm.tools.model.XmlModel, java.lang.String, java.io.Writer) }
* method, where the default implementation calls a {@code toString()} only.
* @return The original element
*/
@Deprecated
@NotNull
@Override
public Element setAttrib(@NotNull final String name, @Nullable final Object value) {
return setAttribute(name, value);
}
/**
* A shortcut for the method {@link #setAttribute(java.lang.String, java.lang.Object) }.
* @param name Required element name
* @param value The {@code null} value is silently ignored. Formatting is performed by the
* {@link XmlWriter#writeValue(java.lang.Object, org.ujorm.tools.model.XmlModel, java.lang.String, java.io.Writer) }
* method, where the default implementation calls a {@code toString()} only.
* @return The original element
*/
@NotNull
public Element setAttr(@NotNull final String name, @Nullable final Object value) {
return setAttribute(name, value);
}
/** Add simple text
* @param data Text item
* @return A parent element.
* @see #addAnchoredText(java.lang.String, java.lang.Object...)
*/
@NotNull
@Override
public Element addText(final Object data) throws IllegalStateException {
internalElement.addText(data);
return this;
}
/**
* Add many texts with no separator
* @param data Text items
* @return A parent element.
* @see #addAnchoredText(java.lang.String, java.lang.Object...)
*/
@NotNull
public Element addText(@NotNull final Object... data) throws IllegalStateException {
return addTexts("", data);
}
/**
* Use the method {@link #addTextTemplated(java.lang.String, java.lang.Object...) } raher.
*
* @param template A message template with an ENGLISH locale. See {@link String#format(java.lang.String, java.lang.Object...) for more parameters.
* @param data A template parameters
* @return A parent element.
*/
@Deprecated
@NotNull
public Element addTemplate(@NotNull final String template, @NotNull final Object... data)
throws IllegalStateException {
return addText(String.format(Locale.ENGLISH, template, data));
}
/**
* Add a template based text with parameters with hight performance.
*
* @param template A message template with an ENGLISH locale. See {@link String#format(java.lang.String, java.lang.Object...) for more parameters.
* @param values A template parameters
* @return A parent element.
*/
@NotNull
@Override
public Element addTextTemplated(CharSequence template, Object... values) {
internalElement.addTextTemplated(template, values);
return this;
}
/**
* Use the the method {@link #addTexts(java.lang.CharSequence, java.lang.Object...) } rather;
* @param separator The delimiter must contain no special HTML character.
* @param data Data to print
* @return The current element
* @throws IllegalStateException
*/
@Deprecated
public Element addTextSeparted(
@NotNull final CharSequence separator,
@NotNull final Object... data)
throws IllegalStateException {
return addTexts(separator, data);
}
/**
* Add many words separated by a delimeter
* @param separator The delimiter must contain no special HTML character.
* @param data Data to print
* @return The current element
* @throws IllegalStateException
*/
public Element addTexts(
@NotNull final CharSequence separator,
@NotNull final Object... data)
throws IllegalStateException {
for (int i = 0, max = data.length; i < max; i++) {
if (i > 0) {
internalElement.addRawText(separator);
}
internalElement.addText(data[i]);
}
return this;
}
@NotNull
@Override
public Element addRawText(@Nullable final Object data) throws IllegalStateException {
internalElement.addRawText(data);
return this;
}
@NotNull
public Element addRawText(@NotNull final Object... data) throws IllegalStateException {
for (Object item : data) {
internalElement.addRawText(item);
}
return this;
}
/**
* Add many words separated by a delimeter
* @param separator The delimiter must contain no special HTML character.
* @param data Data to print
* @return The current element
* @throws IllegalStateException
*/
public Element addRawTexts(
@NotNull final CharSequence separator,
@NotNull final Object... data)
throws IllegalStateException {
for (int i = 0, max = data.length; i < max; i++) {
if (i > 0) {
internalElement.addRawText(separator);
}
internalElement.addRawText(data[i]);
}
return this;
}
@NotNull
@Override
public Element addComment(CharSequence comment) throws IllegalStateException {
internalElement.addComment(comment);
return this;
}
@NotNull
@Override
public Element addCDATA(CharSequence charData) throws IllegalStateException {
internalElement.addCDATA(charData);
return this;
}
@NotNull
@Override
public void close() throws IllegalStateException {
internalElement.close();
}
// -------------- Add ELEMENT -----
/**
* Create new Element
* @param name The element name
* @return New instance of the Element
* @throws IllegalStateException An envelope for IO exceptions
*/
@Override @NotNull
public Element addElement(@NotNull final String name) throws IllegalStateException {
return new Element(internalElement.addElement(name));
}
/**
* Add a new Element with optional CSS classes
* @param name A required name of the element
* @param cssClasses Optional CSS classes.
* @return New instance of the Element
*/
@NotNull
public Element addElement(@NotNull final String name, @NotNull final CharSequence... cssClasses) {
return addElement(name).setClass(cssClasses);
}
/**
* Add an element according to a condition.
* @param enabled A condition for rendering the element.
* @param name An element name
* @param cssClasses CSS classes
* @return New instance of the Element
*/
@NotNull
public Element addElementIf(final boolean enabled,
@NotNull final String name,
@NotNull final CharSequence... cssClasses) {
return addElement(enabled ? name : XmlBuilder.HIDDEN_NAME).setClass(cssClasses);
}
/** Add new Table */
@NotNull
public Element addTable(@NotNull final CharSequence... cssClasses) {
return addElement(TABLE, cssClasses);
}
/** Add new Table with cellpadding a cellspacing values to zero.
* @deprecated Use a CSS style rather.
*/
@Deprecated
@NotNull
public Element addTableNoSpaces(@NotNull final CharSequence... cssClasses) {
return addTable(cssClasses)
.setAttribute(Element.A_CELLPADDING, 0)
.setAttribute(Element.A_CELLSPACING, 0);
}
/** Create a HTML table according to data */
@NotNull
public Element addTable(
@NotNull final Object[][] data,
@NotNull final CharSequence... cssClass) {
return addTable(Arrays.asList(data), cssClass);
}
/** Create a HTML table according to data */
@NotNull
public Element addTable(
@NotNull final Collection