001package com.hfg.css; 002 003 004//------------------------------------------------------------------------------ 005 006import com.hfg.util.StringUtil; 007 008import java.util.ArrayList; 009import java.util.List; 010 011/** 012 A CSS declaration container composed of a CSSProperty and a value. 013 014 @author J. Alex Taylor, hairyfatguy.com 015 */ 016//------------------------------------------------------------------------------ 017// com.hfg Library 018// 019// This library is free software; you can redistribute it and/or 020// modify it under the terms of the GNU Lesser General Public 021// License as published by the Free Software Foundation; either 022// version 2.1 of the License, or (at your option) any later version. 023// 024// This library is distributed in the hope that it will be useful, 025// but WITHOUT ANY WARRANTY; without even the implied warranty of 026// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 027// Lesser General Public License for more details. 028// 029// You should have received a copy of the GNU Lesser General Public 030// License along with this library; if not, write to the Free Software 031// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 032// 033// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 034// jataylor@hairyfatguy.com 035//------------------------------------------------------------------------------ 036 037public class CSSDeclaration 038{ 039 private CSSProperty mProperty; 040 private String mValue; 041 private boolean mImportant; 042 043 //-------------------------------------------------------------------------- 044 public CSSDeclaration(CSSProperty inProperty, int inValue) 045 { 046 this(inProperty, inValue + ""); 047 } 048 049 //-------------------------------------------------------------------------- 050 public CSSDeclaration(CSSProperty inProperty, String inValue) 051 { 052 if (null == inProperty) 053 { 054 throw new CSSException("The CSS property for a CSS declaration cannot be null!"); 055 } 056 057 mProperty = inProperty; 058 if (inValue != null) 059 { 060 mValue = inValue.trim(); 061 062 // Remove trailing semi-colon separator if present 063 if (mValue.endsWith(";")) 064 { 065 mValue = mValue.substring(0, mValue.length() - 1).trim(); 066 } 067 068 if (mValue.endsWith("!important")) 069 { 070 mImportant = true; 071 mValue = mValue.substring(0, mValue.length() - "!important".length()).trim(); 072 } 073 } 074 } 075 076 //-------------------------------------------------------------------------- 077 public static List<CSSDeclaration> parse(String inValue) 078 { 079 List<CSSDeclaration> declarations = null; 080 if (StringUtil.isSet(inValue)) 081 { 082 declarations = new ArrayList<>(20); 083 084 String[] pieces = inValue.trim().split("\\s*;\\s*"); 085 for (String piece : pieces) 086 { 087 int index = piece.indexOf(":"); 088 String value = piece.substring(index + 1).trim(); 089 if (StringUtil.isSet(value)) 090 { 091 declarations.add(new CSSDeclaration(CSSProperty.valueOf(piece.substring(0, index).trim()), value)); 092 } 093 } 094 } 095 096 return declarations; 097 } 098 099 //-------------------------------------------------------------------------- 100 public CSSProperty getProperty() 101 { 102 return mProperty; 103 } 104 105 //-------------------------------------------------------------------------- 106 public String getValue() 107 { 108 return mValue; 109 } 110 111 //-------------------------------------------------------------------------- 112 public CSSDeclaration setValue(String inValue) 113 { 114 mValue = inValue; 115 return this; 116 } 117 118 //-------------------------------------------------------------------------- 119 public boolean isImportant() 120 { 121 return mImportant; 122 } 123 124 //-------------------------------------------------------------------------- 125 public CSSDeclaration setIsImportant(boolean inValue) 126 { 127 mImportant = inValue; 128 return this; 129 } 130 131 //-------------------------------------------------------------------------- 132 @Override 133 public String toString() 134 { 135 return getProperty() + ":" + getValue() + (mImportant ? " !important" : ""); 136 } 137}