Module: Celerity::Util

Defined in:
lib/celerity/util.rb

Constant Summary collapse

HtmlUnit2CelerityElement =
{
  HtmlUnit::Html::HtmlAnchor                => Celerity::Link,
  HtmlUnit::Html::HtmlArea                  => Celerity::Area,
  HtmlUnit::Html::HtmlButton                => Celerity::Button,
  HtmlUnit::Html::HtmlSubmitInput           => Celerity::Button,
  HtmlUnit::Html::HtmlResetInput            => Celerity::Button,
  HtmlUnit::Html::HtmlImageInput            => Celerity::Button,
  HtmlUnit::Html::HtmlButtonInput           => Celerity::Button,
  HtmlUnit::Html::HtmlCaption               => Celerity::Button, # ?
  HtmlUnit::Html::HtmlCheckBoxInput         => Celerity::CheckBox,
  HtmlUnit::Html::HtmlDefinitionDescription => Celerity::Dd,
  HtmlUnit::Html::HtmlDefinitionList        => Celerity::Dl,
  HtmlUnit::Html::HtmlDefinitionTerm        => Celerity::Dt,
  HtmlUnit::Html::HtmlDeletedText           => Celerity::Del,
  HtmlUnit::Html::HtmlDivision              => Celerity::Div,
  HtmlUnit::Html::HtmlFileInput             => Celerity::FileField,
  HtmlUnit::Html::HtmlForm                  => Celerity::Form,
  HtmlUnit::Html::HtmlFrame                 => Celerity::Frame,
  HtmlUnit::Html::HtmlHeading1              => Celerity::H1,
  HtmlUnit::Html::HtmlHeading2              => Celerity::H2,
  HtmlUnit::Html::HtmlHeading3              => Celerity::H3,
  HtmlUnit::Html::HtmlHeading4              => Celerity::H4,
  HtmlUnit::Html::HtmlHeading5              => Celerity::H5,
  HtmlUnit::Html::HtmlHeading6              => Celerity::H6,
  HtmlUnit::Html::HtmlHiddenInput           => Celerity::Hidden,
  HtmlUnit::Html::HtmlImage                 => Celerity::Image,
  HtmlUnit::Html::HtmlLabel                 => Celerity::Label,
  HtmlUnit::Html::HtmlListItem              => Celerity::Li,
  HtmlUnit::Html::HtmlMap                   => Celerity::Map,
  HtmlUnit::Html::HtmlOption                => Celerity::Option,
  HtmlUnit::Html::HtmlOrderedList           => Celerity::Ol,
  HtmlUnit::Html::HtmlParagraph             => Celerity::P,
  HtmlUnit::Html::HtmlPasswordInput         => Celerity::TextField,
  HtmlUnit::Html::HtmlPreformattedText      => Celerity::Pre,
  HtmlUnit::Html::HtmlRadioButtonInput      => Celerity::Radio,
  HtmlUnit::Html::HtmlSelect                => Celerity::SelectList,
  HtmlUnit::Html::HtmlSpan                  => Celerity::Span,
  HtmlUnit::Html::HtmlTable                 => Celerity::Table,
  HtmlUnit::Html::HtmlTableBody             => Celerity::TableBody,
  HtmlUnit::Html::HtmlTableCell             => Celerity::TableCell,
  HtmlUnit::Html::HtmlTableDataCell         => Celerity::TableCell,
  HtmlUnit::Html::HtmlTableFooter           => Celerity::TableFooter,
  HtmlUnit::Html::HtmlTableHeader           => Celerity::TableHeader,
  HtmlUnit::Html::HtmlTableRow              => Celerity::TableRow,
  HtmlUnit::Html::HtmlTextArea              => Celerity::TextField,
  HtmlUnit::Html::HtmlTextInput             => Celerity::TextField,
  HtmlUnit::Html::HtmlUnorderedList         => Celerity::Ul,
  HtmlUnit::Html::HtmlInsertedText          => Celerity::Ins
}
ContentTypes =

HtmlUnit will recognize most common file types, but custom ones can be added here. Used for FileField uploads.

{
  ".bmp" => "image/x-ms-bmp",
  ".doc" => "application/msword",
  ".odg" => "application/vnd.oasis.opendocument.graphics",
  ".odp" => "application/vnd.oasis.opendocument.presentation",
  ".ods" => "application/vnd.oasis.opendocument.spreadsheet",
  ".odt" => "application/vnd.oasis.opendocument.text",
  ".ppt" => "application/vnd.ms-powerpoint",
  ".xls" => "application/vnd.ms-excel",
}
TO_S_SIZE =

number of spaces that separate the property from the value in the create_string method

14

Class Method Summary collapse

Class Method Details

.content_type_for(path) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/celerity/util.rb', line 75

def content_type_for(path)
  if ct = java.net.URLConnection.getFileNameMap.getContentTypeFor(path)
    return ct
  else
    ContentTypes[File.extname(path).downcase]
  end
end

.create_string(element) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/celerity/util.rb', line 96

def create_string(element)
  ret = []

  unless (tag = element.getTagName).empty?
    ret << "tag:".ljust(TO_S_SIZE) + tag
  end

  element.getAttributes.each do |attribute|
    ret << "  #{attribute.getName}:".ljust(TO_S_SIZE + 2) + attribute.getValue.to_s
  end

  unless (text = element.asText).empty?
    ret << "  text:".ljust(TO_S_SIZE + 2) + element.asText
  end

  ret.join("\n")
end

.htmlunit2celerity(klass) ⇒ Object



55
56
57
# File 'lib/celerity/util.rb', line 55

def htmlunit2celerity(klass)
  HtmlUnit2CelerityElement[klass]
end

.logger_for(package_string) ⇒ Object



89
90
91
# File 'lib/celerity/util.rb', line 89

def logger_for(package_string)
  java.util.logging.Logger.getLogger(package_string)
end

.matches?(string, what) ⇒ Fixnum, ...

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Used internally.

Parameters:

  • string (String)

    The string to match against.

  • what (Regexp, String, #to_s)

    The match we’re looking for.

Returns:

  • (Fixnum, true, false, nil)


124
125
126
# File 'lib/celerity/util.rb', line 124

def matches?(string, what)
  Regexp === what ? string.strip =~ what : string == what.to_s
end

.normalize_text(string) ⇒ Object



83
84
85
86
87
# File 'lib/celerity/util.rb', line 83

def normalize_text(string)
  string.gsub("\302\240", ' '). # non-breaking space 00A0
         gsub(/\n|\t/, '').     # get rid of newlines/tabs
         strip
end