/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.apache.batik.anim.dom;
import org.apache.batik.dom.svg.AbstractSVGList;
import org.apache.batik.dom.svg.ListHandler;
import org.apache.batik.dom.svg.SVGItem;
import org.apache.batik.parser.LengthListHandler;
import org.apache.batik.parser.LengthListParser;
import org.apache.batik.parser.ParseException;
import org.w3c.dom.DOMException;
import org.w3c.dom.Element;
import org.w3c.dom.svg.SVGException;
import org.w3c.dom.svg.SVGLength;
import org.w3c.dom.svg.SVGLengthList;
This class is the implementation of
SVGLengthList
.
Author: Nicolas Socheleau Version: $Id: AbstractSVGLengthList.java 1733416 2016-03-03 07:07:13Z gadams $
/**
* This class is the implementation of
* <code>SVGLengthList</code>.
*
* @author <a href="mailto:nicolas.socheleau@bitflash.com">Nicolas Socheleau</a>
* @version $Id: AbstractSVGLengthList.java 1733416 2016-03-03 07:07:13Z gadams $
*/
public abstract class AbstractSVGLengthList
extends AbstractSVGList
implements SVGLengthList {
This length list's direction.
/**
* This length list's direction.
*/
protected short direction;
Separator for a length list.
/**
* Separator for a length list.
*/
public static final String SVG_LENGTH_LIST_SEPARATOR
= " ";
Return the separator between values in the list.
/**
* Return the separator between values in the list.
*/
protected String getItemSeparator() {
return SVG_LENGTH_LIST_SEPARATOR;
}
Create an SVGException when the checkItemType fails.
Returns: SVGException
/**
* Create an SVGException when the checkItemType fails.
* @return SVGException
*/
protected abstract SVGException createSVGException(short type,
String key,
Object[] args);
Returns the element owning this SVGLengthList.
/**
* Returns the element owning this SVGLengthList.
*/
protected abstract Element getElement();
Creates a new SVGLengthList.
/**
* Creates a new SVGLengthList.
*/
protected AbstractSVGLengthList(short direction) {
this.direction = direction;
}
DOM: Implements SVGLengthList.initialize(SVGLength)
. /**
* <b>DOM</b>: Implements {@link SVGLengthList#initialize(SVGLength)}.
*/
public SVGLength initialize(SVGLength newItem)
throws DOMException, SVGException {
return (SVGLength) initializeImpl(newItem);
}
DOM: Implements SVGLengthList.getItem(int)
. /**
* <b>DOM</b>: Implements {@link SVGLengthList#getItem(int)}.
*/
public SVGLength getItem(int index) throws DOMException {
return (SVGLength) getItemImpl(index);
}
DOM: Implements SVGLengthList.insertItemBefore(SVGLength, int)
. /**
* <b>DOM</b>: Implements {@link
* SVGLengthList#insertItemBefore(SVGLength,int)}.
*/
public SVGLength insertItemBefore(SVGLength newItem, int index)
throws DOMException, SVGException {
return (SVGLength) insertItemBeforeImpl(newItem, index);
}
DOM: Implements SVGLengthList.replaceItem(SVGLength, int)
. /**
* <b>DOM</b>: Implements {@link
* SVGLengthList#replaceItem(SVGLength,int)}.
*/
public SVGLength replaceItem(SVGLength newItem, int index)
throws DOMException, SVGException {
return (SVGLength) replaceItemImpl(newItem,index);
}
DOM: Implements SVGLengthList.removeItem(int)
. /**
* <b>DOM</b>: Implements {@link SVGLengthList#removeItem(int)}.
*/
public SVGLength removeItem(int index) throws DOMException {
return (SVGLength) removeItemImpl(index);
}
DOM: Implements SVGLengthList.appendItem(SVGLength)
. /**
* <b>DOM</b>: Implements {@link SVGLengthList#appendItem(SVGLength)}.
*/
public SVGLength appendItem(SVGLength newItem)
throws DOMException, SVGException {
return (SVGLength) appendItemImpl(newItem);
}
/**
* Creates a new {@link SVGItem} object from the given {@link SVGLength}.
*/
protected SVGItem createSVGItem(Object newItem) {
SVGLength l = (SVGLength) newItem;
return new SVGLengthItem(l.getUnitType(), l.getValueInSpecifiedUnits(),
direction);
}
Parses the attribute associated with this SVGLengthList.
Params: - value – attribute value
- handler – length list handler
/**
* Parses the attribute associated with this SVGLengthList.
*
* @param value attribute value
* @param handler length list handler
*/
protected void doParse(String value, ListHandler handler)
throws ParseException{
LengthListParser lengthListParser = new LengthListParser();
LengthListBuilder builder = new LengthListBuilder(handler);
lengthListParser.setLengthListHandler(builder);
lengthListParser.parse(value);
}
Asserts that the given item is an SVGLengthList
. /**
* Asserts that the given item is an {@link SVGLengthList}.
*/
protected void checkItemType(Object newItem) throws SVGException {
if (!(newItem instanceof SVGLength)) {
createSVGException(SVGException.SVG_WRONG_TYPE_ERR,
"expected.length", null);
}
}
An SVGLength
in the list. /**
* An {@link SVGLength} in the list.
*/
protected class SVGLengthItem extends AbstractSVGLength implements SVGItem {
Creates a new SVGLengthItem.
/**
* Creates a new SVGLengthItem.
*/
public SVGLengthItem(short type, float value, short direction) {
super(direction);
this.unitType = type;
this.value = value;
}
Returns the element this length is associated with.
/**
* Returns the element this length is associated with.
*/
protected SVGOMElement getAssociatedElement() {
return (SVGOMElement) AbstractSVGLengthList.this.getElement();
}
List the item belongs to.
/**
* List the item belongs to.
*/
protected AbstractSVGList parentList;
Assigns a parent list to this item.
Params: - list – The list the item belongs.
/**
* Assigns a parent list to this item.
* @param list The list the item belongs.
*/
public void setParent(AbstractSVGList list) {
parentList = list;
}
Returns the parent list of this item.
/**
* Returns the parent list of this item.
*/
public AbstractSVGList getParent() {
return parentList;
}
Notifies the parent list that this item has changed.
/**
* Notifies the parent list that this item has changed.
*/
protected void reset() {
if (parentList != null) {
parentList.itemChanged();
}
}
}
Helper class to interface the LengthListParser
and the ListHandler
. /**
* Helper class to interface the {@link LengthListParser} and the
* {@link ListHandler}.
*/
protected class LengthListBuilder implements LengthListHandler {
The ListHandler to pass newly created SVGLengthItem
objects to. /**
* The ListHandler to pass newly created {@link SVGLengthItem} objects
* to.
*/
protected ListHandler listHandler;
The the length value just parsed.
/**
* The the length value just parsed.
*/
protected float currentValue;
The length unit just parsed.
/**
* The length unit just parsed.
*/
protected short currentType;
Creates a new LengthListBuilder.
/**
* Creates a new LengthListBuilder.
*/
public LengthListBuilder(ListHandler listHandler) {
this.listHandler = listHandler;
}
Implements LengthListHandler.startLengthList()
. /**
* Implements {@link LengthListHandler#startLengthList()}.
*/
public void startLengthList() throws ParseException {
listHandler.startList();
}
Implements LengthHandler.startLength()
. /**
* Implements {@link LengthListHandler#startLength()}.
*/
public void startLength() throws ParseException {
currentType = SVGLength.SVG_LENGTHTYPE_NUMBER;
currentValue = 0.0f;
}
Implements LengthHandler.lengthValue(float)
. /**
* Implements {@link LengthListHandler#lengthValue(float)}.
*/
public void lengthValue(float v) throws ParseException {
currentValue = v;
}
Implements LengthHandler.em()
. /**
* Implements {@link LengthListHandler#em()}.
*/
public void em() throws ParseException {
currentType = SVGLength.SVG_LENGTHTYPE_EMS;
}
Implements LengthHandler.ex()
. /**
* Implements {@link LengthListHandler#ex()}.
*/
public void ex() throws ParseException {
currentType = SVGLength.SVG_LENGTHTYPE_EXS;
}
Implements LengthHandler.in()
. /**
* Implements {@link LengthListHandler#in()}.
*/
public void in() throws ParseException {
currentType = SVGLength.SVG_LENGTHTYPE_IN;
}
Implements LengthHandler.cm()
. /**
* Implements {@link LengthListHandler#cm()}.
*/
public void cm() throws ParseException {
currentType = SVGLength.SVG_LENGTHTYPE_CM;
}
Implements LengthHandler.mm()
. /**
* Implements {@link LengthListHandler#mm()}.
*/
public void mm() throws ParseException {
currentType = SVGLength.SVG_LENGTHTYPE_MM;
}
Implements LengthHandler.pc()
. /**
* Implements {@link LengthListHandler#pc()}.
*/
public void pc() throws ParseException {
currentType = SVGLength.SVG_LENGTHTYPE_PC;
}
Implements LengthHandler.pt()
. /**
* Implements {@link LengthListHandler#pt()}.
*/
public void pt() throws ParseException {
currentType = SVGLength.SVG_LENGTHTYPE_EMS;
}
Implements LengthHandler.px()
. /**
* Implements {@link LengthListHandler#px()}.
*/
public void px() throws ParseException {
currentType = SVGLength.SVG_LENGTHTYPE_PX;
}
Implements LengthHandler.percentage()
. /**
* Implements {@link LengthListHandler#percentage()}.
*/
public void percentage() throws ParseException {
currentType = SVGLength.SVG_LENGTHTYPE_PERCENTAGE;
}
Implements LengthHandler.endLength()
. /**
* Implements {@link LengthListHandler#endLength()}.
*/
public void endLength() throws ParseException {
listHandler.item
(new SVGLengthItem(currentType,currentValue,direction));
}
Implements LengthListHandler.endLengthList()
. /**
* Implements {@link LengthListHandler#endLengthList()}.
*/
public void endLengthList() throws ParseException {
listHandler.endList();
}
}
}