xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rob Davis-5 <t...@robertjdavis.co.uk>
Subject CODE to get elements affected by CSS attribute/value
Date Mon, 07 Apr 2008 09:31:08 GMT

Here's the code that will get a list of elements in the document that use a
CSS style with a certain value.

A generic abstract class - AbstractConditionalCSSElementListProducer -
provides the recursive search. 

A concrete class should be derived from this to provide the search criteria
- an example concrete class is provided - CSSCorelDeviationNegativeFontSize
- this specifies font-size with negative value.

This solves the issue I had whereby Corel Draw 12 was exporting negative
font sizes. After booting the batik CSS and DOM. I use this code to traverse
the GVT to get a list of the affected elements and delete them from the
document before I then use batik for other purposes. Otherwise if I didn't
do this, I would get an exception from an awt class (detailed in one of my
threads earlier) about negative stroke width.

However, the derived classes using the abstract class could do other useful
valid things with the CSS - like global replacing all elements affected by a
style. For example for accessibility reasons - changing the colour or size
of fonts.



import java.awt.Shape;
import java.util.List;
import java.util.ListIterator;
import java.util.Vector;

import org.apache.batik.bridge.BridgeContext;
import org.apache.batik.bridge.DocumentLoader;
import org.apache.batik.bridge.GVTBuilder;
import org.apache.batik.bridge.UpdateManager;
import org.apache.batik.bridge.UserAgent;
import org.apache.batik.bridge.UserAgentAdapter;
import org.apache.batik.css.engine.CSSStyleSheetNode;
import org.apache.batik.css.engine.Rule;
import org.apache.batik.css.engine.SVGCSSEngine;
import org.apache.batik.css.engine.StyleDeclaration;
import org.apache.batik.css.engine.StyleRule;
import org.apache.batik.css.engine.StyleSheet;
import org.apache.batik.css.engine.sac.CSSConditionalSelector;
import org.apache.batik.css.engine.value.FloatValue;
import org.apache.batik.css.engine.value.Value;
import org.apache.batik.css.engine.value.ValueManager;
import org.apache.batik.css.engine.value.css2.FontSizeManager;
import org.apache.batik.gvt.CompositeGraphicsNode;
import org.apache.batik.gvt.GraphicsNode;
import org.apache.batik.gvt.RootGraphicsNode;
import org.apache.batik.util.CSSConstants;
import org.w3c.css.sac.Selector;
import org.w3c.css.sac.SelectorList;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.css.CSSPrimitiveValue;


public abstract class AbstractConditionalCSSElementListProducer
{
	protected SVGCSSEngine cssEngine;
	protected Document doc;
	protected BridgeContext ctx;
	
	//protected RootGraphicsNode rootGN;

	protected GraphicsNode rootGN;

	 

	
	public AbstractConditionalCSSElementListProducer( Document document,
BridgeContext bridgeContext, GraphicsNode aGraphicsNode )
	{
	   doc = document;
		 ctx = bridgeContext;
		 
     rootGN = aGraphicsNode;
		 

		 	cssEngine = (SVGCSSEngine) ctx.getCSSEngineForElement(doc
		 	    .getDocumentElement());
		 	
      
	}
	
	private void updateElementsUsingCSSStyle(
	    CSSConditionalSelector cssConditionalSelector,
	    BridgeContext ctx, GraphicsNode graphicsNode,
	    ListIterator iteratorOnGraphicsNode, Vector elementList )
	{

		if (graphicsNode instanceof CompositeGraphicsNode)
		{

			CompositeGraphicsNode compositeGraphicsNode = (CompositeGraphicsNode)
graphicsNode;

			List rootGNchildren = compositeGraphicsNode.getChildren();



			for ( ListIterator gniterator = rootGNchildren.listIterator(); 
			      gniterator.hasNext();)
			{
				Object object = gniterator.next();

				updateElementsUsingCSSStyle(cssConditionalSelector, ctx,
				    (GraphicsNode)object, gniterator, elementList );

			}
		}
		else // its not a node that contains other nodes, so this is our recursion
stopping/windback
		{
			Element element = ctx.getElement((GraphicsNode) graphicsNode);

			if (element != null)
			{
				String elementName = element.getTagName();

				System.out.println("element.getTagName() " + elementName);

				String elementStyleClass = element.getAttribute("class");

				String textContent = element.getTextContent();

				System.out.println("element.getTextContent() " + textContent);

				System.out.println("element.getAttribute(\"class\") "
				    + elementStyleClass);
			
				boolean b = cssConditionalSelector.match(element, elementName);

				if (b)
				{
					System.out.println("\n\n");

					System.out.println("match");

					System.out.println("cssConditionalSelector.toString() "
					    + cssConditionalSelector.toString());

					System.out.println("element.getAttribute(\"class\") "
					    + elementStyleClass);

					System.out.println(element.getTextContent());

					System.out.println(element.getTextContent());
					
					elementList.add(element);
				}
			}
			
			return;
		}
	}

	
	
	







	public Vector getCSSConditionalSelectorList()
	    throws Exception

	{


		Vector cssList = new Vector();

		/*
		 * 
		 *  // Implements {@link ValueManager#getPropertyName()}.
		 * 
		 * public String getPropertyName() { return
		 * CSSConstants.CSS_FONT_SIZE_PROPERTY; }
		 * 
		 * 
		 * 
		 */







		// assuming one style sheet?

		List styleSheetsList = cssEngine.getStyleSheetNodes();

		CSSStyleSheetNode cssNode = (CSSStyleSheetNode) styleSheetsList.get(0);

		StyleSheet styleSheet = cssNode.getCSSStyleSheet();

		int numRules = styleSheet.getSize();

		for (int ruleIndex = 0; ruleIndex < numRules; ruleIndex++)
		{
			System.out.println("styleSheet.getRule(" + ruleIndex + ") "
			    + styleSheet.getRule(ruleIndex));

			Rule rule = styleSheet.getRule(ruleIndex);

			// rule.getType()" + rule.getType() );

			if (rule instanceof StyleRule)
			{
				StyleRule sr = ((StyleRule) rule);

				System.out.println("rule.getType() " + rule.getType());

				StyleDeclaration sd = ((StyleRule) rule).getStyleDeclaration();

				System.out.println("sr.toString() " + sr.toString());

				System.out.println("sd.toString() " + sd.toString());

				/*
				 * if ( sd instanceof CSSStyleDeclaration ) { csssd =
				 * (CSSStyleDeclaration)sd }
				 * 
				 * sd.getCSSText
				 */
				int sdlen = sd.size();

				for (int sdindex = 0; sdindex < sdlen; sdindex++)
				{

					Value val = sd.getValue(sdindex);

					System.out.println("sd.getIndex(sdindex) " + sd.getIndex(sdindex));

					if (condition(sd, sdindex))
					{
						

						/*
						 * System.out.println( "is negative:" + aFloatValue ); FloatValue
						 * positiveFloatValueObj = new
						 * FloatValue(CSSPrimitiveValue.CSS_NUMBER, Math.abs(aFloatValue) );
						 * 
						 * sd.put(sdindex, positiveFloatValueObj,
						 * CSSPrimitiveValue.CSS_NUMBER, false);
						 * 
						 * 
						 * 
						 * System.out.println( "sd.toString() " + sd.toString() );
						 * 
						 */

						SelectorList sl = sr.getSelectorList();

						int sllen = sl.getLength();

						for (int slindex = 0; slindex < sllen; slindex++)
						{

							Selector selector = sl.item(slindex);

							if (selector instanceof CSSConditionalSelector)
							{
								CSSConditionalSelector cssConditionalSelector =
(CSSConditionalSelector) selector;

								String styleClass = cssConditionalSelector.toString();

								System.out.println("cssConditionalSelector.toString() "
								    + styleClass);

							  cssList.add(cssConditionalSelector);
								

							}

						} // end for slindex

						// System.out.println( "val.getIdentifier() " + val.getIdentifier()
						// );

						// System.out.println( "val.getListStyle() " + val.getListStyle() );

						System.out
						    .println("val.getCssValueType() " + val.getCssValueType());

						System.out.println("val.getCssText() " + val.getCssText());

						// System.out.println( "val.getPrimitiveType()" +
						// val.getPrimitiveType() );

						// System.out.println( "val.getStringValue() " +
						// val.getStringValue() );

					} // end style declaration iteration

					// cater for DOM Exception

				}

				// (CSSConditionalSelector)
				// org.apache.batik.css.engine.StyleDeclaration sd =
				// ((StyleRule)rule).getStyleDeclaration();

				System.out.println("StyleRule");
			} // end instanceof style rule

			System.out.println("rule.toString()" + rule.toString());
		}

		System.out.println("styleSheet.getSize() " + styleSheet.getSize());

		System.out.println("styleSheet.toString() " + styleSheet.toString());


		System.out.println("got here");
		
		return cssList;

	}
	
	public Vector getElements() throws Exception
	{
		
		Vector cssConditionalSelectorList = getCSSConditionalSelectorList();
		
		Vector elementList = new Vector();
		
		for ( int i = 0; i < cssConditionalSelectorList.size(); i++ )
		{
			updateElementsUsingCSSStyle(
(CSSConditionalSelector)cssConditionalSelectorList.get(i), ctx,
					rootGN, null, elementList );

		}
		
		return elementList;

	}
	

	// this method is meant to be override
	protected boolean condition( StyleDeclaration sd, int sdindex )
	{
		return false;
	}

}






import org.apache.batik.bridge.BridgeContext;
import org.apache.batik.css.engine.SVGCSSEngine;
import org.apache.batik.css.engine.StyleDeclaration;
import org.apache.batik.css.engine.value.Value;
import org.apache.batik.css.engine.value.css2.FontSizeManager;
import org.apache.batik.gvt.GraphicsNode;
import org.apache.batik.gvt.RootGraphicsNode;
import org.apache.batik.util.CSSConstants;
import org.w3c.dom.Document;







 


public class CSSCorelDeviationNegativeFontSize extends
AbstractConditionalCSSElementListProducer {


private int propertyIndex;
	
public CSSCorelDeviationNegativeFontSize( Document doc, BridgeContext ctx,
GraphicsNode gn ) throws Exception
{
  super( doc, ctx, gn );

	
	String propertyName = CSSConstants.CSS_FONT_SIZE_PROPERTY;
	
	for ( int svgValueManagersIndex = 0; svgValueManagersIndex <
(cssEngine.SVG_VALUE_MANAGERS).length; svgValueManagersIndex++ )
	{
	  //if ( cssEngine.SVG_VALUE_MANAGERS[svgValueManagersIndex] instanceof
FontSizeManager )
		//CSSConstants.CSS_FONT_SIZE_PROPERTY
		
		if (
cssEngine.SVG_VALUE_MANAGERS[svgValueManagersIndex].getPropertyName().equals(propertyName)
)
	  {
		  System.out.println( "found font size manager " + svgValueManagersIndex
);
		  
		  propertyIndex = svgValueManagersIndex;
	  }
	}
	
	
}
	
protected boolean condition(StyleDeclaration sd, int sdindex )
{
	Value val = sd.getValue(sdindex);
	
  if ( sd.getIndex(sdindex) == propertyIndex )
  {
	   System.out.println( "found font size" );
	   
	   float aFloatValue = Float.valueOf(val.getCssText());
	   
	   if ( aFloatValue < 0.0F )
	   {
	  	 return true;
	   }
  }
  
  return false;
}
	
}

-- 
View this message in context: http://www.nabble.com/Modifying-internal-CSS-in-SVG-document---partial-solution---need-help-with-batik-re-draw-update-tp16275087p16536682.html
Sent from the Batik - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Mime
View raw message