Java Examples for org.w3c.dom.css.CSSStyleSheet
The following java examples will help you to understand the usage of org.w3c.dom.css.CSSStyleSheet. These source code samples are taken from different open source projects.
Example 1
| Project: eclipse.platform.ui-master File: AbstractCSSEngine.java View source code |
@Override
public StyleSheet parseStyleSheet(InputSource source) throws IOException {
// Check that CharacterStream or ByteStream is not null
checkInputSource(source);
CSSParser parser = makeCSSParser();
CSSStyleSheet styleSheet = parser.parseStyleSheet(source);
CSSRuleList rules = styleSheet.getCssRules();
int length = rules.getLength();
CSSRuleListImpl masterList = new CSSRuleListImpl();
int counter;
for (counter = 0; counter < length; counter++) {
CSSRule rule = rules.item(counter);
if (rule.getType() != CSSRule.IMPORT_RULE) {
break;
}
// processing an import CSS
CSSImportRule importRule = (CSSImportRule) rule;
URL url = null;
if (importRule.getHref().startsWith("platform")) {
url = FileLocator.resolve(new URL(importRule.getHref()));
} else {
Path p = new Path(source.getURI());
IPath trim = p.removeLastSegments(1);
boolean isArchive = source.getURI().contains(ARCHIVE_IDENTIFIER);
url = FileLocator.resolve(new URL(trim.addTrailingSeparator().toString() + ((CSSImportRule) rule).getHref()));
File testFile = new File(url.getFile());
if (!isArchive && !testFile.exists()) {
// look in platform default
String path = getResourcesLocatorManager().resolve((importRule).getHref());
testFile = new File(new URL(path).getFile());
if (testFile.exists()) {
url = new URL(path);
}
}
}
InputStream stream = null;
try {
stream = url.openStream();
InputSource tempStream = new InputSource();
tempStream.setURI(url.toString());
tempStream.setByteStream(stream);
parseImport++;
try {
styleSheet = (CSSStyleSheet) this.parseStyleSheet(tempStream);
} finally {
parseImport--;
}
CSSRuleList tempRules = styleSheet.getCssRules();
for (int j = 0; j < tempRules.getLength(); j++) {
masterList.add(tempRules.item(j));
}
} finally {
if (stream != null) {
stream.close();
}
}
}
// add remaining non import rules
for (int i = counter; i < length; i++) {
masterList.add(rules.item(i));
}
// final stylesheet
CSSStyleSheetImpl s = new CSSStyleSheetImpl();
s.setRuleList(masterList);
if (parseImport == 0) {
documentCSS.addStyleSheet(s);
}
return s;
}Example 2
| Project: SimpleFunctionalTest-master File: CssParser.java View source code |
public HashMap<String, CSSStyleRule> extractCssStyleRules(String cssFile) throws IOException {
TEST_FILE_SYSTEM.filesExists(cssFile);
CSSOMParser cssParser = new CSSOMParser();
CSSStyleSheet css = cssParser.parseStyleSheet(new InputSource(new FileReader(TEST_FILE_SYSTEM.file(cssFile))), null, null);
CSSRuleList cssRules = css.getCssRules();
HashMap<String, CSSStyleRule> rules = new HashMap<String, CSSStyleRule>();
for (int i = 0; i < cssRules.getLength(); i++) {
CSSRule rule = cssRules.item(i);
if (rule instanceof CSSStyleRule) {
rules.put(((CSSStyleRule) rule).getSelectorText(), (CSSStyleRule) rule);
}
}
return rules;
}Example 3
| Project: Erengine-master File: CssStyleParser.java View source code |
public boolean read() {
boolean rtn = false;
try {
InputSource source = new InputSource(new InputStreamReader(mStream));
CSSOMParser parser = new CSSOMParser();
CSSStyleSheet stylesheet = parser.parseStyleSheet(source, null, null);
CSSRuleList ruleList = stylesheet.getCssRules();
String selector;
String property;
String value;
HashMap<String, String> propHash;
for (int i = 0; i < ruleList.getLength(); i++) {
CSSRule rule = ruleList.item(i);
if (rule instanceof CSSStyleRule) {
CSSStyleRule styleRule = (CSSStyleRule) rule;
selector = styleRule.getSelectorText();
Logger.eLog(TAG, "selector:" + i + ": " + selector);
CSSStyleDeclaration styleDeclaration = styleRule.getStyle();
propHash = new HashMap<String, String>();
for (int j = 0; j < styleDeclaration.getLength(); j++) {
property = styleDeclaration.item(j);
value = styleDeclaration.getPropertyCSSValue(property).getCssText();
propHash.put(property, value);
Logger.dLog(TAG, "property: " + property + " value: " + value);
}
mCssStyleList.put(selector, propHash);
}
}
if (mStream != null) {
mStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
rtn = true;
return rtn;
}Example 4
| Project: webtools.sourceediting-master File: CSSFontFaceRuleTest.java View source code |
public void testInsertRule() {
final String RULE = "@font-face { font-family: \"Swiss 721\"; src: url(swiss721.pfr); /* The expanded Swiss 721 */ font-stretch: expanded; }";
CSSStyleSheet sheet = getStyleSheet();
assertEquals(0, sheet.insertRule(RULE, 0));
CSSRuleList ruleList = sheet.getCssRules();
CSSRule rule = ruleList.item(0);
assertTrue(rule instanceof CSSFontFaceRule);
CSSStyleDeclaration declaration = ((CSSFontFaceRule) rule).getStyle();
assertEquals(3, declaration.getLength());
CSSValue value;
CSSPrimitiveValue primitiveValue;
value = declaration.getPropertyCSSValue("font-family");
assertTrue(value instanceof CSSPrimitiveValue);
primitiveValue = (CSSPrimitiveValue) value;
assertEquals(CSSPrimitiveValue.CSS_STRING, primitiveValue.getPrimitiveType());
assertEquals("Swiss 721", primitiveValue.getStringValue());
value = declaration.getPropertyCSSValue("src");
assertTrue(value instanceof CSSPrimitiveValue);
primitiveValue = (CSSPrimitiveValue) value;
assertEquals(CSSPrimitiveValue.CSS_URI, primitiveValue.getPrimitiveType());
assertEquals("swiss721.pfr", primitiveValue.getStringValue());
value = declaration.getPropertyCSSValue("font-stretch");
assertTrue(value instanceof CSSPrimitiveValue);
primitiveValue = (CSSPrimitiveValue) value;
assertEquals(CSSPrimitiveValue.CSS_IDENT, primitiveValue.getPrimitiveType());
assertEquals("expanded", primitiveValue.getStringValue());
}Example 5
| Project: infoglue-master File: CSSHelper.java View source code |
public List getCSSRules() {
List list = new ArrayList();
try {
Reader r = new InputStreamReader(new URL(this.cssUrl).openStream());
CSSOMParser parser = new CSSOMParser();
InputSource is = new InputSource(r);
CSSStyleSheet stylesheet = parser.parseStyleSheet(is);
CSSRuleList rules = stylesheet.getCssRules();
for (int i = 0; i < rules.getLength(); i++) {
CSSRule rule = rules.item(i);
list.add(rule);
}
} catch (Exception e) {
logger.warn("An error occurred when reading css-rules: " + e.getMessage(), e);
}
return list;
}Example 6
| Project: Wol-master File: CSSAssistProcessor.java View source code |
/**
* Parse CSS and create completion informations.
*
* @param css CSS
*/
private void processStylesheet(String css) {
try {
CSSOMParser parser = new CSSOMParser();
InputSource is = new InputSource(new StringReader(css));
CSSStyleSheet stylesheet = parser.parseStyleSheet(is);
CSSRuleList list = stylesheet.getCssRules();
// ArrayList assists = new ArrayList();
for (int i = 0; i < list.getLength(); i++) {
CSSRule rule = list.item(i);
if (rule instanceof CSSStyleRule) {
CSSStyleRule styleRule = (CSSStyleRule) rule;
String selector = styleRule.getSelectorText();
SelectorList selectors = parser.parseSelectors(new InputSource(new StringReader(selector)));
for (int j = 0; j < selectors.getLength(); j++) {
Selector sel = selectors.item(j);
if (sel instanceof ConditionalSelector) {
Condition cond = ((ConditionalSelector) sel).getCondition();
SimpleSelector simple = ((ConditionalSelector) sel).getSimpleSelector();
if (simple instanceof ElementSelector) {
String tagName = ((ElementSelector) simple).getLocalName();
if (tagName == null) {
tagName = "*";
} else {
tagName = tagName.toLowerCase();
}
if (cond instanceof AttributeCondition) {
AttributeCondition attrCond = (AttributeCondition) cond;
if (_rules.get(tagName) == null) {
ArrayList<String> classes = new ArrayList<String>();
// classes.add(new AssistInfo(attrCond.getValue()));
classes.add(attrCond.getValue());
_rules.put(tagName, classes);
} else {
ArrayList<String> classes = _rules.get(tagName);
// classes.add(new AssistInfo(attrCond.getValue()));
classes.add(attrCond.getValue());
}
}
}
}
}
}
}
} catch (Throwable ex) {
}
}Example 7
| Project: Loboevolution-master File: AbstractCSS2Properties.java View source code |
/*
* (non-Javadoc)
*
* @see
* org.lobobrowser.html.style.AbstractCSS2Properties.SubPropertySetter#
* changeValue (org.lobobrowser.html.style.AbstractCSS2Properties,
* java.lang.String, org.w3c.dom.css.CSSStyleDeclaration, boolean)
*/
@Override
public void changeValue(AbstractCSS2Properties properties, String newValue, CSSStyleDeclaration declaration, boolean important) {
String baseHref = null;
String finalValue;
if (declaration != null) {
CSSRule rule = declaration.getParentRule();
if (rule != null) {
CSSStyleSheet sheet = rule.getParentStyleSheet();
if (sheet instanceof com.steadystate.css.dom.CSSStyleSheetImpl) {
com.steadystate.css.dom.CSSStyleSheetImpl ssheet = (com.steadystate.css.dom.CSSStyleSheetImpl) sheet;
baseHref = ssheet.getHref();
}
}
}
if (baseHref == null) {
baseHref = properties.context.getDocumentBaseURI();
}
String start = "url(";
if ((newValue == null) || !newValue.toLowerCase().startsWith(start)) {
finalValue = newValue;
} else {
int startIdx = start.length();
int closingIdx = newValue.lastIndexOf(')');
if (closingIdx == -1) {
finalValue = newValue;
} else {
String quotedUri = newValue.substring(startIdx, closingIdx);
String tentativeUri = HtmlValues.unquoteAndUnescape(quotedUri);
if (baseHref == null) {
finalValue = newValue;
} else {
try {
URL styleUrl = Urls.createURL(null, baseHref);
finalValue = "url(" + HtmlValues.quoteAndEscape(Urls.createURL(styleUrl, tentativeUri).toExternalForm()) + ")";
} catch (MalformedURLExceptionUnsupportedEncodingException | mfu) {
logger.error("Unable to create URL for URI=[" + tentativeUri + "], with base=[" + baseHref + "].", mfu);
finalValue = newValue;
}
}
}
}
properties.setPropertyValueLCAlt(BACKGROUND_IMAGE, finalValue, important);
}Example 8
| Project: eclipse-themes-master File: RewriteCustomTheme.java View source code |
/**
* find and returns a {@link StyleSheet} which represent
* "jeeeyul-custom.css".
*
* @param documentCSS
* @return
* @since 1.2
*/
private StyleSheet findCustomThemeSheet(DocumentCSS documentCSS) {
StyleSheet customThemeSheet = null;
StyleSheetList styleSheets = documentCSS.getStyleSheets();
SearchCustomThemeSheet: for (int i = 0; i < styleSheets.getLength(); i++) {
StyleSheet eachSheet = styleSheets.item(i);
if (eachSheet instanceof CSSStyleSheet) {
CSSRuleList cssRules = ((CSSStyleSheet) eachSheet).getCssRules();
for (int j = 0; j < cssRules.getLength(); j++) {
ExtendedCSSRule rule = (ExtendedCSSRule) cssRules.item(j);
SelectorList selectorList = rule.getSelectorList();
String selectorText = selectorList.item(0).toString();
if (CUSTOM_THEME_FIRST_SELECTOR.equals(selectorText)) {
customThemeSheet = eachSheet;
break SearchCustomThemeSheet;
}
}
}
}
return customThemeSheet;
}Example 9
| Project: beowulf-master File: HTMLLinkElementImpl.java View source code |
/**
* If the LINK refers to a stylesheet document, this method loads and parses
* it.
*/
protected void processLink() {
this.styleSheet = null;
// String rel = this.getAttribute("rel");
/*
* if(rel != null) { String cleanRel = rel.trim().toLowerCase(); boolean
* isStyleSheet = cleanRel.equals("stylesheet"); boolean isAltStyleSheet
* = cleanRel.equals("alternate stylesheet"); if(isStyleSheet ||
* isAltStyleSheet) { UserAgentContext uacontext =
* this.getUserAgentContext(); if(uacontext.isExternalCSSEnabled()) {
* String media = this.getMedia(); if(CSSUtilities.matchesMedia(media,
* uacontext)) { HTMLDocumentImpl doc = (HTMLDocumentImpl)
* this.getOwnerDocument(); try { boolean liflag = loggableInfo; long
* time1 = liflag ? System.currentTimeMillis() : 0; try { CSSStyleSheet
* sheet = CSSUtilities.parse(this, this.getHref(), doc,
* doc.getBaseURI(), false); if(sheet != null) { this.styleSheet =
* sheet; if(sheet instanceof CSSStyleSheetImpl) { CSSStyleSheetImpl
* sheetImpl = (CSSStyleSheetImpl) sheet; if(isAltStyleSheet) {
* sheetImpl.setDisabledOnly(true); } else {
* sheetImpl.setDisabledOnly(this.disabled); } } else {
* if(isAltStyleSheet) { sheet.setDisabled(true); } else {
* sheet.setDisabled(this.disabled); } } doc.addStyleSheet(sheet); } }
* finally { if(liflag) { long time2 = System.currentTimeMillis();
* logger
* .info("processLink(): Loaded and parsed CSS (or attempted to) at URI=["
* + this.getHref() + "] in " + (time2 - time1) + " ms."); } }
*
* } catch(MalformedURLException mfe) {
* this.warn("Will not parse CSS. URI=[" + this.getHref() +
* "] with BaseURI=[" + doc.getBaseURI() +
* "] does not appear to be a valid URI."); } catch(Throwable err) {
* this.warn("Unable to parse CSS. URI=[" + this.getHref() + "].", err);
* } } } } }
*/
}Example 10
| Project: nmedit-master File: CSSUtils.java View source code |
public static CSSStyleDeclaration getStyleDeclaration(String cssClassName, Element e, StorageContext context) {
CSSStyleSheet css = context.getStyleSheet();
CSSStyleRule rule = context.getStyleRule(cssClassName);
CSSStyleDeclarationImpl decl = (CSSStyleDeclarationImpl) parseStyleDeclaration(e, css);
if (decl != null) {
if (rule != null)
decl.setParentRule(rule);
return decl;
}
if (rule != null)
return rule.getStyle();
return null;
}Example 11
| Project: richfaces-master File: CompiledCSSResource.java View source code |
@Override
public InputStream getInputStream() throws IOException {
FacesContext ctx = FacesContext.getCurrentInstance();
InputStream stream = null;
CSSStyleSheet styleSheet = null;
try {
stream = getResourceInputStream();
if (null == stream) {
return null;
}
InputSource source = new InputSource(new InputStreamReader(stream));
CSSOMParser parser = new CSSOMParser(new SACParserCSS3());
ErrorHandlerImpl errorHandler = new ErrorHandlerImpl(this, ctx.isProjectStage(ProjectStage.Production));
parser.setErrorHandler(errorHandler);
// parse and create a stylesheet composition
styleSheet = parser.parseStyleSheet(source, null, null);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
LOGGER.warn(e.getMessage(), e);
}
}
}
if (styleSheet != null) {
// TODO nick - handle encoding
String encoding = ctx.getExternalContext().getResponseCharacterEncoding();
CSSVisitorImpl cssVisitor = new CSSVisitorImpl(ctx);
cssVisitor.setEncoding(encoding != null ? encoding : "UTF-8");
cssVisitor.visitStyleSheet(styleSheet);
String cssText = cssVisitor.getCSSText();
return new ByteArrayInputStream(cssText.getBytes(cssVisitor.getEncoding()));
} else {
if (!ctx.isProjectStage(ProjectStage.Production)) {
LOGGER.info(MessageFormat.format(NULL_STYLESHEET, getLibraryName(), getResourceName()));
}
return null;
}
}Example 12
| Project: gngr-master File: HTMLDocumentImpl.java View source code |
public void notifyStyleSheetChanged(final CSSStyleSheet styleSheet) {
final Node ownerNode = styleSheet.getOwnerNode();
if (ownerNode != null) {
final boolean disabled = styleSheet.getDisabled();
if (ownerNode instanceof HTMLStyleElementImpl) {
final HTMLStyleElementImpl htmlStyleElement = (HTMLStyleElementImpl) ownerNode;
if (htmlStyleElement.getDisabled() != disabled) {
htmlStyleElement.setDisabledImpl(disabled);
}
} else if (ownerNode instanceof HTMLLinkElementImpl) {
final HTMLLinkElementImpl htmlLinkElement = (HTMLLinkElementImpl) ownerNode;
if (htmlLinkElement.getDisabled() != disabled) {
htmlLinkElement.setDisabledImpl(disabled);
}
}
}
invalidateStyles();
allInvalidated();
}Example 13
| Project: qrone-master File: CSS3OM.java View source code |
public CSSStyleSheet getStyleSheet() {
return stylesheet;
}Example 14
| Project: jStyleDomBridge-master File: AbstractCSSRule.java View source code |
/**
* @return The style sheet that contains this rule.
*/
public CSSStyleSheet getParentStyleSheet() {
return containingStyleSheet;
}Example 15
| Project: batik-master File: SVGDOMImplementation.java View source code |
// DOMImplementationCSS /////////////////////////////////////////////////
/**
* <b>DOM</b>: Implements {@link
* org.w3c.dom.css.DOMImplementationCSS#createCSSStyleSheet(String,String)}.
*/
public CSSStyleSheet createCSSStyleSheet(String title, String media) {
throw new UnsupportedOperationException(// XXX
"DOMImplementationCSS.createCSSStyleSheet is not implemented");
}