org.w3c.css.sac.InputSource Java Examples

The following examples show how to use org.w3c.css.sac.InputSource. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CssParser.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Parses a CSS resource and get the CSSStyleSheet as the output.
 * 
 * @param source
 *            the source of the CSS resource
 * @return the CSSStyleSheet if succeed
 * @throws IOException
 *             if the resource is not well-located
 */

public CSSStyleSheet parseStyleSheet( InputSource source )
		throws IOException
{
	CssHandler handler = new CssHandler( );
	parser.setDocumentHandler( handler );
	parser.setErrorHandler( errorHandler );
	try 
	{
		parser.parseStyleSheet( source );
	}
	catch ( StringIndexOutOfBoundsException e ) 
	{
		throw new CSSException( CSSException.SAC_SYNTAX_ERR );
	}
	return (StyleSheet) handler.getRoot( );
}
 
Example #2
Source File: Parser.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Convert the source into a Reader. Used only by DOM Level 2 parser methods.
 */
private Reader getReader( InputSource source ) throws IOException {
  if ( source.getCharacterStream() != null ) {
    return source.getCharacterStream();
  } else if ( source.getByteStream() != null ) {
    // My DOM level 2 implementation doesn't use this case.
    if ( source.getEncoding() == null ) {
      // unknown encoding, use ASCII as default.
      return new InputStreamReader( source.getByteStream(), "ASCII" );
    } else {
      return new InputStreamReader( source.getByteStream(),
        source.getEncoding() );
    }
  } else {
    // systemId
    // @@TODO
    throw new CSSException( "not yet implemented" );
  }
}
 
Example #3
Source File: CSSEngine.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Parses and creates a style declaration.
 * 
 * @param value
 *            The style declaration text.
 */
public CSSStyleDeclaration parseStyleDeclaration( String value )
{
	styleDeclarationBuilder.styleDeclaration = new StyleDeclaration( this );
	try
	{
		parser.setDocumentHandler( styleDeclarationBuilder );
		parser.parseStyleDeclaration( new InputSource( new StringReader(
				value ) ) );
	}
	catch ( Exception e )
	{
		/** @todo: logout the error message */
	}
	return styleDeclarationBuilder.styleDeclaration;
}
 
Example #4
Source File: CssParseTest.java    From compiler with Apache License 2.0 6 votes vote down vote up
@Test
public void parseTest(){
//	InputSource source = new InputSource(new StringReader("h1 { background: #ffcc44; }"));
//	CSSOMParser parser = new CSSOMParser(new SACParserCSS3());
//	CSSStyleSheet sheet = parser.parseStyleSheet(source, null, null);
	
	com.steadystate.css.parser.CSSOMParser parser = new com.steadystate.css.parser.CSSOMParser();
	String content = FileIO.readFileContents(new File("test/datagen/Css/ParseTest.css"));
	InputSource source = new InputSource(new StringReader(content));
	try {
		com.steadystate.css.dom.CSSStyleSheetImpl sSheet = (CSSStyleSheetImpl) parser.parseStyleSheet(source, null, null);
		CssVisitor visitor = new CssVisitor();
		boa.types.Ast.Element document = visitor.getStyleSheet(sSheet);
		System.out.println(document.toString());
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
	
}
 
Example #5
Source File: StyleParser.java    From svgtoandroid with MIT License 6 votes vote down vote up
private void init() {
    if (style != null) {
        String styleContent = style.getValue().getText();
        if (styleContent != null && !styleContent.isEmpty()) {
            InputSource source = new InputSource(new StringReader(styleContent));
            CSSOMParser parser = new CSSOMParser(new SACParserCSS3());
            parser.setErrorHandler(new ParserErrorHandler());
            try {
                styleSheet = parser.parseStyleSheet(source, null, null);
                cssFormat = new CSSFormat().setRgbAsHex(true);

                CSSRuleList rules = styleSheet.getCssRules();
                for (int i = 0; i < rules.getLength(); i++) {
                    final CSSRule rule = rules.item(i);
                    if (rule instanceof CSSStyleRuleImpl) {
                        styleRuleMap.put(((CSSStyleRuleImpl) rule).getSelectorText(), (CSSStyleRuleImpl) rule);
                    }
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
 
Example #6
Source File: FeedUtils.java    From commafeed with Apache License 2.0 6 votes vote down vote up
public static String escapeImgCss(String orig) {
	String rule = "";
	CSSOMParser parser = new CSSOMParser();
	try {
		List<String> rules = new ArrayList<>();
		CSSStyleDeclaration decl = parser.parseStyleDeclaration(new InputSource(new StringReader(orig)));

		for (int i = 0; i < decl.getLength(); i++) {
			String property = decl.item(i);
			String value = decl.getPropertyValue(property);
			if (StringUtils.isBlank(property) || StringUtils.isBlank(value)) {
				continue;
			}

			if (ALLOWED_IMG_CSS_RULES.contains(property) && StringUtils.containsNone(value, FORBIDDEN_CSS_RULE_CHARACTERS)) {
				rules.add(property + ":" + decl.getPropertyValue(property) + ";");
			}
		}
		rule = StringUtils.join(rules, "");
	} catch (Exception e) {
		log.error(e.getMessage(), e);
	}
	return rule;
}
 
Example #7
Source File: FeedUtils.java    From commafeed with Apache License 2.0 6 votes vote down vote up
public static String escapeIFrameCss(String orig) {
	String rule = "";
	CSSOMParser parser = new CSSOMParser();
	try {
		List<String> rules = new ArrayList<>();
		CSSStyleDeclaration decl = parser.parseStyleDeclaration(new InputSource(new StringReader(orig)));

		for (int i = 0; i < decl.getLength(); i++) {
			String property = decl.item(i);
			String value = decl.getPropertyValue(property);
			if (StringUtils.isBlank(property) || StringUtils.isBlank(value)) {
				continue;
			}

			if (ALLOWED_IFRAME_CSS_RULES.contains(property) && StringUtils.containsNone(value, FORBIDDEN_CSS_RULE_CHARACTERS)) {
				rules.add(property + ":" + decl.getPropertyValue(property) + ";");
			}
		}
		rule = StringUtils.join(rules, "");
	} catch (Exception e) {
		log.error(e.getMessage(), e);
	}
	return rule;
}
 
Example #8
Source File: StyleSheetHandler.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Receive notification of the beginning of a style sheet.
 * <p/>
 * The CSS parser will invoke this method only once, before any other methods in this interface.
 *
 * @param source the input source
 * @throws CSSException Any CSS exception, possibly wrapping another exception.
 */
public void startDocument( InputSource source )
  throws CSSException {
  initParseContext( source );
  if ( this.styleSheet == null ) {
    this.styleSheet = new StyleSheet();
    this.styleSheet.setSource( getSource() );
  }
}
 
Example #9
Source File: Parser.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public boolean parsePriority( InputSource source )
  throws CSSException, IOException {
  this.source = source;
  ReInit( getCharStreamWithLurk( source ) );

  return prio();
}
 
Example #10
Source File: Parser.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public LexicalUnit parsePropertyValue( InputSource source )
  throws CSSException, IOException {
  this.source = source;
  ReInit( getCharStreamWithLurk( source ) );

  return expr();
}
 
Example #11
Source File: Parser.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public String parseNamespaceToken( InputSource source )
  throws CSSException, IOException {
  this.source = source;
  ReInit( getCharStreamWithLurk( source ) );

  if ( selectorFactory == null ) {
    selectorFactory = new SelectorFactoryImpl();
  }
  if ( conditionFactory == null ) {
    conditionFactory = new ConditionFactoryImpl();
  }
  return _parseNamespaceToken();
}
 
Example #12
Source File: Parser.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public SelectorList parseSelectors( InputSource source )
  throws CSSException, IOException {
  this.source = source;
  ReInit( getCharStreamWithLurk( source ) );

  if ( selectorFactory == null ) {
    selectorFactory = new SelectorFactoryImpl();
  }
  if ( conditionFactory == null ) {
    conditionFactory = new ConditionFactoryImpl();
  }
  return _parseSelectors();
}
 
Example #13
Source File: Parser.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void parseMediaRule( InputSource source )
  throws CSSException, IOException {
  this.source = source;
  ReInit( getCharStreamWithLurk( source ) );

  if ( selectorFactory == null ) {
    selectorFactory = new SelectorFactoryImpl();
  }
  if ( conditionFactory == null ) {
    conditionFactory = new ConditionFactoryImpl();
  }
  _parseMediaRule();
}
 
Example #14
Source File: Parser.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Parse methods used by DOM Level 2 implementation.
 */
public void parseImportRule( InputSource source )
  throws CSSException, IOException {
  this.source = source;
  ReInit( getCharStreamWithLurk( source ) );

  if ( selectorFactory == null ) {
    selectorFactory = new SelectorFactoryImpl();
  }
  if ( conditionFactory == null ) {
    conditionFactory = new ConditionFactoryImpl();
  }
  _parseImportRule();
}
 
Example #15
Source File: Parser.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * This method parses a style declaration (including the surrounding curly braces).
 *
 * @param source the source of the style declaration.
 * @throws IOException  the source can't be parsed.
 * @throws CSSException the source is not CSS valid.
 */
public void parseStyleDeclaration( InputSource source )
  throws CSSException, IOException {
  this.source = source;
  ReInit( getCharStreamWithLurk( source ) );

  if ( selectorFactory == null ) {
    selectorFactory = new SelectorFactoryImpl();
  }
  if ( conditionFactory == null ) {
    conditionFactory = new ConditionFactoryImpl();
  }
  _parseDeclarationBlock();
}
 
Example #16
Source File: Parser.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * This method parses only one rule (style rule or at-rule, except @charset).
 *
 * @param source the source of the rule.
 * @throws IOException  the source can't be parsed.
 * @throws CSSException the source is not CSS valid.
 */
public void parseRule( InputSource source )
  throws CSSException, IOException {
  this.source = source;
  ReInit( getCharStreamWithLurk( source ) );

  if ( selectorFactory == null ) {
    selectorFactory = new SelectorFactoryImpl();
  }
  if ( conditionFactory == null ) {
    conditionFactory = new ConditionFactoryImpl();
  }
  _parseRule();
}
 
Example #17
Source File: StyleSheetHandler.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void initParseContext( InputSource source ) {
  // the default namespace might be fed from outside ..
  final CSSParserContext parserContext = CSSParserContext.getContext();
  parserContext.setNamespaces( namespaces );
  parserContext.setStyleKeyRegistry( styleKeyRegistry );
  parserContext.setSource( getSource() );
}
 
Example #18
Source File: Parser.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Main parse methods
 *
 * @param source the source of the style sheet.
 * @throws IOException  the source can't be parsed.
 * @throws CSSException the source is not CSS valid.
 */
public void parseStyleSheet( InputSource source )
  throws CSSException, IOException {
  this.source = source;
  ReInit( getCharStreamWithLurk( source ) );
  if ( selectorFactory == null ) {
    selectorFactory = new SelectorFactoryImpl();
  }
  if ( conditionFactory == null ) {
    conditionFactory = new ConditionFactoryImpl();
  }

  parserUnit();
}
 
Example #19
Source File: CSSParser.java    From tm4e with Eclipse Public License 1.0 5 votes vote down vote up
public CSSParser(InputSource source, Parser parser) throws CSSException, IOException {
	this.handler = new CSSDocumentHandler();
	parser.setDocumentHandler(handler);
	parser.setConditionFactory(CSSConditionFactory.INSTANCE);
	parser.setSelectorFactory(CSSSelectorFactory.INSTANCE);
	parser.parseStyleSheet(source);
}
 
Example #20
Source File: CssParser.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
public void startDocument( InputSource source ) throws CSSException
{
	if ( nodeStack.empty( ) )
	{
		StyleSheet ss = new StyleSheet( );

		nodeStack.push( ss );
	}
	else
	{
		// Error
	}
}
 
Example #21
Source File: StyleSheetHandler.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Receive notification of the end of a document.
 * <p/>
 * The CSS parser will invoke this method only once, and it will be the last method invoked during the parse. The
 * parser shall not invoke this method until it has either abandoned parsing (because of an unrecoverable error) or
 * reached the end of input.
 *
 * @param source the input source
 * @throws CSSException Any CSS exception, possibly wrapping another exception.
 */
public void endDocument( InputSource source )
  throws CSSException {
  final Iterator entries = namespaces.entrySet().iterator();
  while ( entries.hasNext() ) {
    final Map.Entry entry = (Map.Entry) entries.next();
    final String prefix = (String) entry.getKey();
    final String uri = (String) entry.getValue();
    styleSheet.addNamespace( prefix, uri );
  }
}
 
Example #22
Source File: CssBaseTest.java    From compiler with Apache License 2.0 5 votes vote down vote up
private String parseCSS(String content) throws Exception {
	com.steadystate.css.parser.CSSOMParser parser = new com.steadystate.css.parser.CSSOMParser();
	InputSource source = new InputSource(new StringReader(content));
	com.steadystate.css.dom.CSSStyleSheetImpl sSheet = (CSSStyleSheetImpl) parser.parseStyleSheet(source, null, null);
	CssVisitor visitor = new CssVisitor();
	Element sheet = visitor.getStyleSheet(sSheet);
	return sheet.toString();
}
 
Example #23
Source File: StyleSheetParserUtil.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Parses a style value. If the style value is a compound key, the corresonding style entries will be added to the
 * style rule.
 *
 * @param namespaces an optional map of known namespaces (prefix -> uri)
 * @param name       the stylekey-name to which the value should be assigned.
 * @param value      the value text
 * @param baseURL    an optional base url
 * @param baseRule   an optional base-rule to which the result gets added.
 * @return the CSS-Style-Rule that contains all values for the given text.
 */
public CSSStyleRule parseStyles( final Map namespaces,
                                 final String name,
                                 final String value,
                                 final ResourceKey baseURL,
                                 final CSSDeclarationRule baseRule,
                                 final ResourceManager resourceManager,
                                 final StyleKeyRegistry styleKeyRegistry ) {
  if ( name == null ) {
    throw new NullPointerException( "Name is null" );
  }
  if ( value == null ) {
    throw new NullPointerException( "Value is null" );
  }

  try {
    final Parser parser = getParser();
    synchronized( parser ) {
      final StyleSheetHandler handler = new StyleSheetHandler();
      handler.init( styleKeyRegistry, resourceManager, baseURL, -1, null );

      setupNamespaces( namespaces, handler );
      final InputSource source = new InputSource();
      source.setCharacterStream( new StringReader( value ) );

      handler.initParseContext( source );
      handler.setStyleRule( baseRule );
      parser.setDocumentHandler( handler );
      final LexicalUnit lu = parser.parsePropertyValue( source );
      handler.property( name, lu, false );
      final CSSStyleRule rule = (CSSStyleRule) handler.getStyleRule();

      CSSParserContext.getContext().destroy();
      return rule;
    }
  } catch ( Exception e ) {
    e.printStackTrace();
    return null;
  }
}
 
Example #24
Source File: HtmlVisitor.java    From compiler with Apache License 2.0 5 votes vote down vote up
private boa.types.Ast.Element parseCss(String wholeData) {
	com.steadystate.css.parser.CSSOMParser parser = new com.steadystate.css.parser.CSSOMParser();
	InputSource source = new InputSource(new StringReader(wholeData));
	try {
		com.steadystate.css.dom.CSSStyleSheetImpl sSheet = (CSSStyleSheetImpl) parser.parseStyleSheet(source, null, null);
		CssVisitor visitor = new CssVisitor();
		return(visitor.getStyleSheet(sSheet));
	} catch (IOException e) {
		return null;
	}
}
 
Example #25
Source File: CssFormatter.java    From formatter-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Override
protected String doFormat(String code, LineEnding ending) throws IOException {

    InputSource source = new InputSource(new StringReader(code));
    CSSOMParser parser = new CSSOMParser(new SACParserCSS3());
    CSSStyleSheetImpl sheet = (CSSStyleSheetImpl) parser.parseStyleSheet(source, null, null);
    String formattedCode = sheet.getCssText(formatter);

    if (code.equals(formattedCode)) {
        return null;
    }
    return formattedCode;
}
 
Example #26
Source File: CSSDocumentHandler.java    From tm4e with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void startDocument(InputSource arg0) throws CSSException {
	// TODO Auto-generated method stub

}
 
Example #27
Source File: StyleSheetParserUtil.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Parses a single style value for the given key. Returns <code>null</code>, if the key denotes a compound definition,
 * which has no internal representation.
 *
 * @param namespaces an optional map of known namespaces (prefix -> uri)
 * @param key        the stylekey to which the value should be assigned.
 * @param value      the value text
 * @param baseURL    an optional base url
 * @return the parsed value or null, if the value was not valid.
 */
public CSSValue parseStyleValue( final Map namespaces,
                                 final StyleKey key,
                                 final String value,
                                 final ResourceKey baseURL,
                                 final ResourceManager resourceManager,
                                 final StyleKeyRegistry styleKeyRegistry ) {
  if ( key == null ) {
    throw new NullPointerException();
  }
  if ( value == null ) {
    throw new NullPointerException();
  }

  try {
    final Parser parser = getParser();
    synchronized( parser ) {
      final StyleSheetHandler handler = new StyleSheetHandler();
      setupNamespaces( namespaces, handler );

      handler.init( styleKeyRegistry, resourceManager, baseURL, -1, null );

      final InputSource source = new InputSource();
      source.setCharacterStream( new StringReader( value ) );

      handler.initParseContext( source );
      handler.setStyleRule( new CSSStyleRule( new StyleSheet(), null ) );
      parser.setDocumentHandler( handler );
      final LexicalUnit lu = parser.parsePropertyValue( source );
      handler.property( key.getName(), lu, false );
      final CSSStyleRule rule = (CSSStyleRule) handler.getStyleRule();

      CSSParserContext.getContext().destroy();

      return rule.getPropertyCSSValue( key );
    }
  } catch ( Exception e ) {
    e.printStackTrace();
    return null;
  }
}
 
Example #28
Source File: StyleSheetParserUtil.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Parses a style rule.
 *
 * @param namespaces an optional map of known namespaces (prefix -> uri)
 * @param styleText  the css text that should be parsed
 * @param baseURL    an optional base url
 * @param baseRule   an optional base-rule to which the result gets added.
 * @return the CSS-Style-Rule that contains all values for the given text.
 */
public CSSDeclarationRule parseStyleRule( final Map namespaces,
                                          final String styleText,
                                          final ResourceKey baseURL,
                                          final CSSDeclarationRule baseRule,
                                          final ResourceManager resourceManager,
                                          final StyleKeyRegistry styleKeyRegistry ) {
  if ( styleText == null ) {
    throw new NullPointerException( "Name is null" );
  }
  if ( resourceManager == null ) {
    throw new NullPointerException( "ResourceManager must not be null" );
  }
  if ( styleKeyRegistry == null ) {
    throw new NullPointerException( "Style-Key Registry must not be null" );
  }

  try {
    final Parser parser = getParser();
    synchronized( parser ) {
      final StyleSheetHandler handler = new StyleSheetHandler();
      setupNamespaces( namespaces, handler );
      handler.init( styleKeyRegistry, resourceManager, baseURL, -1, null );

      final InputSource source = new InputSource();
      source.setCharacterStream( new StringReader( styleText ) );

      handler.initParseContext( source );
      if ( baseRule != null ) {
        handler.setStyleRule( baseRule );
      } else {
        handler.setStyleRule( new CSSStyleRule( new StyleSheet(), null ) );
      }
      parser.setDocumentHandler( handler );
      parser.parseStyleDeclaration( source );
      final CSSDeclarationRule rule = handler.getStyleRule();
      CSSParserContext.getContext().destroy();
      return rule;
    }
  } catch ( Exception e ) {
    e.printStackTrace();
    return null;
  }
}
 
Example #29
Source File: CssParser.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
public void endDocument( InputSource source ) throws CSSException
{

	// Pop style sheet nodes
	root = nodeStack.pop( );
}
 
Example #30
Source File: CSSEngine.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * <b>SAC</b>: Implements {@link
 * DocumentHandler#endDocument(InputSource)}.
 */
public void endDocument( InputSource source ) throws CSSException
{
	throw new InternalError( );
}