cz.vutbr.web.css.TermURI Java Examples

The following examples show how to use cz.vutbr.web.css.TermURI. 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: HtmlUtils.java    From openemm with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Resolve the URI to an absolute.
 * @param termUri a rule to process.
 */
private static void resolveUri(TermURI termUri) {
    URL base = termUri.getBase();
    String value = termUri.getValue();

    if (base == null || base.getHost() == null) {
        logger.error("Base URL is required");
        return;
    }

    if (StringUtils.isEmpty(value)) {
        logger.error("Empty URI");
        return;
    }

    termUri.setValue(HttpUtils.resolveRelativeUri(base, value));
}
 
Example #2
Source File: TermURIImpl.java    From jStyleParser with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public TermURI setValue(String uri) {
    if(uri == null) 
        throw new IllegalArgumentException("Invalid uri for TermURI(null)");
    
    /* this shlould be done by parser
    uri = uri.replaceAll("^url\\(", "")
    	  .replaceAll("\\)$", "")
    	  .replaceAll("^'", "")
    	  .replaceAll("^\"", "")
    	  .replaceAll("'$", "")
    	  .replaceAll("\"$", "");
    */
    
    this.value = uri;
    return this;
}
 
Example #3
Source File: HtmlUtils.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Convert all the URIs (if any) to an absolute.
 * @param declaration a rules to process.
 */
private static void resolveUris(Declaration declaration) {
    for (Term<?> term : declaration) {
        if (term instanceof TermURI) {
            resolveUri((TermURI) term);
        }
    }
}
 
Example #4
Source File: GraphicsVisualContext.java    From CSSBox with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected String registerExternalFont(TermURI urlstring, String format)
        throws MalformedURLException, IOException
{
    String nameFound = null;
    if (format == null || FontDecoder.supportedFormats.contains(format))
    {
        URL url = DataURLHandler.createURL(urlstring.getBase(), urlstring.getValue());
        String regName = FontDecoder.findRegisteredFont(url);
        if (regName == null)
        {
            DocumentSource imgsrc = getViewport().getConfig().createDocumentSource(url);
            Font newFont;
            try {
                newFont = FontDecoder.decodeFont(imgsrc, format);
            } catch (FontFormatException e) {
                throw new IOException(e);
            }
            if (GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(newFont))
                log.debug("Registered font: {}", newFont.getFontName());
            else
                log.debug("Failed to register font: {} (not fatal, probably already existing)", newFont.getFontName());
            regName = newFont.getFontName();
            FontDecoder.registerFont(url, regName);
        }
        nameFound = regName;
    }
    return nameFound;
}
 
Example #5
Source File: ListItemBox.java    From CSSBox with GNU Lesser General Public License v3.0 5 votes vote down vote up
private ReplacedImage loadMarkerImage(NodeData style)
{
    CSSProperty.ListStyleImage img = style.getProperty("list-style-image");
    if (img == CSSProperty.ListStyleImage.uri)
    {
        TermURI urlstring = style.getValue(TermURI.class, "list-style-image");
        ReplacedImage bgimg = new ReplacedImage(this, ctx, urlstring.getBase(), urlstring.getValue());
        if (ctx.getConfig().getLoadBackgroundImages())
            bgimg.setImage(ctx.getImageLoader().loadImage(bgimg.getUrl()));
        return bgimg;
    }
    else
        return null;
}
 
Example #6
Source File: DeclarationTransformerImpl.java    From jStyleParser with GNU Lesser General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unused")
private boolean processCursor(Declaration d,
		Map<String, CSSProperty> properties, Map<String, Term<?>> values) {

	if (d.size() == 1 && Decoder.genericOneIdent(Cursor.class, d, properties)) {
		return true;
	} else {

		final Set<Cursor> allowedCursors = EnumSet.complementOf(EnumSet
				.of(Cursor.INHERIT));

		TermList list = tf.createList();
		Cursor cur = null;
		for (Term<?> term : d.asList()) {
			if (term instanceof TermURI) {
				list.add(term);
			} else if (term instanceof TermIdent
					&& (cur = Decoder.genericPropertyRaw(Cursor.class,
							allowedCursors, (TermIdent) term)) != null) {
				// this have to be the last cursor in sequence
				// and only one Decoder.generic cursor is allowed
				if (d.indexOf(term) != d.size() - 1)
					return false;

				// passed as last cursor, insert into properties and values
				properties.put("cursor", cur);
				if (!list.isEmpty())
					values.put("cursor", list);
				return true;
			} else
				return false;
		}
		return false;
	}
}
 
Example #7
Source File: DeclarationTransformerImpl.java    From jStyleParser with GNU Lesser General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unused")
private boolean processContent(Declaration d,
		Map<String, CSSProperty> properties, Map<String, Term<?>> values) {

	// content contains no explicit values
	if (d.size() == 1 && Decoder.genericOneIdent(Content.class, d, properties)) {
		return true;
	} else {

		// valid term idents
		final Set<String> validTermIdents = new HashSet<String>(Arrays
				.asList("open-quote", "close-quote", "no-open-quote",
						"no-close-quote"));

		TermList list = tf.createList();

		for (Term<?> t : d.asList()) {
			// one of valid terms
			if (t instanceof TermIdent
					&& validTermIdents.contains(((TermIdent) t).getValue()
							.toLowerCase()))
				list.add(t);
			else if (t instanceof TermString)
				list.add(t);
			else if (t instanceof TermURI)
				list.add(t);
			else if (t instanceof TermFunction.CounterFunction || t instanceof TermFunction.Attr)
				list.add(t);
			else
				return false;
		}
		// there is nothing in list after parsing
		if (list.isEmpty())
			return false;

		properties.put("content", Content.list_values);
		values.put("content", list);
		return true;
	}
}
 
Example #8
Source File: DeclarationTransformerImpl.java    From jStyleParser with GNU Lesser General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unused")
private boolean processFilter(Declaration d,
        Map<String, CSSProperty> properties, Map<String, Term<?>> values) {

    // single ident: none, or global ones
    if (d.size() == 1 && Decoder.genericOneIdent(Filter.class, d, properties)) {
        return true;
    } else {
        //list of uri() or <filter-function> expected
        TermList list = tf.createList();

        for (Term<?> t : d.asList()) {
            if (t instanceof TermFunction.FilterFunction)
                list.add(t);
            else if (t instanceof TermURI)
                list.add(t);
            else
                return false;
        }
        // there is nothing in list after parsing
        if (list.isEmpty())
            return false;

        properties.put("filter", Filter.list_values);
        values.put("filter", list);
        return true;
    }
}
 
Example #9
Source File: DeclarationTransformerImpl.java    From jStyleParser with GNU Lesser General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unused")
private boolean processBackdropFilter(Declaration d,
        Map<String, CSSProperty> properties, Map<String, Term<?>> values) {

    // single ident: none, or global ones
    if (d.size() == 1 && Decoder.genericOneIdent(BackdropFilter.class, d, properties)) {
        return true;
    } else {
        //list of uri() or <filter-function> expected
        TermList list = tf.createList();

        for (Term<?> t : d.asList()) {
            if (t instanceof TermFunction.FilterFunction)
                list.add(t);
            else if (t instanceof TermURI)
                list.add(t);
            else
                return false;
        }
        // there is nothing in list after parsing
        if (list.isEmpty())
            return false;

        properties.put("backdrop-filter", BackdropFilter.list_values);
        values.put("backdrop-filter", list);
        return true;
    }
}
 
Example #10
Source File: ListStyleVariator.java    From jStyleParser with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected boolean variant(int v, IntegerRef iteration,
        Map<String, CSSProperty> properties, Map<String, Term<?>> values) {

    // we won't use multivalue functionallity
    int i = iteration.get();

    switch (v) {
    case TYPE:
        // list style type
        return genericTermIdent(ListStyleType.class, terms.get(i),
                AVOID_INH, names.get(TYPE), properties);
    case POSITION:
        // list style position
        return genericTermIdent(ListStylePosition.class, terms.get(i),
                AVOID_INH, names.get(POSITION), properties);
    case IMAGE:
        // list style image
        return genericTermIdent(types.get(IMAGE), terms.get(i),
                AVOID_INH, names.get(IMAGE), properties)
                || genericTerm(TermURI.class, terms.get(i), names
                        .get(IMAGE), ListStyleImage.uri, ValueRange.ALLOW_ALL,
                        properties, values);
    default:
        return false;
    }
}
 
Example #11
Source File: SimpleTest.java    From jStyleParser with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testURI1() throws IOException, CSSException   {
	
	StyleSheet ss = CSSFactory.parseString(TEST_URI1, null);
	assertEquals("There is one rule", 1, ss.size());
	
	Declaration dec = (Declaration) ss.get(0).get(0);
	assertEquals("There is one declaration", 1, ss.get(0).size());
	
	Object term = dec.get(0);
	assertTrue("Term value is URI", term instanceof TermURI);
	
	TermURI uri = (TermURI) term;
	assertEquals("URI has proper value", "image.jpg", uri.getValue());
}
 
Example #12
Source File: TermURIImpl.java    From jStyleParser with GNU Lesser General Public License v3.0 4 votes vote down vote up
public TermURI setBase(URL base)
{
    this.base = base;
    return this;
}
 
Example #13
Source File: TermFactoryImpl.java    From jStyleParser with GNU Lesser General Public License v3.0 4 votes vote down vote up
public TermURI createURI(String value) {
	return (new TermURIImpl()).setValue(value);
}
 
Example #14
Source File: TermFactoryImpl.java    From jStyleParser with GNU Lesser General Public License v3.0 4 votes vote down vote up
public TermURI createURI(String value, URL base) {
    return (new TermURIImpl()).setValue(value).setBase(base);
}
 
Example #15
Source File: VisualContext.java    From CSSBox with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * Decodes and registers a new font based on its URL.
 * @param urlstring the external font URL
 * @param format the font format according to CSS spec (e.g. 'truetype')
 * @return The registed font name or {@code null} when failed
 * @throws MalformedURLException
 * @throws IOException
 */
protected abstract String registerExternalFont(TermURI urlstring, String format)
        throws MalformedURLException, IOException;