Package net.java.textilej.parser.builder

Source Code of net.java.textilej.parser.builder.AbstractXmlDocumentBuilder

package net.java.textilej.parser.builder;

import java.io.Writer;
import java.net.URI;
import java.util.regex.Pattern;

import net.java.textilej.parser.DocumentBuilder;
import net.java.textilej.util.DefaultXmlStreamWriter;
import net.java.textilej.util.XmlStreamWriter;

public abstract class AbstractXmlDocumentBuilder extends DocumentBuilder {
  private static final Pattern ABSOLUTE_URL_PATTERN = Pattern.compile("[a-zA-Z]{3,8}://?.*");
 
  protected final XmlStreamWriter writer;
  protected URI base;
  protected boolean baseInHead = false;
 
  public AbstractXmlDocumentBuilder(Writer out) {
    writer = createXmlStreamWriter(out);
  }
 
  public AbstractXmlDocumentBuilder(XmlStreamWriter writer) {
    this.writer = writer;

  }
 
  protected XmlStreamWriter createXmlStreamWriter(Writer out) {
    XmlStreamWriter writer = new DefaultXmlStreamWriter(out);
    return writer;
  }

  @Override
  public void characters(String text) {
    writer.writeCharacters(text);
  }
  protected String makeUrlAbsolute(String url) {
    if (base == null || baseInHead || url == null) {
      return url;
    }
    if (ABSOLUTE_URL_PATTERN.matcher(url).matches()) {
      return url;
    }
    if (url.startsWith("#")) {
      return url;
    }
    String absoluteUrl = base.toString();
    if (!absoluteUrl.endsWith("/") && !url.startsWith("/")) {
      absoluteUrl = absoluteUrl+'/';
    }
    absoluteUrl = absoluteUrl+url;
    return absoluteUrl;
  }

  /**
   * Set the base URI of the HTML document.  Causes all relative URLs to be prefixed with the base URI.
   * The base URI is assumed to refer to a foler-like resource.
   *
   * @param uri the URI, or null
   */
  public void setBase(URI uri) {
    base = uri;
  }
 
  /**
   * Get the base URI of the HTML document.  A not-null value causes all relative URLs to be prefixed with the base URI.
   * The base URI is assumed to refer to a foler-like resource.
   */
  public URI getBase() {
    return base;
  }

  /**
   * Indicate if the {@link #getBase() base URI} should be emitted into the <head> of the document.
   * The default value is false.
   * Ignored unless {@link #isEmitAsDocument()}
   */
  public boolean isBaseInHead() {
    return baseInHead;
  }

  /**
   * Indicate if the {@link #getBase() base URI} should be emitted into the <head> of the document.
   * The default value is false.
   * Ignored unless {@link #isEmitAsDocument()}
   */
  public void setBaseInHead(boolean baseInHead) {
    this.baseInHead = baseInHead;
 
}
TOP

Related Classes of net.java.textilej.parser.builder.AbstractXmlDocumentBuilder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.