/** * Appends parsed source to passed style sheet. This style sheet must be * IMPERATIVELY parsed by this factory to guarantee proper appending. Uses * no inline element * * @param source * Source, interpretation depends on {@code type} * @param type * Type of source provided * @param base * Base url * @param sheet * StyleSheet to be modified * @return Modified StyleSheet * @throws IOException * When problem with input stream occurs * @throws CSSException * When unrecoverable exception during parsing occurs * @throws IllegalArgumentException * When type of source is INLINE */ public StyleSheet append(Object source, NetworkProcessor network, String encoding, SourceType type, StyleSheet sheet, URL base) throws IOException, CSSException { if (type == SourceType.INLINE) throw new IllegalArgumentException( "Missing element for INLINE input"); return append(source, network, encoding, type, null, false, sheet, base); }
/** * Parses text into StyleSheet * * @param css * Text with CSS declarations * @return Parsed StyleSheet * @throws IOException * When exception during read occurs * @throws CSSException * When exception during parse occurs */ public static final StyleSheet parse(String css) throws IOException, CSSException { return CSSParserFactory.parse(css, null, SourceType.EMBEDDED, null); }
private static CSSTreeParser createTreeParser(Object source, NetworkProcessor network, String encoding, SourceType type, Preparator preparator, URL base, List<MediaQuery> media) throws IOException, CSSException { CSSInputStream input = getInput(source, network, encoding, type); input.setBase(base); CommonTokenStream tokens = feedLexer(input); CommonTree ast = feedParser(tokens, type); return feedAST(tokens, ast, preparator, media); }
private static CSSTreeParser createParser(Object source, String encoding, SourceType type, Preparator preparator, StyleSheet stylesheet, URL base) throws IOException, CSSException { CSSInputStream input = type.getInput(source, encoding); input.setBase(base); CommonTokenStream tokens = feedLexer(input, stylesheet); CommonTree ast = feedParser(tokens, type, stylesheet); return feedAST(tokens, ast, preparator, stylesheet); }
throws CSSException, IOException CSSTreeParser parser = createTreeParser(source, network, encoding, type, preparator, base, media); parse(parser, type); parseAndImport(url, network, encoding, SourceType.URL, sheet, preparator, url, imedia); } catch (IOException e) { log.warn("Couldn't read imported style sheet: {}", e.getMessage()); return addRulesToStyleSheet(parser.getRules(), sheet);
Preparator preparator = new SimplePreparator(ps, inline, inlinePriority); CSSTreeParser parser = createParser(source, encoding, type, preparator, sheet, base); StyleSheet ret = type.parse(parser); lastPriority = ret.getLastMark();
public static CSSParserFactory getInstance() { if(instance == null) instance = new CSSParserFactory(); return instance; }
/** * Parses source of given type * * @param source * Source, interpretation depends on {@code type} * @param type * Type of source provided * @param inline * InlineElement * @param inlinePriority * True when the rule should have an 'inline' (greater) priority * @return Created StyleSheet * @throws IOException * When problem with input stream occurs * @throws CSSException * When unrecoverable exception during parsing occurs */ public static StyleSheet parse(Object source, String encoding, SourceType type, Element inline, boolean inlinePriority, URL base) throws IOException, CSSException { StyleSheet sheet = (StyleSheet) CSSFactory.getRuleFactory() .createStyleSheet().unlock(); PriorityStrategy ps = new AtomicPriorityStrategy(lastPriority); Preparator preparator = new SimplePreparator(ps, inline, inlinePriority); CSSTreeParser parser = createParser(source, encoding, type, preparator, sheet, base); StyleSheet ret = type.parse(parser); lastPriority = ret.getLastMark(); return ret; }
/** * Parses URL into StyleSheet * * @param url * URL of file to be parsed * @param encoding * Encoding of file * @return Parsed StyleSheet * @throws CSSException * When exception during parse occurs * @throws IOException * When file not found */ public static final StyleSheet parse(URL url, String encoding) throws CSSException, IOException { return CSSParserFactory.parse((Object) url, encoding, SourceType.URL, url); }
/** * Appends parsed source to passed style sheet. This style sheet must be * IMPERATIVELY parsed by this factory to guarantee proper appending. Uses * no inline element * * @param source * Source, interpretation depends on {@code type} * @param type * Type of source provided * @param base * Base url * @param sheet * StyleSheet to be modified * @return Modified StyleSheet * @throws IOException * When problem with input stream occurs * @throws CSSException * When unrecoverable exception during parsing occurs * @throws IllegalArgumentException * When type of source is INLINE */ public static StyleSheet append(Object source, String encoding, SourceType type, StyleSheet sheet, URL base) throws IOException, CSSException { if (type == SourceType.INLINE) throw new IllegalArgumentException( "Missing element for INLINE input"); return append(source, encoding, type, null, false, sheet, base); }
/** * Parses source of given type. Uses no element. * * @param source * Source, interpretation depends on {@code type} * @param type * Type of source provided * @param base * The base URL * @return Created StyleSheet * @throws IOException * When problem with input stream occurs * @throws CSSException * When unrecoverable exception during parsing occurs * @throws IllegalArgumentException * When type of source is INLINE */ public StyleSheet parse(Object source, NetworkProcessor network, String encoding, SourceType type, URL base) throws IOException, CSSException { if (type == SourceType.INLINE) throw new IllegalArgumentException( "Missing element for INLINE input"); return parse(source, network, encoding, type, null, false, base); }
result = CSSParserFactory.append(extractElementText(elem), null, SourceType.EMBEDDED, result, base); log.debug("Matched embedded CSS style"); result = CSSParserFactory.append(uri, encoding, SourceType.URL, result, uri); log.debug("Matched linked CSS style"); result = CSSParserFactory.append( elem.getAttribute("style"), null, SourceType.INLINE, elem, true, result, base); result = CSSParserFactory.append( elem.getAttribute("XDefaultStyle"), null, SourceType.INLINE, elem, false, result, base);
/** * Parses source of given type. Uses no element. * * @param source * Source, interpretation depends on {@code type} * @param type * Type of source provided * @param base * The base URL * @return Created StyleSheet * @throws IOException * When problem with input stream occurs * @throws CSSException * When unrecoverable exception during parsing occurs * @throws IllegalArgumentException * When type of source is INLINE */ public static StyleSheet parse(Object source, String encoding, SourceType type, URL base) throws IOException, CSSException { if (type == SourceType.INLINE) throw new IllegalArgumentException( "Missing element for INLINE input"); return parse(source, encoding, type, null, false, base); }
result = pf.append(extractElementText(elem), network, null, SourceType.EMBEDDED, result, base); log.debug("Matched embedded CSS style"); result = pf.append(uri, network, encoding, SourceType.URL, result, uri); log.debug("Matched linked CSS style"); result = pf.append( elem.getAttribute("style"), network, null, SourceType.INLINE, result = pf.append( elem.getAttribute("XDefaultStyle"), network, null, SourceType.INLINE,
/** * Parses URL into StyleSheet * * @param url * URL of file to be parsed * @param network * Network processor used for handling the URL connections * @param encoding * Encoding of file * @return Parsed StyleSheet * @throws CSSException * When exception during parse occurs * @throws IOException * When file not found */ public static final StyleSheet parse(URL url, NetworkProcessor network, String encoding) throws CSSException, IOException { return getCSSParserFactory().parse((Object) url, network, encoding, SourceType.URL, url); }
/** * Parses text into a StyleSheet * * @param css * Text with CSS declarations * @param base * The URL to be used as a base for loading external resources. Base URL may * be {@code null} if there are no external resources in the CSS string * referenced by relative URLs. * @param network * Network processor for retrieving the URL resources * @return Parsed StyleSheet * @throws IOException * When exception during read occurs * @throws CSSException * When exception during parse occurs */ public static final StyleSheet parseString(String css, URL base, NetworkProcessor network) throws IOException, CSSException { URL baseurl = base; if (baseurl == null) baseurl = new URL("file:///base/url/is/not/specified"); //prevent errors if there are still some relative URLs used return getCSSParserFactory().parse(css, network, null, SourceType.EMBEDDED, baseurl); }
public void analyse(Collection<URI> sheetURIs, Collection<String> embeddedCSS, URI embedContainerURI, NetworkProcessor network) throws IOException, URISyntaxException, CSSException { if (!SupportedCSS.isSupportedMedia(Medium)) { throw new IllegalStateException("medium '" + Medium + "' is not supported"); } List<URI> alluris = new ArrayList<URI>(); List<String> csscode = new ArrayList<String>(embeddedCSS); for (int k = 0; k < embeddedCSS.size(); ++k) alluris.add(embedContainerURI); for (URI uri : sheetURIs) { if (uri != null) { alluris.add(uri); csscode.add(IOUtils.toString(uri.toURL().openStream(), "UTF-8")); } } List<StyleSheet> styleSheets = new ArrayList<StyleSheet>(); for (int k = 0; k < csscode.size(); ++k) { String basePath = alluris.get(k).resolve(".").toString(); String withAbsURL = makeURLabsolute(csscode.get(k), basePath); styleSheets.add(parserFactory.parse(withAbsURL, network, null, SourceType.EMBEDDED, new URL("http://base"))); /* * we cannot use CSSFactory.parse(withAbsURL) because it tries to * convert the null base URL into a String */ } mAnalyzer = new Analyzer(styleSheets); }
/** * Parses text into StyleSheet * * @param css * Text with CSS declarations * @return Parsed StyleSheet * @throws IOException * When exception during read occurs * @throws CSSException * When exception during parse occurs * @deprecated * This function does not specify the base URL. {@link #parseString(String, URL)} * should be used instead. */ @Deprecated public static final StyleSheet parse(String css) throws IOException, CSSException { URL base = new URL("file:///base/url/is/not/specified"); //Cannot determine the base URI in this method but we need some base URI for relative URLs return getCSSParserFactory().parse(css, new DefaultNetworkProcessor(), null, SourceType.EMBEDDED, base); }