Module: Confetti::Template::JavaChecks
- Included in:
- AndroidManifest, WebosAppinfo
- Defined in:
- lib/confetti/templates/java_checks.rb
Constant Summary collapse
- RESERVED_WORDS =
%w{abstract assert boolean break byte case catch char class const continue default do double else enum extends false final finally float for goto if implements import instanceof int interface long native new null package private protected public return short static strictfp super switch synchronized this throw throws transient true try void volatile while}
Instance Method Summary collapse
- #convert_to_java_identifier(str) ⇒ Object
- #convert_to_java_package_id(str) ⇒ Object
- #is_java_identifier(str) ⇒ Object
- #is_java_package_id(str) ⇒ Object
-
#reserved_word?(str) ⇒ Boolean
checks whether str is a reserved word in Java.
Instance Method Details
#convert_to_java_identifier(str) ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/confetti/templates/java_checks.rb', line 15 def convert_to_java_identifier(str) return "_#{ str }" if reserved_word?(str) str. sub(/^\d/,"_"). gsub(/\s/,""). gsub(/[^a-zA-Z0-9_]/,"_") end |
#convert_to_java_package_id(str) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/confetti/templates/java_checks.rb', line 31 def convert_to_java_package_id(str) return str if is_java_package_id(str) bits = str.split('.', -1) fixed_bits = bits.reject {|cmp| cmp.empty? }.map do |cmp| convert_to_java_identifier(cmp) end fixed_bits.unshift('com') if fixed_bits.length == 1 fixed_bits.join('.') end |
#is_java_identifier(str) ⇒ Object
11 12 13 |
# File 'lib/confetti/templates/java_checks.rb', line 11 def is_java_identifier(str) str.match(/^[a-zA-Z_][a-zA-Z0-9_]*$/) and !reserved_word?(str) end |
#is_java_package_id(str) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/confetti/templates/java_checks.rb', line 24 def is_java_package_id(str) components = str.split('.', -1) # second param to #split ensures can't be dot-terminated components.length > 1 and components.all? { |s| is_java_identifier(s) } end |
#reserved_word?(str) ⇒ Boolean
checks whether str is a reserved word in Java
45 46 47 |
# File 'lib/confetti/templates/java_checks.rb', line 45 def reserved_word?(str) RESERVED_WORDS.include?(str.downcase) end |